Class: Campa::Lambda
- Inherits:
-
Object
- Object
- Campa::Lambda
- Defined in:
- lib/campa/lambda.rb
Overview
Represents an anonymous function that will be executed in a given Context.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#closure ⇒ Object
readonly
Returns the value of attribute closure.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #==(other) ⇒ Boolean
- #call(*args, env:) ⇒ Object
-
#initialize(params, body, closure = Context.new) ⇒ Lambda
constructor
A new instance of Lambda.
Constructor Details
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
38 39 40 |
# File 'lib/campa/lambda.rb', line 38 def body @body end |
#closure ⇒ Object (readonly)
Returns the value of attribute closure.
38 39 40 |
# File 'lib/campa/lambda.rb', line 38 def closure @closure end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
38 39 40 |
# File 'lib/campa/lambda.rb', line 38 def params @params end |
Instance Method Details
#==(other) ⇒ Boolean
Stablishes equality between Campa::Lambda objects by comparing #params and #body
86 87 88 89 90 |
# File 'lib/campa/lambda.rb', line 86 def ==(other) return false if !other.is_a?(Campa::Lambda) params == other.params && body == other.body end |
#call(*args, env:) ⇒ Object
Executes the expressions contained in the #body one by one using as Context the parameter env: on this method.
The env: param will be used here as a fallback to a brand new Context created in the moment of the invocation. This isolates the Context passed as a parameter of any mutations that would be created by the Campa::Lambda if there is any binding during the execution.
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/campa/lambda.rb', line 69 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 |