stream_split v0.1.0 StreamSplit

Summary

Functions

This function looks at the first n items in a stream. The remainder of the enumerable is returned as a stream that may be lazily enumerated at a later time

This function is a combination of Enum.take/2 and Enum.drop/2 returning first n dropped elements and the rest of the enum as a stream

Functions

peek(enum, n)
peek(Enumerable.t, pos_integer) :: {List.t, Enumerable.t}

This function looks at the first n items in a stream. The remainder of the enumerable is returned as a stream that may be lazily enumerated at a later time.

You may think of this function as popping n items of the enumerable, then pushing them back after making a copy.

Use this function with a stream to peek at items, but not iterate a stream with side effects more than once.

Examples

iex> {head, new_enum} = peek(Stream.cycle(1..3), 4)
iex> head
[1, 2, 3, 1]
iex> Enum.take(new_enum, 7)
[1, 2, 3, 1, 2, 3, 1]
take_and_drop(enum, n)
take_and_drop(Enumerable.t, pos_integer) :: {List.t, Enumerable.t}

This function is a combination of Enum.take/2 and Enum.drop/2 returning first n dropped elements and the rest of the enum as a stream.

The important difference is that the enumerable is only iterated once, and only for the required n items. The rest of the enumerable may be iterated lazily later from the returned stream.

Examples

iex> {head, tail} = take_and_drop(Stream.cycle(1..3), 4)
iex> head
[1, 2, 3, 1]
iex> Enum.take(tail, 7)
[2, 3, 1, 2, 3, 1, 2]