Your Turn
- Exercise: ControlFlow-1
Rewrite the FizzBuzz example using case.
- Exercise: ControlFlow-2
We now have three different implementations of FizzBuzz. One uses cond, one uses case, and one uses separate functions with guard clauses.
Take a minute to look at all three. Which do you feel best expresses the problem. Which will be easiest to maintain?
The case style and the implementation using guard clauses are different from control structures in most other languages. If you feel that one of these was the best implementation, can you think of ways to remind yourself to investigate these options as you write Elixir code in the future?
- Exercise: ControlFlow-3
Many built-in functions have two forms. The xxx form returns the tuple {:ok, data} and ...
Get Programming Elixir now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.