Elixir Language Lists Mapping Lists

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

map is a function in functional programming which given a list and a function, returns a new list with the function applied to each item in that list. In Elixir, the map/2 function is in the Enum module.

iex> Enum.map([1, 2, 3, 4], fn(x) -> x + 1 end)
[2, 3, 4, 5]

Using the alternative capture syntax for anonymous functions:

iex> Enum.map([1, 2, 3, 4], &(&1 + 1))
[2, 3, 4, 5]

Referring to a function with capture syntax:

iex> Enum.map([1, 2, 3, 4], &to_string/1)
["1", "2", "3", "4"]

Chaining list operations using the pipe operator:

iex> [1, 2, 3, 4]
...> |> Enum.map(&to_string/1)
...> |> Enum.map(&("Chapter " <> &1))
["Chapter 1", "Chapter 2", "Chapter 3", "Chapter 4"]


Got any Elixir Language Question?