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

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

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

Elixir MapSet

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

Remember

  • 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.

--

--

--

Founder of Tentamen, software testing agency.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Basic Django PostgreSQL Database Queries

Understanding Concurrency vs Parallelism in a simple way

A rules-as-code case study from New Zealand

A picture of a red sign on a stake driven into the ground that says “Please stay on the path”.

Apache ShardingSphere 5.0.0-beta Release and Features

NoSQL Databases

Solana Faction Cards NFT Staking Live on Mainnet!

Which is the best domain and hosting provider?

Connecting to mongodb atlas/external service from Google cloud functions via static IP address

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
Karlo Smid

Karlo Smid

Founder of Tentamen, software testing agency.

More from Medium

Elixir’s best practices: Achieve secure authentication

Ranked data with Ecto window Queries

Comparing dates in Elixir