miguelszerman
In the Enumerable chapter of the Elixir getting started guide there is a section called Lazy vs Eager. However, it never explains what Lazy or Eager means in the Elixir context.
There is an external mini-explanation on educative.io:
Enum, being eager, produces a whole list of numbers after each operation in the script until the result is reached. Conversely, Stream, being lazy, creates a stream that represents a function without executing it straight away.
A better explanation with the trade-offs, common use cases, and links to further resources would be nice to include in the guide.
Trending in Questions
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used?
I’ve got two Github accoun...
New
Other Trending Topics
I’ve been fixing race condition / crash bugs in an open source org, for a few months now and almost all of them had the same problem. The...
New
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
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
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #elixirconf-us
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dimitarvp
I don’t know about links and I am not a computer scientist by education but as a seasoned programmer I can offer you the following explanations.
— Eager loading —
Everything gets loaded in memory right away. Example 1:
File.read!loads the entire file into a single binary (string) and then passes it toCSV.parse_stringwhich also works with an entire binary. That’s eager loading – everything is there to be used in one go.Example 2:
Here you don’t just work with one list of 1000 elements; you work with four of them in total since each
Enum.maporEnum.filterproduces a new list that’s also loaded entirely in memory. Meaning the memory for all four lists will be allocated and used until they are thrown away.Something like 95% of the time you don’t care and it’s fine. But every now and then this is an awful idea because you don’t know beforehand how many records do you have to process in advance. Which brings us to…
— Lazy loading —
Stuff that you work with gets loaded in memory in chunks / batches. You never load the entire thing in memory.
Let’s take the Example 1 from above and turn it into lazy-loading code.
Notice how we replaced
File.read!withFile.stream!andCSV.parse_stringwithCSV.parse_stream. You should read quickly on these functions but basically they operate with anEnumerablethat allows them to pull data on demand (in batches). OK, maybe not the best example because you have to defer to documentation for an external library so let’s go to Example 2:The
Streamfunctions are usually identical with those with the same names fromEnumand do the same thing, only they never operate with the entire list given. In this case you only work with two lists in total: the original one and the resulting one which is produced by feeding a stream toEnum._to_list(NOTE: you can merge the last two steps by just doingEnum.filter(& &1 / 2 == 0)and it will have the same effect, but I opted for slightly longer code for illustrative purposes).The very good thing about this approach is that the original list doesn’t even have to be loaded into memory as well. Example 3 and that one is much closer to real-life scenarios:
I and many others have successfully used code like the above to process dozens of millions of DB records, while the code never loads more than 1000 at the same time.
Now this is not super formal or strictly adhering to the scientific definitions, surely, but is more like an answer to the question: “What does eager / lazy loading means when programming [in Elixir]?”.
TL;DR – it’s usually a protection from bursty memory loads. And it can sometimes slow down a competing
Enumimplementation if you go too micro (on my machines I never useStreamunless I have to operate with more than 3000-4000 records at a time).josevalim
Your description is great. Can you please submit a PR to add it to the guides?
dimitarvp
Swamped with work and this post was a bit of an anxious procrastination, admittedly.
I promise I’ll find a time slot in the next several days and will PR this – do you mind references to external libraries, or you are OK with them?
josevalim
Apologies for the confusion, your description was great. However, I was eyeing @miguelszerman’s summary, because it is small and therefore a perfect fit for an introductory guide.
Good news is that it is less work on your plate!
dimitarvp
Hahaha. I got greatness-blocked!