The Elixir Enum Module
Elixir Enum Module
To help myself in learning more Elixir, I'm going to continue these Elixir guide/tutorial posts where I try to explain a different piece of Elixir in each post. Today's post will be covering the Enum module and it's usage.
The Enum Module provides a set of algorithms that enumerate over collections according to the Enumerable protocol:
iex> Enum.map([1, 2, 3], fn(x) -> x * 2 end)
[2,4,6]
Some particular types, like dictionaries, yield a specific format on enumeration. For dicts, the argument is always a {key, value} tuple:
iex> dict = %{a: 1, b: 2}
iex> Enum.map(dict, fn {k, v} -> {k, v * 2} end)
[a: 2, b: 4]
Below are some other functions available in the Enum Module along with a link to it's definition in the Enum Module documentation.
at/2
filter/2
reduce/3
into/2
take/2
The Capture Operator
Let’s first talk about capturing function. Capture means "&" can turn a function into an anonymous function which can be passed as arguments to other function or be bound to a variable.
& can capture two types of functions, a function with given name and arity from a module.
The notation is: &(module_name.function_name/arity) ex:
speak = &(IO.puts/1)
speak.("hello") # hello
We capture puts function from IO module and bind it with a local name speak.
The capture operator can be a little difficult to wrap your head around, so here are some examples and a helpful image to help grasp this concept:
# Multiple each number by itself
Enum.map [1, 2, 3], fn(num) ->
num * num
end
# Shortened with capture operator:
# Parentheses are required around the capture in this
# case to make it clear where the capture starts and ends.
Enum.map([1, 2, 3], &(&1 * &1))
When you are capturing a named function, you don’t need the parentheses:
# Remove \n chars from the end of each word
Enum.map ["hello\n", "there\n"], fn(word) ->
String.replace(word, "\n", "")
end
# Shortened with capture operator:
Enum.map(["hello\n", "there\n"],
&String.replace(&1, "\n", ""))
Read the documentation on the Capture operator for more details.
Stream
Stream is a lazy version of the Enumerable module. Note that the functions in the Enum module are eager: they always start the enumeration of the given collection. The Stream module allows lazy enumeration of collections and provides infinite streams. It implements most Enum functions, but instead of returning a modified list, it returns a struct like this:
%Stream{
enum: [...], # Enumerable to iterate through
funs: [...] # Anonymous functions to run
}
Since the majority of the functions in Enum enumerate the whole collection and return a list as result, infinite streams need to be carefully used with such functions, as they can potentially run forever. For example:
Enum.each Stream.cycle([1,2,3]), &IO.puts(&1)
Streams are lazy, and only iterate over the list once:
# Iterates over the list twice
list
|> Enum.filter(&is_number/1)
|> Enum.filter(&(&1 * 2 == 4))
# Iterates over the list once
list
|> Stream.filter(&is_number/1)
|> Stream.filter(&(&1 * 2 == 4))
|> Enum.into([])
Use Enum.into/2 or Stream.run/1 to make a stream do work.
list
|> Stream.filter(&is_number/1)
|> Stream.filter(&(&1 * 2 == 4))
|> Enum.into([])
[1, 2, 3]
|> Stream.each(&IO.puts/1)
|> Stream.run
Checkout some more Stream building functions in the documentation:
cycle/1
iterate/2
resource/3
Resources
Enum Documentation
Capture Operator Documentation
Stream Documentation
Leave The Elixir Enum Module to:
Read more #stemsocial posts
Best Posts From nolyoi
We have not curated any of nolyoi's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From nolyoi
- I'm back! And what I've been up to (getting married, working on a skincare app).
- ## I am accumulating! With prices headed down, it's always the best ti ...
- Tezos weekly chart looks amazing, and other Tezos tokens! Don't sleep on them!
- End of season rewards + some quest rewards
- ShapeShift FOX airdrop $150-$7000 worth of FOX!
- How I've made hundreds in Bitcoin doing almost nothing with these apps
- 2 months of DeFi on Tezos
- Splinterlands haul: A LEGENDARY Robo Dragon Knight!
- Plenty - The new DeFi yield farm on Tezos (XTZ)!
- Earning Bitcoin (BTC) from purchases you already make!
- Some photos from my trip to Florida
- VOTE $BANANO! $BAN https://twitter.com/KuCoinItalia/status/1391850381 ...
- My experience building with Flutter and finally coming close to app completion.
- My Top 5 Altcoin HODLs
- Finally got around to farming some CUB!
- File structure visualization of a Flutter mobile app I'm working on. ...
- Why I'm buying up Bitcoin Cash and Monero.
- Now if only $OXEN would pick itself up and get in the top 200. Another ...
- Been stacking up KCS for awhile now because you get almost 13% APR in ...
- How I'm funding my open-source project, and supporting open source software too! If you have any change to spare, doate and get HIVE on tio!