When To Use Elixir Range, Keyword Lists And MapSet — Tentamen Software Testing Blog

Image for post
Image for post
Elixir Range data type

TL;DR

In the previous post, we introduced the last two Elixir Basic Types, Reference, And Pid. Today we present higher-level types: Range, Keyword Lists, and MapSet. This post is part of the functional language series, and it is based on the remarkable book Elixir In Action by Sasa Juric.

Basic Vs. Higher Level Data Types

Elixir Basic data types are inherited from Erlang. The reason is simple, we need those to run Elixir programs on BEAM virtual machine. But to reduce the amount of boilerplate code, we need some higher-level data abstractions.

Range

The Range represents a consecutive set of integer numbers. It is enumerable (implements enumerable behavior), and we could use all Enum module functions with Range. The operator in helps us to determine if the number is part of a range.

Keyword Lists

Image for post
Image for post
Elixir Keyword List

Keyword list is a list of tuples, where the first tuple element is an atom. In Elixir it is used as an optional list of function parameters. Many Elixir libraries stick to that convention. We have two options to define a Keyword list and two options to use it as a function parameter. Remember that searching a list is O(n). Keyword lists are used over Maps as function parameters because they preserve element order, and one key could have multiply values.

MapSet

Image for post
Image for post
Elixir MapSet

MapSet is an enumerable set of unique values of any type.

Remember

Originally published at https://blog.tentamen.eu on October 2, 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