xgeek116

xgeek116

Fill a list after Enum.Each Loo^p

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

Marked As Solved

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).

Also Liked

Where Next?

Popular in Questions Top

SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
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
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement