Class: Alf::Types::TupleComputation
- Inherits:
-
Object
- Object
- Alf::Types::TupleComputation
- Defined in:
- lib/alf/types/tuple_computation.rb
Overview
Defines the computation of a tuple from expressions mapped to names.
Example:
computation = TupleComputation[
:big? => lambda{ status > 20 }
:who => lambda{ "#{first} #{last}" }
]
computation.call(:last => "Jones", :first => "Bill", :status => 10)
# => {:big? => false, :who => "Bill Jones"}
Instance Method Summary collapse
-
#call(tuple) ⇒ Object
(also: #[])
Makes the computation in the context of a tuple.
-
#evaluate(scope = nil) ⇒ Hash
Computes the resulting tuple when expressions are evaluated in the context of
scope. -
#project(attributes, allbut = false) ⇒ Object
Projects this tuple computation on specified attribute names.
-
#to_attr_list ⇒ AttrList
Converts to an attribute list.
-
#to_heading ⇒ AttrList
Converts to a heading.
-
#to_lispy ⇒ String
Returns a lispy expression.
-
#to_ruby_literal ⇒ String
(also: #inspect, #to_s)
Returns a ruby literal for this expression.
-
#to_tuple_computation ⇒ Object
Returns self.
Instance Method Details
#call(tuple) ⇒ Object Also known as: []
Makes the computation in the context of a tuple
This is a convenient method for the following, longer expression:
evaluate(TupleScope.new(tuple))
Note, however, using a scope as in the example above is much more efficient when evaluating the same computation on multiple tuples in sequence.
47 48 49 |
# File 'lib/alf/types/tuple_computation.rb', line 47 def call(tuple) evaluate(Support::TupleScope.new(tuple)) end |
#evaluate(scope = nil) ⇒ Hash
Computes the resulting tuple when expressions are evaluated in the context of scope
57 58 59 |
# File 'lib/alf/types/tuple_computation.rb', line 57 def evaluate(scope = nil) hmap{|_,v| v.is_a?(TupleExpression) ? v.evaluate(scope) : v } end |
#project(attributes, allbut = false) ⇒ Object
Projects this tuple computation on specified attribute names.
65 66 67 68 |
# File 'lib/alf/types/tuple_computation.rb', line 65 def project(attributes, allbut = false) TupleComputation.new\ AttrList.coerce(attributes).project_tuple(reused_instance, allbut) end |
#to_attr_list ⇒ AttrList
Converts to an attribute list.
85 86 87 |
# File 'lib/alf/types/tuple_computation.rb', line 85 def to_attr_list AttrList.new(reused_instance.keys) end |
#to_heading ⇒ AttrList
Converts to a heading.
78 79 80 |
# File 'lib/alf/types/tuple_computation.rb', line 78 def to_heading Heading.new hmap{|_,v| v.is_a?(TupleExpression) ? v.infer_type : v.class} end |
#to_lispy ⇒ String
Returns a lispy expression.
92 93 94 |
# File 'lib/alf/types/tuple_computation.rb', line 92 def to_lispy "{" << map{|k,v| "#{k}: #{Support.to_lispy(v)}" }.join(', ') << "}" end |
#to_ruby_literal ⇒ String Also known as: inspect, to_s
Returns a ruby literal for this expression.
99 100 101 |
# File 'lib/alf/types/tuple_computation.rb', line 99 def to_ruby_literal "Alf::TupleComputation[#{to_lispy[1...-1]}]" end |
#to_tuple_computation ⇒ Object
Returns self
71 72 73 |
# File 'lib/alf/types/tuple_computation.rb', line 71 def to_tuple_computation self end |