Control Flow in Elixir by Anna Neyzberg discusses pattern matching and control flow in Elixir. Some key points include:
1) Pattern matching is used for control flow in Elixir functions through matching the shape and values of terms.
2) Variables in Elixir are bound to values through pattern matching rather than assignment.
3) Functions can have multiple definitions through multi-clause functions to handle different patterns and cases.
7. • An attempt to read the file my_app.config takes place.
• If the attempt succeeds the file content are extracted to the
variable contents.
• If the attempt fails an error is raised. This is because
File.read returns a tuple in the form of - {:error, reason}
11. In Conclusion…..
• In elixir the “=“ sign does not mean set
this variable to something else
• New initialized variables are bound to the
term on the right hand side
•Multi-clause Functions are functions that have
multiple definitions and the same arity.
let’s take a look at what assignment means in elixir -
let’s take a look at this code snippet - really straight forward -
what we think is happening vs what is actually happening.
when you initialize a variable, the variable is bound to that value - an the = sign is actually called the match operator
This might look like an assignment but in reality something more complex is going on
at runtime, the right side of the operator is matched to the left side…You can see this in the third statement. where the variable is on the right side.
Variables in elixir cannot change value. However however they can be rebound to a new value.
In this example, you can set x to 2 and then set it back to 3 later.
It’s important to note that rebinding doesn’t mutate the existing memory location, but reserves new memory and assigns a symbolic name to point to the new location (data immutable - once a location in memory is occupied with data, it can’t be modified till memory is released).
in the last line -notice the pin operator - this is useful when you are matching against the contents of a variable rather than matching and binding the value of a variable to the term on the right side.
Tuples, as many of you know are something like untyped structures or records and most often they are used to group a fixed number of elements
This current example assumes that the right hand term is a tuple of 2 elements
“Atoms are literal named constants. They’re similar to symbols in Ruby or enumerations in C/C++”
Excerpt From: Saša Jurić. “Elixir in Action.” iBooks.
here we can bind result and value to their respective values on the right hand side.
What if we only want to assign a value to our variable ‘value’ if we know that the result has returned ok? - If the right side doesn’t correspond to the pattern an error is raised
Note here that we are also using a constant for matching. so the patterns always expects the first element of the tuple to match :ok
In this case while this is a simple example, you can see that this function will execute a different action depending on the arguments that were passed in.
Again we are using constants. It is common in elixir for functions to either return {:ok, result}, or { :error, reason}
- let’s take a look at one more example really
Lets image your file system relies on a configuration file and always expects it to be available
You can read the contents of the file with the File.read/1 function
in this single line of code - 3 things are happening
- This is where pattern matching is seen to be really powerful in elixir
You can declare functions that will only be executed if arguments match the patterns
Elixir allows you to overload a function my specifying multiple clauses
a clause is a functioned defined by the def construct
If you provide multiple definitions of the same function with the same arity, its said the function has multiple clauses.
in Erlang lists are used to manage dynamic, variable sized collections of data
lists may look like arrays but they are singly linked lists
Looping works very different in elixir than it does in most other languages. Constructs like while and do while aren’t provided
and since data is immutable, they wouldn’t really work. - so the principle tool for looping in elixir is recursion
Here we creates a function that takes a list as an argument.
elixir allows you to reference the first element in the list as the head, and the rest of the list as the tail.
We then call our square function on the tail. This will keep calling the function on the remains times in the list, until it matches an empty list, in which case it will return an empty list.