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 while and do...while loops all of the looping structures and patterns in Elixir make use of recursion under the hood.
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.
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.
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 for comprehension.
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