O'Reilly logo

Programming Elixir by Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Digging Ridiculously Deep

Here’s the internal representation of a simple expression:

 
iex(1)>​ ​quote​ ​do​: 1 + 2
 
{:+, [context: Elixir, ​import​: Kernel], [1, 2]}

It’s just a three-element tuple. In this particular case, the first element is the function (or macro), the second is housekeeping metadata, and the third is the arguments.

We know we can evaluate this code fragment using eval_quoted, and we can save typing by leaving off the metadata:

 
iex>​ Code.eval_quoted {:+, [], [1,2]}
 
{3,[]}

And now we can start to see the promise (and danger) of a homoiconic language. Because code is just tuples and because we can manipulate those tuples, we rewrite the definitions of existing functions. We can create new code on the fly. And we can do ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required