What Will I Learn?
- You will learn about Looping Constructs in Elixir
- You will learn about Recusion
- You will learn about Tail Call Recursion and how to use it
- You will learn how to use Comprehensions in Elixir
- You will learn about Enumerations and the Enum Module
- Elixir v1.8 requires Erlang 20.0 or later
OS Support for Elixir and Phoenix:
- Mac OSx
- Unix and Linux
- Raspberry Pi
- Some basic Programming Knowledge
- An Elixir installation
- VsCode or any other Text Editor
Resources for Elixir and Phoenix:
- Elixir Website: https://elixir-lang.org
- Elixir Installation Instructions: https://elixir-lang.org/install.html
- Awesome Elixir Github: https://github.com/h4cc/awesome-elixir
- Phoenix Website: https://phoenixframework.org/
- Phoenix Installation Instructions: https://hexdocs.pm/phoenix/installation.html
- Elixir Documentation: https://elixir-lang.org/docs.html
- Phoenix Documentation: https://hexdocs.pm/phoenix/Phoenix.html
- LiveView Github Repository: https://github.com/phoenixframework/phoenix_live_view
- Elixir Image: https://elixir-lang.org
In this Video Tutorial, we take a look at the basic looping structures that are used in Elixir. Looping is a very important concept in programming and because Elixir is a Functional Programming language it makes heavy use of recursion, macros and functions. Rather than having
do...while loops all of the looping structures and patterns in Elixir make use of recursion under the hood.
Defining Functions in Terms of Themselves with Recusion
Elixir as a language makes heavy use of Collections of data. To be able to preform operations over these collections, we need some way of looping and iterating through the data structures. If, for instance, we wanted to take a list of numbers and sum them all together then we would need a method to select each number individually to add it to the overall accumulator. And because Data is immutable by default, we can't just use a basic for
loop to move through the data. This is where the idea of Recursion comes into play.
In the above image, we have a recursive function called
sum which takes a list of numbers and sums them together until it hits the end of the list. This function uses two clauses to define the proper behavior that it needs. The top clause defines how the function will end the loop by way of pattern matching on an empty list. The second clause pulls the head off of the list and then adds it to a recursive call to the
sum function on the Tail of the list. You can see how the function unwraps the data one item at a time and then adds it all together.
Making Use of Tail Call Recursion/Optimization
When a function is called in most programming languages, the function operation results in a stack push. This means that a typical Recursive function will ultimately run out of Stack space if it calls to itself too often. In Elixir we can solve this problem by making use of a concept called Tail-call Recursion. Tail-call Recursion relies on a the idea that the last line of the function will be the recursive call. By structuring the function in this way, the compiler can use jump statements instead of preforming a stack push.
In the image above, we have two different functions called
getNumber. The top function
getNumber/1contains two clauses; one calls down to the second function
getNumber/3 and the other throws an error if
n is negative. The Tail call happens in the bottom clause of the
getNumber/3 function. Notice how the final line in the
getNumber/3 clause is a recursive call to
getNumber/3. Not only is this function faster than a non-tail call function but it also can run almost indefinitely.
Using a For Loop Comprehension to Iterate through Data
Elixir features many different macros and functions that allow the user to easily iterate over a collection of data without having to explicitly write a recursive function. All of these structures use Recursion under the hood but they hide the recursion from the user. A good example of this is the
In this image we have a handful of comprehensions. These comprehensions are very useful because they not only allow us to iterate through collections of data but they also give us an ability to filter the data and cast it into another collection type. The first comprehension just takes the data and multiples it by itself each element at a time. It then returns a list of these new elements. The second comprehension takes two lists and returns a list of tuples based on the pattern at the end of the
do: block. The final two comprehensions use the optional
into keyword to transfer the data into a map structure. Both of these return a map where the keys are a tuple structure and the data is a single value. The final one also features a guard which filters the data.
Full Github Source Code can be found here: https://github.com/tensor-programming/intro-to-elixir/tree/tensor-programming-part-5
Intro to Elixir
- Introduction to Elixir - A Background and the Primitive Types - Part One
- Introduction to Elixir - Functions, Built-in and Complex Types - Part Two
- Introduction to Elixir - Pattern Matching and Control Flow - Part Three
- Introduction to Elixir - Guards and Conditional Macros - Part Four