small medium large xlarge

Generic-user-small
05 Jul 2014, 10:02
Jamiel Almeida (1 post)

On page 110 of the pdf it has an example that reads:

# enum/stream2.exs
IO.puts File.open!("/usr/share/dict/words")
    |> IO.stream(:line)
    |> Enum.max_by(&String.length/1)

As an improvement to a previous example that reads:

# enum/longest_line.exs
IO.puts File.read!("/usr/share/dict/words")
    |> String.split
    |> Enum.max_by(&String.length/1)

And uses that as an example to explain how streams would help us not read the whole thing to memory (implicit, when they explain that the first example needs to read everything to memory).

Now, I may be missing something here, but, doesn’t the fact that we’re calling Enum.max_by/2 which expects a collection, not a stream, make the contents of the files still be in memory and implicitly cast the stream to a collection, hence defeating the whole point of the example’s benefits?