Question : for loop in elixir
Answered by : yan-basile
for x <- 0..10 do IO.puts "x is: #{x}"
end
#But elixir programmer use more the following style of "loop" using Enum
Enum.each(0..9, fn x -> IO.puts "x is: #{x}"
end)
Source : | Last Update : Fri, 22 Apr 22
Question : elixir for loop
Answered by : kelvin-raphael
## Use Enum.map or Enum.each @spec map(t(), (element() -> any())) :: list()
Returns a list where each element is the result of invoking fun on each
corresponding element of enumerable.
For maps, the function expects a key-value tuple.
## Examples iex> Enum.map([1, 2, 3], fn x -> x * 2 end) [2, 4, 6] iex> Enum.map([a: 1, b: 2], fn {k, v} -> {k, -v} end) [a: -1, b: -2]
@spec each(t(), (element() -> any())) :: :ok
Invokes the given fun for each element in the enumerable.
Returns :ok.
## Examples Enum.each(["some", "example"], fn x -> IO.puts(x) end) "some" "example" #=> :ok
Source : | Last Update : Tue, 01 Mar 22