Method: Immutable::List#span

Defined in:
lib/immutable/list.rb

#span {|item| ... } ⇒ Array

Return two ‘List`s, one up to (but not including) the first item for which the block returns nil or false, and another of all the remaining items.

Examples:

Immutable::List[4, 3, 5, 2, 1].span { |x| x > 2 }
# => [Immutable::List[4, 3, 5], Immutable::List[2, 1]]

Yields:

  • (item)

Returns:

  • (Array)


508
509
510
511
512
513
514
# File 'lib/immutable/list.rb', line 508

def span(&block)
  return [self, EmptyList].freeze unless block_given?
  splitter = Splitter.new(self, block)
  mutex = Mutex.new
  [Splitter::Left.new(splitter, splitter.left, mutex),
   Splitter::Right.new(splitter, mutex)].freeze
end