Elixir pattern matching reaction hype
| elixirAre you kiddings me? This is no yoke.
You can pattern match a function call!?
Consider this:
This line 1) Reads my_app.config
2) if the return value pattern
matches successfully to :ok
, then the file contents get assigned to
contents 3) if the match fails, i.e., the operation is not :ok
then
an exception is raised.
Writing this in Ruby would maybe something like this:
But that ain’t it folks, you can also pattern match arguments in functions… head explode.
Greeting.hello/1
expects to be called with a tuple containing an
atom/name. Each definition of the same arity function is a clause,
together constituting the aptly named multiclause function.
But what if no clause matches? No matter, because a variable pattern ALWAYS matches. That is hella dope. Guard clauses can be created by simply matching against a variable. However, the order in which the clauses are defined matters, since Elixir will try matching from top to bottom. Hence guard clauses should be at the bottom.