Class: Rjq::AST::Reduce

Inherits:
Node
  • Object
show all
Defined in:
lib/rjq/ast.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#source_span

Instance Method Summary collapse

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

#generatorObject (readonly)

Returns the value of attribute generator.



705
706
707
# File 'lib/rjq/ast.rb', line 705

def generator
  @generator
end

#initialObject (readonly)

Returns the value of attribute initial.



705
706
707
# File 'lib/rjq/ast.rb', line 705

def initial
  @initial
end

#updateObject (readonly)

Returns the value of attribute update.



705
706
707
# File 'lib/rjq/ast.rb', line 705

def update
  @update
end

#variableObject (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