Method: Functional::Tuple#sequence

Defined in:
lib/functional/tuple.rb

#sequence {|head, tail| ... } ⇒ Enumerable

Calls the given block once for each element in self, passing that element and a tuple with all the remaining items in the tuple. When the last item is reached ab empty tuple is passed as the second parameter. This is the classic functional programming ‘head|tail` list processing idiom. An Enumerator is returned if no block is given.

Yield Parameters:

  • head (Object)

    the current item for this iteration

  • tail (Tuple)

    the remaining items (tail) or an empty tuple when processing the last item

Returns:

  • (Enumerable)

    when no block is given



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/functional/tuple.rb', line 177

def sequence
  return enum_for(:sequence) unless block_given?
  @data.length.times do |index|
    last = @data.length - 1
    if index == last
      yield(@data[index], Tuple.new)
    else
      yield(@data[index], Tuple.new(@data.slice(index+1..last)))
    end
  end
end