Class: Rjq::AST::Reduce
Instance Attribute Summary collapse
-
#generator ⇒ Object
readonly
Returns the value of attribute generator.
-
#initial ⇒ Object
readonly
Returns the value of attribute initial.
-
#update ⇒ Object
readonly
Returns the value of attribute update.
-
#variable ⇒ Object
readonly
Returns the value of attribute variable.
Attributes inherited from Node
Instance Method Summary collapse
- #eval(input, context) ⇒ Object
-
#initialize(generator, variable, initial, update) ⇒ Reduce
constructor
A new instance of Reduce.
Methods inherited from Node
#assign_value, #invalid_path_message, #paths, #take, #with_source_span
Constructor Details
#initialize(generator, variable, initial, update) ⇒ Reduce
Returns a new instance of Reduce.
707 708 709 710 711 712 |
# File 'lib/rjq/ast.rb', line 707 def initialize(generator, variable, initial, update) @generator = generator @variable = variable @initial = initial @update = update end |
Instance Attribute Details
#generator ⇒ Object (readonly)
Returns the value of attribute generator.
705 706 707 |
# File 'lib/rjq/ast.rb', line 705 def generator @generator end |
#initial ⇒ Object (readonly)
Returns the value of attribute initial.
705 706 707 |
# File 'lib/rjq/ast.rb', line 705 def initial @initial end |
#update ⇒ Object (readonly)
Returns the value of attribute update.
705 706 707 |
# File 'lib/rjq/ast.rb', line 705 def update @update end |
#variable ⇒ Object (readonly)
Returns the value of attribute variable.
705 706 707 |
# File 'lib/rjq/ast.rb', line 705 def variable @variable end |
Instance Method Details
#eval(input, context) ⇒ Object
714 715 716 717 718 719 720 721 722 |
# File 'lib/rjq/ast.rb', line 714 def eval(input, context) accumulators = @initial.eval(input, context) @generator.eval(input, context).each do |value| ctx = AST.bind_pattern(context, @variable, value) next unless ctx accumulators = accumulators.flat_map { |accumulator| @update.eval(accumulator, ctx) } end accumulators end |