Class: Campa::Lambda

Inherits:
Object
  • Object
show all
Defined in:
lib/campa/lambda.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, body, closure = Context.new) ⇒ Lambda

Returns a new instance of Lambda.



5
6
7
8
9
10
# File 'lib/campa/lambda.rb', line 5

def initialize(params, body, closure = Context.new)
  @params = params
  @body = Array(body)
  @closure = closure
  @evaler = Evaler.new
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/campa/lambda.rb', line 3

def body
  @body
end

#closureObject (readonly)

Returns the value of attribute closure.



3
4
5
# File 'lib/campa/lambda.rb', line 3

def closure
  @closure
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/campa/lambda.rb', line 3

def params
  @params
end

Instance Method Details

#==(other) ⇒ Object



23
24
25
26
27
# File 'lib/campa/lambda.rb', line 23

def ==(other)
  return false if !other.is_a?(Campa::Lambda)

  params == other.params && body == other.body
end

#call(*args, env:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/campa/lambda.rb', line 12

def call(*args, env:)
  raise arity_error(args) if params.to_a.length != args.length

  @body.reduce(nil) do |_, expression|
    evaler.call(
      expression,
      invocation_env(env, args)
    )
  end
end