elixir enum each
To iterate over elements in Elixir using the Enum.each/2
function, you can provide a collection and a function to be applied to each element. The function will be called for each element in the collection.
Here's an example of how to use Enum.each/2
in Elixir:
list = [1, 2, 3, 4, 5]
Enum.each(list, fn element ->
IO.puts("Element: #{element}")
end)
In this example, we have a list [1, 2, 3, 4, 5]
. We use Enum.each/2
to iterate over each element in the list. For each element, we print a message using IO.puts/1
function.
The output will be:
Element: 1
Element: 2
Element: 3
Element: 4
Element: 5
Please note that the Enum.each/2
function is used for its side effects, such as printing or updating state. If you want to transform the elements of a collection and return a new collection, you can use other functions like Enum.map/2
.