Introduction to Elixir - Guards and Conditional Macros - Part Four
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
andwith
Macros - You will learn about
unless
,if
andcond
- 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:
- 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
Sources:
- Elixir Image: https://elixir-lang.org
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.
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.
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
.
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.
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
- 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
Thank you @tensor for your contribution to the video category.
This Part 4 on "Guards and Conditional Macros" is a good addition tutorial series on Elixir.
Again your presentation is motivating and the concepts outlines are clear and easy to follow.
Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post, click here.
Need help? Chat with us on Discord.
[utopian-moderator]
Thank you for your review, @rosatravels! Keep up the good work!
Congratulations @tensor! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :
You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
To support your work, I also upvoted your post!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!
Hi, @tensor!
You just got a 7.2% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.
Hey, @tensor!
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!
Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!