Class: CallableTree::Node::Internal::Strategy::Seek

Inherits:
Object
  • Object
show all
Includes:
CallableTree::Node::Internal::Strategy
Defined in:
lib/callable_tree/node/internal/strategy/seek.rb

Instance Method Summary collapse

Methods included from CallableTree::Node::Internal::Strategy

#==, #eql?, #hash, #name, #terminable?

Constructor Details

#initialize(terminable: true) ⇒ Seek



10
11
12
13
14
15
16
17
18
# File 'lib/callable_tree/node/internal/strategy/seek.rb', line 10

def initialize(terminable: true)
  self.terminable = terminable
  @terminator =
    if terminable
      proc { |node, output, *inputs, **options| node.terminate?(output, *inputs, **options) }
    else
      proc { false }
    end
end

Instance Method Details

#call(nodes, *inputs, **options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/callable_tree/node/internal/strategy/seek.rb', line 20

def call(nodes, *inputs, **options)
  nodes
    .lazy
    .select { |node| node.match?(*inputs, **options) }
    .map do |node|
      output = node.call(*inputs, **options)
      terminated = @terminator.call(node, output, *inputs, **options)
      [output, terminated]
    end
    .select { |_output, terminated| terminated }
    .map { |output, _terminated| output }
    .first
end