All posts

Elixir Atoms and Integers explained through q&a

This post works best when paired with the Elixir docs for a general overview of Atoms and Integers.

What is an Elixir Atom?

It is an Elixir data type thats value is its own name.

What is an Elixir Atom used for?

They are often used to indicate the result of an operation either as the first value of a tuple or as a standalone response. :ok is often used to indicate a successful operation and :error is used if something goes wrong.

{:ok, result} = some_function()

:ok = some_other_function()

What is special about the values true, false and nil as they relate to atoms?

Each of those data types are atoms. Elixir allows you to skip typing the colon (:) for these atoms. Ex:

true == :true
=> true

nil == :nil
=> true

false == :false
=> true

Which data type represents whole numbers in Elixir Integer or Float?

Integer. Float is used for fractional numbers.

The Kernel and Integer module each define integer functions. What integer functions does the Kernel define?

abs/1, div/2, min/2, max/2, rem/2. These are all arithmetic based functions.

What functions does the Integer module define?

is_env and is_odd. These are most often used as Guards in Elixir.

Integer.is_even(12)
=> true

Integer.is_odd(12)
=> false

More Elixir Decks:

  1. Tuples
  2. List

Postscript:

I built two products to help fund continued efforts for this website, it would mean a lot if you would check them out:
1. 2500+ Words for the Undermotivated Software Engineer - A short essay I wrote on the titled topic ($3).
2. Build 3 Chrome Extensions in Under 2 Hours - A course I built to teach chrome extension development ($9-20).