module Fear::For

@api private @see Fear.for

Public Instance Methods

call(monads, inner_values = [], &block) click to toggle source

@param monads [<Fear::Option, Fear::Either, Fear::Try, Proc>]

# File lib/fear/for.rb, line 11
def call(monads, inner_values = [], &block)
  head, *tail = *monads

  if tail.length.zero?
    map(head, inner_values, &block)
  else
    flat_map(head, tail, inner_values, &block)
  end
end

Private Instance Methods

flat_map(head, tail, inner_values, &block) click to toggle source
# File lib/fear/for.rb, line 27
        def flat_map(head, tail, inner_values, &block)
  resolve(head, inner_values).flat_map do |x|
    call(tail, inner_values + [x], &block)
  end
end
map(head, inner_values) { |*inner_values, x| ... } click to toggle source
# File lib/fear/for.rb, line 21
        def map(head, inner_values)
  resolve(head, inner_values).map do |x|
    yield(*inner_values, x)
  end
end
resolve(monad_or_proc, inner_values) click to toggle source
# File lib/fear/for.rb, line 33
        def resolve(monad_or_proc, inner_values)
  if monad_or_proc.respond_to?(:call)
    monad_or_proc.(*inner_values)
  else
    monad_or_proc
  end
end