...

/

Functions as Data

Functions as Data

Let’s learn about using functions as a datatype in Elixir.

Remember that since Elixir is a functional language, all functions are data. Sometimes using functions can offer tremendous performance wins.

Drawing a square without a function

Here is one way to store the drawing instructions for a square:

Executable

Press + to interact
square = [ {:line, {5, 0}, {15, 0}},
{:line, {15, 0}, {15, 10}},
{:line, {15, 10}, {5, 10}},
{:line, {5, 10}, {5, 0}}]

Output

iex(1)> square = [ {:line, {5, 0}, {15, 0}},
...(1)>                    {:line, {15, 0}, {15, 10}},
...(1)>                    {:line, {15, 10},
...