module HigherOrderFunctions

Higher-order functions

Constants

VERSION

Public Instance Methods

inject_right(init: nil, &block) click to toggle source
# File lib/higher_order_functions.rb, line 37
def inject_right(init: nil, &block)
  raise ArgumentError unless block_given?

  init ? reverse_each.inject(init, &block) : reverse_each.inject(&block)
end
scan() { |last, item| ... } click to toggle source
# File lib/higher_order_functions.rb, line 30
def scan
  raise ArgumentError unless block_given?

  drop(1).inject([first]) { |result, item| result << yield(result.last, item) }
end
sequence() { |element| ... } click to toggle source
# File lib/higher_order_functions.rb, line 19
def sequence
  raise ArgumentError unless block_given?

  element = self
  Enumerator.new do |enumerator|
    enumerator << element
    loop { enumerator << (element = yield(element)) }
  end
end
unfold(condition, function) { |seed| ... } click to toggle source
# File lib/higher_order_functions.rb, line 7
def unfold(condition, function)
  raise ArgumentError unless block_given?

  seed = self
  array = []
  until condition[seed]
    array << function[seed]
    seed = yield(seed)
  end
  array
end