class APL::AST::Function

Attributes

op[R]
x[R]
y[R]

Public Class Methods

new(op:, x:, y:) click to toggle source
# File lib/apl/ast/function.rb, line 6
def initialize(op:, x:, y:)
  @op = op
  @x = x
  @y = y
end

Private Class Methods

dyadic() click to toggle source
# File lib/apl/ast/function.rb, line 50
def self.dyadic
  @dyadic ||= {
    '+': ->(x, y) { x + y },
    '-': ->(x, y) { x - y },
    '×': ->(x, y) { x * y },
    '÷': ->(x, y) { x.to_f / y }
  }
end
monadic() click to toggle source
# File lib/apl/ast/function.rb, line 33
def self.monadic
  @monadic ||= {
    '+': ->(y) { y },
    '-': ->(y) { -y },
    '×': ->(y) do
      if y == 0
        0
      elsif y < 0
        -1
      else
        1
      end
    end,
    '÷': ->(y) { 1.0 / y }
  }
end

Public Instance Methods

==(other)
Alias for: eql?
compute!() click to toggle source
# File lib/apl/ast/function.rb, line 12
def compute!
  ops = monadic? ? self.class.monadic : self.class.dyadic
  block = ops[op] || raise(ArgumentError, "I don't know how to do the #{op} operation.")
  computed_args = args.map do |arg|
    begin
      arg.compute!
    rescue NoMethodError
      arg
    end
  end
  block.call *computed_args
end
eql?(other) click to toggle source
# File lib/apl/ast/function.rb, line 25
def eql?(other)
  [:op, :x, :y].all? {|field| self.send(field) == other.send(field) }
end
Also aliased as: ==

Private Instance Methods

args() click to toggle source
# File lib/apl/ast/function.rb, line 59
def args
  [x, y].compact
end
monadic?() click to toggle source
# File lib/apl/ast/function.rb, line 63
def monadic?
  x.nil?
end