Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Upon completing this lesson, a student should be able to answer the following questions.
- What is an atom and how might we use them in our programs?
- Why might we use atoms for the sake of comparing constant values rather than strings?
Atoms are named constants. In other words, their name is their value.
Atoms are often used to represent common constants in your program. For example, they often represent the :success or :error of a program.
:success
:error
They might also be used to represent the state of some action. For example:
:completed
:in_progress
:starting
You might wonder why we have both strings and atoms because they seem to do the same thing. One primary reason is performance. Atoms are stored in an atom table and can be referenced by a key. This makes it way faster to check if two atoms are equal.
What is an atom table? Imagine it like an excel spreadsheet. Each value has a key. For the computer, comparing these keys in the table is much more performance that checking if every character in a string is equal.
It's kind of like checking if 1 == 2
instead of checking if"HeLloWorld" == "HeLloWorLd"
.
Atoms are defined using a colon :
and a series of letters, digits, and certain valid symbols.
There are specific rules for naming atoms, but fortunately you don't need to memorize them! Instead, pay attention to the colors in your code. If you define an atom incorrectly, you'll notice that it's no longer blue.
Here are some valid atoms:
:hello
:my_atom1
Here are some invalid atoms, notice they are mostly white:
:$this_is_invalid
:!this_is_invalid
:2
:@
:$
:?
:invalid # spaces are not valid
Sometimes languages establish conventions. Conventions are common patterns of doing things which the community agrees upon. For example, In Elixir it's conventional for atoms to have lowercase names separated by underscores.
While the following will work:
:MY_ATOM!
It's usually unconventional to name an atom with capital letters except in specific circumstances.
You can use quotes with atoms to avoid name rules.
:"$Now we don't have to follow any rules. This is a valid atom (though unconventional)"
nil
, false
, and true
are all actually atoms.
We can use ===
to verify that :nil
and nil
are equivalent.
nil === :nil
The same goes for true
, and false
. They are
all atoms but omit the colon :
.
true === :true
false === :false
In the Elixir cell below, use ===
to check if nil
is equal to :nil
.
Check if true
is equal to :true
.
Check if false
is equal to :false
.
Consider the following resource(s) to deepen your understanding of the topic.
DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.
Run git status
to ensure there are no undesirable changes.
Then run the following in your command line from the curriculum
folder to commit your progress.
$ git add .
$ git commit -m "finish Atoms reading"
$ git push
We're proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.
We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.