Method: Immutable::Stream#zip_with

Defined in:
lib/immutable/stream.rb

#zip_with(*xss, &block) ⇒ Stream

Takes zero or more streams and returns the stream obtained by applying the given block to an array of the corresponding elements of self and the input streams. xs.zip_with(*yss, &block) is equivalent to xs.zip(*yss).map(&block).

Parameters:

  • xss (Array<Stream>)

    the input streams.

Returns:

  • (Stream)

    the new stream.



440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/immutable/stream.rb', line 440

def zip_with(*xss, &block)
  Stream.lazy {
    if empty?
      self
    else
      heads = xss.map { |xs| xs.empty? ? nil : xs.head }
      tails = xss.map { |xs| xs.empty? ? Stream.empty : xs.tail }
      h = yield(head, *heads)
      Stream.cons ->{ h }, ->{ tail.zip_with(*tails, &block) }
    end
  }
end