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

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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 49084 226
New

We're in Beta

About us Mission Statement