All posts

Understanding Elixir Range

This and the other “Deck” posts are a repurposing of flashcard study decks to Q&A blog posts. Google was not showing love to this content as a set of flashcards and I didn’t want to delete them entirely, I hope you find it useful.

What is an Elixir Range?

It is way to work with a sequence of integers more effectively without having to explicitly define each integer in the sequence. It also helps with more efficiently checking if a values falls within a sequence of integers. Instead of having to write [1,2,3,4,5, ...] you can instead write 1..5.

Can a Range be either ascending or descending?

Yes the following two examples are both valid ranges:

ASC

2..20

DESC

20..2

How can pattern matching be used to set a variables to contain the values of the start and end integers of a range?

Using left hand assignment where the range variables are declared left of the equals sign (match operator) like so:

score_range = 2..20 
start..finish = score_range 
=> start
2
=> finish 20
20

Do Enum module functions work with Ranges?

Yes. Enum module functionality is implemented for Ranges. Two example functions:

Enum.member?/2
Enum.count/1

For your continued enjoyment: