February 2018
Beginner
200 pages
4h 37m
English
Elixir’s pattern matching shapes everything you program. It’s useful for assigning variables, unpacking values, and making decisions such as which function to invoke. The basis of pattern matching is that it tries to make two things match, and it does something when it fails to do so.
You’ll start to learn about pattern matching with the = operator. This operator raises a MatchError when it fails to match two things, stopping the program execution. Otherwise, when both sides of the operator have a match, the program keeps running. Let’s see how it works in practice, step by step. Open an IEx session and type the following pattern-matching expression:
| | iex> 1 = 1 |
| | 1 |
| | iex> 2 = 1 |
| | ** (MatchError) no match of right ... |
Read now
Unlock full access