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

Your Turn

  • Exercise: MacrosAndCodeEvaluation-3

    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

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