O'Reilly logo

Programming Elixir by Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The List Module in Action

The List module provides a set of functions that operate on lists.

 
#
 
# Concatenate lists
 
#​​​
 
iex>​ [1,2,3] ++ [4,5,6]
 
[1, 2, 3, 4, 5, 6]
 
#
 
# Flatten
 
#​​​
 
iex>​ List.flatten([[[1], 2], [[[3]]]])
 
[1, 2, 3]
 
#
 
# Folding (like reduce, but can choose direction)
 
#​​​
 
iex>​ List.foldl([1,2,3], ​""​, ​fn​ value, acc -> ​"#{value}(#{acc})"​ ​end​)
 
"3(2(1()))"​​​
 
iex>​ List.foldr([1,2,3], ​""​, ​fn​ value, acc -> ​"#{value}(#{acc})"​ ​end​)
 
"1(2(3()))"
 
#
 
# Merging lists and splitting them apart
 
#​​​
 
iex>​ l = List.zip([[1,2,3], [:a,:b,:c], [​"cat"​, ​"dog"​]])
 
[{1, :a, ​"cat"​}, {2, :b, ​"dog"​}]​​
 
iex>​ List.unzip(l)
 
[[1, 2], [:a, :b], [​"cat"​, ​"dog"​]]
 
#
 
# Accessing ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required