Search⌘ K

Functions as Data

Explore Elixir's concept of functions as data within its datatype system. Understand the benefits of sending functions to data rather than transferring large datasets across processes, improving efficiency in distributed systems.

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

C++
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}, {5, 10}},
...(1)>                    {:line, {5, 10}, {5, 0}}]
[
  {:line, {5, 0}, {15, 0}},
  {:line,
...