When To Use Elixir Range, Keyword Lists And MapSet — Tentamen Software Testing Blog
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.
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 list is a
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 is an enumerable set of unique values of any type.
- The Range is a consecutive set of Integer numbers
- The Range is a map with a small memory footprint
- Range is enumerable
- Keyword Lists is a list of special tuples
- There is a Keyword module
- Keywords are useful as optional function parameters
- MapSet is a set of unique values.
Originally published at https://blog.tentamen.eu on October 2, 2020.