In the previous post, we started to organize Elixir’s primary data type in Tuples. Today we explain what Elixir Lists are and when to use them. This post is part of the functional language series, and it is based on the remarkable book Elixir In Action by Sasa Juric.
The ListList is a collection of elements. Observe the syntax in the above picture. Items could be of different types. From data structure theory, it is a Linked List:
Each item only knows about the next item. The List has a head (beginning) and tail (last item).
Elixir modules List and Enum are handy for List operations:
When To Use
The complexity of functions that operate on Lists is O(n). Complexity grows in linear proportion with the number of elements. For example, to find the first element is faster than finding the last item because we always go in List from first to the next item. Because of that, we always add new items at the beginning of a List.
This is a special operator that concatenates two lists. First List Tail becomes Second Tail Head.
- List Syntax
- Which Elixir Modules are useful for List manipulation
- What is O(n) notation?
- Add at the beginning
- Traverse operation
- Always test on the ListList of at least
Originally published at https://blog.tentamen.eu on August 3, 2020.