The expression in list comprehension is the operation or transformation applied to each element of the iterable, usually following the do
keyword.
Key takeaways:
Elixir is a functional, dynamically typed programming language.
Lists in Elixir are immutable and implemented using linked lists.
List comprehensions generate new lists by applying operations to existing lists.
Syntax includes
for
,pattern
,enumerable
,filter
, anddo
.List comprehensions improve code readability and facilitate efficient list manipulation.
Elixir is a functional, dynamically typed, high-level, general-purpose programming language first released in 2011. When working with data structures, Elixir provides numerous built-in data types, making it easy to handle and manipulate data.
Lists are used to store collections of values of the same or different types. Lists are immutable and implemented using linked lists. Elixir supports list comprehensions, which concisely generate new lists by applying some operation/function to each element of an existing list.
The basic syntax of list comprehension is given below:
newlist = for pattern <- enumerable, filter, do: expression
for
: It is used to start a comprehension.
pattern
: It is a variable used to take values from given enumerable
one by one.
enumerable
: It is the list on which we perform some operation.
filter
: It is an optional parameter that includes or excludes elements from the enumerable based on requirement.
do: expression
: It is the operation to be performed on each element of the enumerable
.
The above syntax is explained through the illustration below:
The code example of comprehension is given below:
list_1 = [1, 2, 3, 4, 5, 6, 7, 8]IO.puts("Input list:")IO.inspect(list_1)list_2 = for n <- list_1, do: n / 2IO.puts("Resultant list:")IO.inspect(list_2)
Explanation:
Line 1: Define a list.
Lines 2–3: Print the input list values.
Line 4: Perform list comprehension on list_1
by dividing each element by 2.
Lines 5–6: Print the resultant list values.
The code example of comprehension is given below with a filter, where we filter out even values and perform a square on those even values:
list_1 = [1, 2, 3, 4, 5, 6, 7, 8]IO.puts("Input list:")IO.inspect(list_1)list_2 = for n <- list_1, rem(n, 2) == 0, do: n * nIO.puts("Resultant list:")IO.inspect(list_2)
Explanation:
Line 1: Define a list.
Lines 2–3: Print the input list values.
Line 4: Perform list comprehension on even elements of list_1
by squaring its values.
Lines 5–6: Print the resultant list values.
The code example of comprehension is given below with two filters based on even and odd values, where the even values are squared and the odd numbers are halved:
list_1 = [1, 2, 3, 4, 5, 6, 7, 8]IO.puts("Input list:")IO.inspect(list_1)list_2 = for n <- list_1, do: if rem(n, 2) == 0, do: n * n, else: n / 2.0IO.puts("Resultant list:")IO.inspect(list_2)
Explanation:
Line 1: Define a list.
Lines 2–3: Print the input list values.
Line 4: Perform list comprehension on list_1
.
Lines 5–6: Print the resultant list values.
into
operationThe following code example shows how the into
operation is used with for
operator in Elixir. The into
operator returns the result of list comprehensions as a different data structure. The code snippets below convert the list of integers to characters using into
operator.
defmodule SomeMod dodef string dofor c <- [65, 110, 115, 119, 101, 114], into: "", do: <<c>>endendIO.inspect SomeMod.string
Explanation:
Line 1: Define a module named SomeMod
.
Line 2: Define a function string
inside a module.
Line 3: Perform list comprehension by converting a list of integers to a string.
Line 6: Print the result.
In Elixir, list comprehensions provide a concise and powerful way to manipulate lists. They provide a clean and readable syntax to perform various operations such as filtering, mapping, combining lists, etc.
Haven’t found what you were looking for? Contact Us
Free Resources