venomnert
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
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
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 + 5stream “get me a value”. It then has to tell thex * 5stream “get me a value”, which in turn calls thex + 2stream and so on.In the enum case, it just has to do a list traversal. In general I usually lean towards
Enumunless I’m doing something where the laziness will help avoid work. Here’s a good example:This will walk through a stream only as far as it needs to find the first operation where
%{successful: true}. If I didEnum.mapit’d have to do the expensive function for everything.venomnert
Ahh that makes sense. And it seems as though Enum is usually faster based on what I have been finding.
josevalim
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
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:
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
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.