Class: BusScheme::Lambda

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

Constant Summary collapse

@@stack =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg_names, body) ⇒ Lambda

create new lambda object



34
35
36
# File 'lib/lambda.rb', line 34

def initialize(arg_names, body)
  @arg_names, @body, @enclosing_scope = [arg_names, body, Lambda.scope]
end

Instance Attribute Details

#scopeObject (readonly)

Returns the value of attribute scope.



31
32
33
# File 'lib/lambda.rb', line 31

def scope
  @scope
end

Class Method Details

.scopeObject



46
47
48
# File 'lib/lambda.rb', line 46

def self.scope
  @@stack.empty? ? nil : @@stack.last.scope
end

Instance Method Details

#call(*arg_values) ⇒ Object

execute lambda with given arg_values



39
40
41
42
43
44
# File 'lib/lambda.rb', line 39

def call(*arg_values)
  raise BusScheme::ArgumentError if @arg_names.length != arg_values.length
  @scope = Scope.new(@arg_names.zip(arg_values).to_hash, @enclosing_scope)
  @@stack << self
  BusScheme[:begin].call(*@body).affect { @@stack.pop }
end