xgeek116

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

Showing Posts 1 to 5

kokolegorille

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.

{list_a, list_b} = Enum.reduce(..., {[], []}, fn el, acc -> ... end)
al2o3cr

al2o3cr

Like @LostKobrakai mentioned, scoping is one problem: code inside a do block 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:

# in Ruby
list_a = []
list_b = []

list_c.each do |item|
  item_a, item_b = process(item)
  list_a.concat(item_a.inner_list)
  list_b.concat(item_b.inner_list)
end
# in Python
list_a = []
list_b = []

for item in list_c:
  item_a, item_b = process(item)
  list_a.extend(item_a.inner_list)
  list_b.extend(item_b.inner_list)

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/1 on every element of list_c:

processed_list = Enum.map(list_c, &process/1)

Each element of processed_list is 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_a and list_b)

This kind of transformation comes up often enough that it has a name: Enum.unzip/1. Using it looks like:

tuple_of_lists = Enum.unzip(processed_list)

Now tuple_of_lists is shaped like {list_of_item_as, list_of_item_bs}


Finally, we need to extract the inner_list parts. We could use Enum.map again:

# not quite right - see below for a correct solution
{list_of_as, list_of_bs} = tuple_of_lists
list_a = Enum.map(list_of_as, & &1.inner_list)
list_b = Enum.map(list_of_bs, & &1.inner_list)

but this doesn’t quite do what we’re looking for: it results in list_a and list_b being lists of lists. You could use List.flatten/1, if the values in inner_list are 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:

{list_of_as, list_of_bs} = tuple_of_lists
list_a = Enum.flat_map(list_of_as, & &1.inner_list)
list_b = Enum.flat_map(list_of_bs, & &1.inner_list)

Putting all the pieces together gives this code:

processed_list = Enum.map(list_c, &process/1)

tuple_of_lists = Enum.unzip(processed_list)

{list_of_as, list_of_bs} = tuple_of_lists
list_a = Enum.flat_map(list_of_as, & &1.inner_list)
list_b = Enum.flat_map(list_of_bs, & &1.inner_list)

This code can be tidied up with some Elixir syntax goodies:

# squish "processed_list" out of existence since it is bound and then immediately used
tuple_of_lists =
  list_c
  |> Enum.map(&process/1)
  |> Enum.unzip()

{list_of_as, list_of_bs} = tuple_of_lists
list_a = Enum.flat_map(list_of_as, & &1.inner_list)
list_b = Enum.flat_map(list_of_bs, & &1.inner_list)

and even more with then which was recently added:

{list_a, list_b} =
  list_c
  |> Enum.map(&process/1)
  |> Enum.unzip()
  |> then(fn {list_of_as, list_of_bs} ->
    {
      Enum.flat_map(list_of_as, & &1.inner_list),
      Enum.flat_map(list_of_bs, & &1.inner_list)
    }
  end)

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_items and tuple_of_lists).

xgeek116

xgeek116 OP

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

al2o3cr

& here is the “function capture operator”. There’s a great explanation in the Getting Started guide.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
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
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
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
samoloth
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

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews