Search code examples
elixirfunction-call

Elixir function calling in a loop with 2 parameters


Is it possible in Elixir to call a function i times with 2 parameters? It would look line this in Java:

List myList = new ArrayList();
for(int i=1; i<10; i++) {
   myList.addAll(doSomething(other_list,i))
}

Solution

  • Basing on your Java code, I suppose doSomething(otherList, i) returns a list. If so, then you can use Enum.flat_map in Elixir, like this:

    Enum.flat_map(1..9, fn i ->
      do_something(other_list, i)
    end)