What are enums in Elixir?
The Elixir language supports enumerables. An enumerable object is an iterative object implementing the Enumerable protocol, such as a list, tuple, map, etc.
Elixir provides a handy module Enum to work with enumerated objects. This module allows a collection of functions to enumerate values in an enumerable data structure and work with them by iterating over the available items.
Typically, enumerable functions accept the following as parameters:
An Enumerable object.
A custom function to apply to the enumerated values.
Let's explore some of the most common and showcasing Enum functions (access the following link for a complete list of functions associated with the Enum module).
The map function
The map is used to apply a custom function to each item of a collection and produce a list of processed items.
Syntax
map(enumerable, fun)
This function takes two arguments:
enumerable:This is a collection of elements to be processed.fun: This is a function to apply to each element of the collection.
Code example
Let's look at the code below:
output = Enum.map([1, 2, 3, 4], fn(item) -> item*10 end)IO.inspect(output)
Let's go through the code snippet above:
Line 1: We apply the
mapfunction on a list of integer values and multiply each item value by. Line 2: We display the produced result.
The reduce function
The reduce is used to reduce the entire enumerable into a single value. This function makes use of a special variable called the accumulator. Between iterations, the accumulator holds the intermediate state of the reduction.
Syntax
reduce(enumerable, acc, fun)
This function takes three arguments:
enumerable: This is a collection of elements to be processed.acc: The accumulatoraccis optional, and it is passed as the first argument to the functionfun. When no accumulator is provided, the first element value is considered an accumulator.fun: This is a function to apply to each element of the collection while considering the accumulator.
Code example
Let's look at the code below:
output = Enum.reduce([1, 2, 3, 4], 10, fn(x, accum) -> x + accum end)IO.inspect(output)
Let's go through the code snippet above:
Line 1: We apply the
reducefunction on a list of integer values while considering the valueas an accumulator, to sum up the list of given items and the accumulator value provided. Line 2: We display the produced result.
The join function
The join is used to join the elements of a given enumerable into a string while utilizing a joiner as a separator.
Syntax
join(enumerable, joiner \\ "")
This function takes two arguments:
enumerable: This is a collection of elements to be processed.joiner: Thejoineris optional and defaults to an empty string.
An error occurs if the enumerable elements are not convertible to string.
Code example
Let's look at the code below:
output = Enum.join([1, 2, 3 , 4], " - ")IO.inspect(output)
Let's go through the code snippet above:
Line 1: We apply the
joinfunction on a list of integer values to concatenate them while using the" - "as a joiner.Line 2: We display the produced result.
The all function
The all function is used to iterate over all the elements of a given enumerable, invoke a function on each of these elements and return true if the called function evaluates to True for all the elements. Otherwise, False is returned.
Syntax
all?(enumerable, fun)
This function takes two arguments:
enumerable: This is a collection of elements to be evaluated.fun: This is a function to apply to the collections' elements.
Code example
Let's look at the code below:
output = Enum.all?([1, 2, 3, 4], fn x -> x > 0 end)IO.inspect(output)
Let's go through the code snippet above:
Line 1: We apply the
allfunction on a list of integer values to check whether all the provided values are greater than. Line 2: We display the produced result.
The slice function
The slice is used to slice an enumerable into a subset list.
Syntax
slice(enumerable, start_index, amount)
This function returns as many elements as possible until reaching the amount specified while starting with the element located after the start_index.
It takes the following arguments:
enumerable: This is a collection of elements to take a subset of.start_index: This is a start position (zero-based) from the beginning of the enumerable.amount: This is the number of elements to extract if available.
Code example
Let's look at the code below:
output = Enum.slice(1..10, 5, 3)IO.inspect(output)
Let's go through the code snippet above:
Line 1: We apply the
slicefunction on a list of integer values ranging fromto then slice it into a sublist of elements starting from the sixth element. Line 2: We display the produced result.
Free Resources