Before we get with
it…A quick pattern matching review
In Elixir, the =
operator is actually called the match operator. While, we can usually see it as assignment, in Elixirland, it’s much easier if we can see it as pattern matching [1].
Another quick example (taken from Elixir docs) [2]:
And in the context of program (example_1.exs
):
Run $ iex example_1.exs
:
Quick sidebar note from the Elixir docs:
In addition to the Elixir file extension .ex
, Elixir also supports .exs
files for scripting. Elixir treats both files exactly the same way, the only difference is in intention. .ex
files are meant to be compiled while .exs files are used for scripting. When executed, both extensions compile and load their modules into memory, although only .ex
files write their bytecode to disk in the format of .beam
files [3].
Almost with
it…A quick review of pipes
I’m hoping you have some experience with Unix pipes as these are similar. From the docs, “The |>
symbol…is the pipe operator: it takes the output from the expression on its left side and passes it as the first argument to the function call on its right side.” [4] They allow the ability to Stream from one operation to the next.
An example:
What is with
?
The with
operator is a control flow mechanism that allows a sequence of operations to return a value or the ability to break out of that sequence.
Let’s look at some examples from Jose:
|
|
If they all match in the sequence, we’re done.
Here, because they don’t, the sequence is aborted and an error is returned.