stream_split v0.1.3 StreamSplit

Link to this section 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 may be seen as splitting head and tail for a List, but for enumerables.

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.

Link to this section 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.

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]

This function may be seen as splitting head and tail for a List, but for enumerables.

It is implemented on top of take_and_drop/2

Examples

iex> {head, tail} = pop(Stream.cycle(1..3))
iex> head
1
iex> Enum.take(tail, 7)
[2, 3, 1, 2, 3, 1, 2]
Link to this function

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]