Method: Immutable::Stream#zip

Defined in:
lib/immutable/stream.rb

#zip(*xss) ⇒ Stream

Takes zero or more streams and returns a new stream in which each element is an array of the corresponding elements of self and the input streams.

Parameters:

  • xss (Array<Stream>)

    the input streams.

Returns:

  • (Stream)

    the new stream.



531
532
533
534
535
536
537
538
539
540
541
# File 'lib/immutable/stream.rb', line 531

def zip(*xss)
  Stream.lazy {
    if null?
      self
    else
      heads = xss.map { |xs| xs.null? ? nil : xs.head }
      tails = xss.map { |xs| xs.null? ? Stream.null : xs.tail }
      Stream.cons ->{ [head, *heads] }, ->{ tail.zip(*tails) }
    end
  }
end