Module: Factbase::Math

Included in:
Term
Defined in:
lib/factbase/terms/math.rb

Overview

Math terms.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Instance Method Details

#_arithmetic(op, fact, maps, fb) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/factbase/terms/math.rb', line 72

def _arithmetic(op, fact, maps, fb)
  assert_args(2)
  lefts = _values(0, fact, maps, fb)
  return nil if lefts.nil?
  raise 'Too many values at first position, one expected' unless lefts.size == 1
  rights = _values(1, fact, maps, fb)
  return nil if rights.nil?
  raise 'Too many values at second position, one expected' unless rights.size == 1
  v = lefts[0]
  r = rights[0]
  if v.is_a?(Time) && r.is_a?(String)
    (num, units) = r.split
    num = num.to_i
    r =
      case units
      when 'seconds', 'second'
        num
      when 'minutes', 'minute'
        num * 60
      when 'hours', 'hour'
        num * 60 * 60
      when 'days', 'day'
        num * 60 * 60 * 24
      when 'weeks', 'week'
        num * 60 * 60 * 24 * 7
      else
        raise "Unknown time unit '#{units}' in '#{r}"
      end
  end
  v.send(op, r)
end

#_cmp(op, fact, maps, fb) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/factbase/terms/math.rb', line 57

def _cmp(op, fact, maps, fb)
  assert_args(2)
  lefts = _values(0, fact, maps, fb)
  return false if lefts.nil?
  rights = _values(1, fact, maps, fb)
  return false if rights.nil?
  lefts.any? do |l|
    l = l.floor if l.is_a?(Time) && op == :==
    rights.any? do |r|
      r = r.floor if r.is_a?(Time) && op == :==
      l.send(op, r)
    end
  end
end

#div(fact, maps, fb) ⇒ Object



26
27
28
# File 'lib/factbase/terms/math.rb', line 26

def div(fact, maps, fb)
  _arithmetic(:/, fact, maps, fb)
end

#eq(fact, maps, fb) ⇒ Object



37
38
39
# File 'lib/factbase/terms/math.rb', line 37

def eq(fact, maps, fb)
  _cmp(:==, fact, maps, fb)
end

#gt(fact, maps, fb) ⇒ Object



45
46
47
# File 'lib/factbase/terms/math.rb', line 45

def gt(fact, maps, fb)
  _cmp(:>, fact, maps, fb)
end

#gte(fact, maps, fb) ⇒ Object



53
54
55
# File 'lib/factbase/terms/math.rb', line 53

def gte(fact, maps, fb)
  _cmp(:>=, fact, maps, fb)
end

#lt(fact, maps, fb) ⇒ Object



41
42
43
# File 'lib/factbase/terms/math.rb', line 41

def lt(fact, maps, fb)
  _cmp(:<, fact, maps, fb)
end

#lte(fact, maps, fb) ⇒ Object



49
50
51
# File 'lib/factbase/terms/math.rb', line 49

def lte(fact, maps, fb)
  _cmp(:<=, fact, maps, fb)
end

#minus(fact, maps, fb) ⇒ Object



18
19
20
# File 'lib/factbase/terms/math.rb', line 18

def minus(fact, maps, fb)
  _arithmetic(:-, fact, maps, fb)
end

#plus(fact, maps, fb) ⇒ Object



14
15
16
# File 'lib/factbase/terms/math.rb', line 14

def plus(fact, maps, fb)
  _arithmetic(:+, fact, maps, fb)
end

#times(fact, maps, fb) ⇒ Object



22
23
24
# File 'lib/factbase/terms/math.rb', line 22

def times(fact, maps, fb)
  _arithmetic(:*, fact, maps, fb)
end

#zero(fact, maps, fb) ⇒ Object



30
31
32
33
34
35
# File 'lib/factbase/terms/math.rb', line 30

def zero(fact, maps, fb)
  assert_args(1)
  vv = _values(0, fact, maps, fb)
  return false if vv.nil?
  vv.any? { |v| (v.is_a?(Integer) || v.is_a?(Float)) && v.zero? }
end