class Funk::Evaluators::Eager

Attributes

graph[R]

Public Class Methods

new(graph, instruments: []) click to toggle source
# File lib/funk/evaluators/eager.rb, line 8
def initialize(graph, instruments: [])
  @graph, @instruments = graph, instruments
end

Public Instance Methods

call(input) click to toggle source
# File lib/funk/evaluators/eager.rb, line 12
def call(input)
  instruments = @instruments.map{ |i| i.new }
  @graph.tsort.each_with_object(Result.new(instruments)) do |fn, result|
    result[fn.name] = evaluate(fn, result.merge(input), instruments)
  end
end

Private Instance Methods

after_call(fn, input, value, instruments) click to toggle source
# File lib/funk/evaluators/eager.rb, line 34
def after_call(fn, input, value, instruments)
  instruments.reverse.each do |inst|
    inst.after_call(fn, input, value) if inst.respond_to?(:after_call)
  end
end
before_call(fn, input, instruments) click to toggle source
# File lib/funk/evaluators/eager.rb, line 28
def before_call(fn, input, instruments)
  instruments.each do |inst|
    inst.before_call(fn, input) if inst.respond_to?(:before_call)
  end
end
evaluate(fn, input, instruments) click to toggle source
# File lib/funk/evaluators/eager.rb, line 21
def evaluate(fn, input, instruments)
  before_call(fn, input, instruments)
  value = fn.call(input)
  after_call(fn, input, value, instruments)
  value
end