Does Stream.take mutate the original stream?

In the documentation says

Lazily takes the next count elements from the enumerable and stops enumeration

As I understand what it does is to stop enumeration in the resulting stream, the original one remains unchanged.
Is that true?

Given elixir is a language with immutable data it can’t change even if it wanted to. So yes, the original stream stays as it was.

This depends on the implementation of the stream.

A file stream will advance the read position in the underlying file descriptor.

Therefore the rule of thumb is to not reuse a stream from an unknown source.

1 Like

I have recently used something like:

  fn -> [] end,
  fn acc ->
    receive do
      {:message, msg} -> {[msg], acc}
  fn _ -> [] end

I highly doubt that such as stream would be left “as it was”.

@ludo if you are looking for something like or Enum.split/2 that would return one extracted value and “updated” stream then unfortunately there is no such thing right now (and it is really sad, as it would be enormously handy function).