xgeek116
I’m trying to fill a list from the result of many loops (iterating another list) but the result is always empty here is my code :
list_a = []
list_b = []
Enum.each(list_c, fn(item) ->
{item_a, item_b} = process(item)
list_a ++ item_a.inner_list
list_b ++ item_b.inner_list
end)
For each iteration, item_a and item_b have a list named inner_list, so I tried to concatenate each time that list with the result lists but list_a and list_b are always empty.
Thanks in advance
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
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
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
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
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
See this: Why does the order for making a redirect and putting a new variable in conn matter? - #8 by LostKobrakai
kokolegorille
As mentionned by previous post, it will not work because of scope…
Also your code fits mutable language, but not immutable.
It does not mean You cannot do it, You have to do it in a different way.
al2o3cr
Like @LostKobrakai mentioned, scoping is one problem: code inside a
doblock can’t rebind variables outside of that block in a way that’s visible outside.The other problem is that
++makes a new list, it doesn’t mutate either argument. Code like what you posted is pretty common in other languages:This approach will not work in Elixir. My suggestion is that you temporarily forget you even saw
Enum.each; it’s only useful in a very narrow set of situations.A better way to think about solving these kinds of problems in Elixir is to focus on how the “shape” of the data changes.
As a first step, we want to run
process/1on every element oflist_c:Each element of
processed_listis a tuple shaped like{item_a, item_b}.There are several possibilities here: we could refine these tuples by extracting
inner_list, but instead we’ll start by regrouping them.What we have is a list of 2-element tuples.
What we want is a 2-element tuple of lists (what the original called
list_aandlist_b)This kind of transformation comes up often enough that it has a name:
Enum.unzip/1. Using it looks like:Now
tuple_of_listsis shaped like{list_of_item_as, list_of_item_bs}Finally, we need to extract the
inner_listparts. We could useEnum.mapagain:but this doesn’t quite do what we’re looking for: it results in
list_aandlist_bbeing lists of lists. You could useList.flatten/1, if the values ininner_listare not themselves lists.BUT
There’s an easier way! Again, what we’re looking for is referenced enough to have a name:
Enum.flat_map:Putting all the pieces together gives this code:
This code can be tidied up with some Elixir syntax goodies:
and even more with
thenwhich was recently added:This code does exactly the same steps as the first “un-simplified” version, but avoids having to name a bunch of variables that are used exactly once (like
processed_itemsandtuple_of_lists).xgeek116
Thanks a lot @al2o3cr , that is a great answer and it worked !
I have some questions if you can help me about your solution :
processed_list = Enum.map(list_c, **&**process/1), in this line why we put & before the function name ?
list_a = Enum.map(list_of_as, **& &1.**inner_list), in this line what is **& &1. ?
Thanks
al2o3cr
&here is the “function capture operator”. There’s a great explanation in the Getting Started guide.