Tensor Programming avatar

Introduction to Elixir - Guards and Conditional Macros - Part Four

tensor

Published: 22 May 2019 › Updated: 22 May 2019Introduction to Elixir - Guards and Conditional Macros - Part Four

Introduction to Elixir - Guards and Conditional Macros - Part Four

elixir.jpg

Repository: https://github.com/elixir-lang/elixir

What Will I Learn?

  • You will learn about Guards in Elixir
  • You will learn about Conditional Macros in Elixir
  • You will learn about the case and with Macros
  • You will learn about unless, if and cond
  • You will learn how to do Conditional Pattern matching in Elixir
  • You will learn how to use Function Clauses and Guards to build conditionals

Requirements

System Requirements:
  • Elixir v1.8 requires Erlang 20.0 or later
OS Support for Elixir and Phoenix:
  • Mac OSx
  • Unix and Linux
  • Windows
  • Raspberry Pi
  • Docker
Required Knowledge
  • Some basic Programming Knowledge
  • An Elixir installation
  • VsCode or any other Text Editor
Resources for Elixir and Phoenix:
Sources:

Difficulty

  • Beginner

Description

In this Elixir Video Tutorial, we take a look at some of the basic methods of branching code execution in Elixir. This includes working with various conditional macros and operators as well as function guards and clauses. Because Elixir relies heavily on Pattern matching to determine control flow, often it is useful to use these macros and guards to be able to define the different branches that a program should follow given a piece of data.

Using Functional Guards in Elixir

Elixir allows us the ability to overload functions by defining multiple clauses. Along with this, we have the ability to use a basic pattern matching expression called a guard to constrict the arguments passed into a function's interface. Guards work well with Elixir's dynamic type system by providing a tool that can differentiate a function clause by conditionals and other logical expressions. Guards are specified after the argument list on a function clause using the when macro.

guards.png

Above, we've defined a single function with three clauses called test. This test function allows us to determine if a number is zero, positive or negative and its able to do this by using pattern matching and guards. The guards constrict the input into the clauses by checking to see if the argument is greater than or less than zero. The middle clause on the other hand always matches if the argument is zero. A side effect of this type of guard is that the argument must be a type that can work inside of the guard. If a user passes in a list or a map, the guards will throw back an error instead of attempting to execute the function body.

Using Macros to Define Conditional Branches and Statements

Multiclause functions with guards often are able to create branching logic in our programs but they are not always the most appropriate solution. Sometimes its much simpler to just use a classical branching construct inside of the body of a function. This is where the case, if, cond, with and unless macros become useful. The if macro is the simplest of these macros; it allows us to define one or two branching paths in a code based on a condition. The unless macro is the inverse of if. The cond macro allows us to define as many branches as we want in our code.

cond.png

The cond macro used in the above image gives us the ability to check for two conditionals. First we check if a is greater than or equal to b and if it is we return a. Next, we check if true is true; this line acts as a catch all because the Boolean true will always be true.

case.png

The same function can be rewritten using the case macro. In this example we can check to see if a case is equal to some value; since this example uses Booleans, it can either be true or false. However, if we wanted to pattern match on something more complicated, we could have infinite branches in this case statement.

with.png

The case macro works like a bunch of chained if-else macros and the with macro works like a bunch of chained case macros. The with macro first evaluates the term on the right and if that term matches on the pattern on the left, it moves down to the next line and repeats this process. By doing this, we can ensure that multiple cases must be correct before executing a bit of logic.

Full Github Source Code can be found here: https://github.com/tensor-programming/intro-to-elixir/tree/tensor-programming-part-4

Video Tutorial

Curriculum

Intro to Elixir
Related Phoenix Videos
GraphQL Series

Leave Introduction to Elixir - Guards and Conditional Macros - Part Four to:

Written by

Tensor programming is aimed at providing programming education through both youtube and blogs.

Read more #utopian-io posts


Best Posts From Tensor Programming

We have not curated any of tensor's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Tensor Programming