October 2014
Intermediate to advanced
280 pages
7h 20m
English
The Elixir test framework, ExUnit, uses some clever code-quoting tricks. For example, if you assert
| | assert 5 < 4 |
You’ll get the error “expected 5 to be less than 4.”
The Elixir source code is on GitHub (at https://github.com/elixir-lang/elixir). The implementation of this is in the file elixir/lib/ex_unit/lib/ex_unit/assertions.ex. Spend some time reading this file, and work out how it implements this trick.
(Hard) Once you’ve done that, see if you can use the same technique to implement a function that takes an arbitrary arithmetic expression and returns a natural language version.
| | explain do: 2 + 3 * 4 |
| | #=> multiply 3 and 4, then add 2 |
Read now
Unlock full access