Hasimbola

Hasimbola

Multiple response from loop

Hello everybody,
Someone can explain me this result please?
I have 2 list
x = [“0001”, “0002”]
y = [1,1]

And I loop them like this:

for x1 ← x do
for y1<- y do
IO.inspect x1
IO.inspect y1
end
end

But the result is like this:

“0001”
1
“0001”
1
“0002”
1
“0002”
1

But I want result like this :

“0001”
1
“0002”
1
Thanks! :smiley:

Marked As Solved

sabiwara

sabiwara

Elixir Core Team

Do you mean something like Enum.zip/2?

for {x1, y1} <- Enum.zip(x, y) do

Also Liked

Eiji

Eiji

If Enum.zip/2 solves your problem then consider using [head | tail] notation for lists like:

defmodule Example do
  # when done or empty input do nothing returning empty list
  def sample([], []), do: []

  # optional code in case you have more items in one of the list
  # def sample([], _non_empty_list), do: []
  # def sample(_non_empty_list, []), do: []

  # loop using [head | tail] notation together with your code
  def sample([head1 | tail1], [head2 | tail2]) do
    IO.puts(head1)
    IO.puts(head2)
    sample(tail1, tail2)
  end
end

x = ["0001", "0002"]
y = [1, 1]
Example.sample(x, y)

This gave you exactly same result, but … Enum.zip is one loop and for is another one - this means that if you write a bit more code then using just one loop your code would be faster.

sabiwara

sabiwara

Elixir Core Team

Indeed, it depends on the actual use case and what we want to return.

We might also want to reach for Enum.zip_reduce/4 (for an arbitrary accumulator) or Enum.zip_with/3 (if we want to return a list).

Enum.zip_reduce(x, y, :ok, fn x1, y1, _acc ->
  IO.puts(x1)
  IO.puts(y1)
end)

Last Post!

sabiwara

sabiwara

Elixir Core Team

Indeed, it depends on the actual use case and what we want to return.

We might also want to reach for Enum.zip_reduce/4 (for an arbitrary accumulator) or Enum.zip_with/3 (if we want to return a list).

Enum.zip_reduce(x, y, :ok, fn x1, y1, _acc ->
  IO.puts(x1)
  IO.puts(y1)
end)

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New

We're in Beta

About us Mission Statement