Home > Mobile >  How to fill a list in Elixir with similar method like for loops in other languages
How to fill a list in Elixir with similar method like for loops in other languages

Time:09-27

I would like to make a list with 2 integer in Elixir like this:

@type pairs::{i:: integer, j::integer}

@spec my_list(i:: integer, j::integer) :: res::[pairs]
def my_list(i, j) do
  ...
end

If i=3 and j=2 I would like to get this form: [{1,1},{1,2},{2,1},{2,2},{3,1},{3,2}].

CodePudding user response:

You are after for/1 comprehension.

for i <- 1..3, j <- 1..2, do: {i, j}

Please note, that there are also many other ways to accomplish this task, like Enum and/or Stream modules functions.

  • Related