venomnert

venomnert

Understanding Streams and Enum

Hey guys,

I came across the idea of Stream, Enum, lazy evaluation and eager evaluation recently. I have been trying to wrap my head around the idea of these concepts this week. I reached out to Elixir slack and got a better understanding. So, I was hoping if you guys can review my explanation of Streams and Enum and let me know if I understood it correctly.

Let’s work with following example.

Let’s say we have the following list of numbers [1,…,1000]. We want to apply the following transformation on it: add by 2, multiply by 5, and add by 5.

For Enum here is the following implementation:
1…1000
|> Enum.map(fn(x) -> x + 2 end)
|> Enum.map(fn(x) -> x * 5 end)
|> Enum.map(fn(x) -> x + 5 end)

The following will happen:

  • For Enum.map(fn(x) -> x + 2 end) the map will go over 1000 iteration and will produce a new list [2, …, 1002]
  • For Enum.map(fn(x) -> x * 5 end) the map will go over 1000 iteration and will produce a new list [10, …,5010]
  • For Enum.map(fn(x) -> x + 5 end) the map will go over 1000 iteration and will produce a new list [15, …,5015]
  • So the result is we have iterate 3000 times and produced 3 new lists

For Stream here is the following implementation:
1…1000
|> Stream.map(fn(x) -> x + 2 end)
|> Stream.map(fn(x) -> x * 5 end)
|> Stream.map(fn(x) -> x + 5 end)
|> Enum.take(1000)

The following will happen:

  • For Stream.map(fn(x) -> x + 2 end) will wrap around the list [1,…,1000] and return as a stream
  • For Stream.map(fn(x) -> x * 5 end) will wrap around the previous stream and return a stream
  • For Stream.map(fn(x) -> x + 5 end) will wrap around the previous stream and return a stream
    • So it would look like the following: Stream.map( fn( fn( fn(x) -> x + 2 end ) -> x * 5 end ) -> x + 5 end)
    • Where x is the first element of the list so x = 1
  • For Enum.take(1000) execute the above function and return all the elements from the list
  • So the result is we have iterate 1000 times and produced 1 new list

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

That is correct yes. It might seem therefore that using a stream is always better, since you have fewer traversals. This isn’t always the case though, since although you only go through the list 1000 times, getting each value has more overhead. To get the first item, the Enum.take has to tell the x + 5 stream “get me a value”. It then has to tell the x * 5 stream “get me a value”, which in turn calls the x + 2 stream and so on.

In the enum case, it just has to do a list traversal. In general I usually lean towards Enum unless I’m doing something where the laziness will help avoid work. Here’s a good example:

some_list
|> Stream.map(&expensive_function/1)
|> Enum.find(& &1.successful)

This will walk through a stream only as far as it needs to find the first operation where %{successful: true}. If I did Enum.map it’d have to do the expensive function for everything.

Also Liked

josevalim

josevalim

Creator of Elixir

Yes. I like to say that streams are about using less memory at the cost of CPU. Streams will only be faster for quite large collections (such as infinite ones, which would never finish with Enum) or a high amount of traversals.

sasajuric

sasajuric

Author of Elixir In Action

For smaller inputs and/or just one transformation Enum will be faster. For larger inputs with multiple transformations, using streams in the middle can sometimes be dramatically faster because we don’t generate large intermediate lists. More importantly, memory usage will be stable, thus reducing the chance of blowing up the production for some unexpected large input. That’s why I usually write a transformation pipeline in the style of:

input_enumerable
|> Stream.trans_1
|> Stream.trans_2
|> ...
|> Enum.last_trans

As always, there are some gotchas. If your code consumes an enumerable multiple times, it’s usually better to make sure that the input enumerable is not a stream, to avoid needless (and sometimes quite costly) duplicate computations.

dimitarvp

dimitarvp

There is no magic number for everyone but anything requiring processing of 2000 items and above I always delegate to streams – the concrete scenario allowing.

As others mentioned, this stabilizes your memory usage and vastly reduces the chance of your production code to get killed off by watchdogs if its RAM usage spikes sharply. RAM is much more valuable and stringently monitored and controlled compared to CPU or I/O operations on most hosting providers.

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement