Stream.take
You're seeing just the function
take
, go back to Stream module for more information.
Specs
take(Enumerable.t(), integer()) :: Enumerable.t()
Lazily takes the next count
elements from the enumerable and stops
enumeration.
If a negative count
is given, the last count
values will be taken.
For such, the collection is fully enumerated keeping up to 2 * count
elements in memory. Once the end of the collection is reached,
the last count
elements will be executed. Therefore, using
a negative count
on an infinite collection will never return.
Examples
iex> stream = Stream.take(1..100, 5)
iex> Enum.to_list(stream)
[1, 2, 3, 4, 5]
iex> stream = Stream.take(1..100, -5)
iex> Enum.to_list(stream)
[96, 97, 98, 99, 100]
iex> stream = Stream.cycle([1, 2, 3]) |> Stream.take(5)
iex> Enum.to_list(stream)
[1, 2, 3, 1, 2]