When To Use Elixir Lists — Tentamen Software Testing Blog

Image for post
Image for post

TL;DR

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.

Syntax

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:

Image for post
Image for post

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:

Image for post
Image for post
Image for post
Image for post

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.

Remember

Originally published at https://blog.tentamen.eu on August 3, 2020.

Written by

Founder of Tentamen, software testing agency.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store