O'Reilly logo

Elixir Cookbook by Paulo A Pereira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using guard clauses and pattern matching in function definitions

In the Using pattern matching recipe in Chapter 2, Data Types and Structures, we saw how it was possible to use the = operator to match values on the right-hand side with values on the left-hand side. In this recipe, we will see pattern matching in action without using the = operator. We will use pattern matching implicitly in function definitions with the same name and arity, and Elixir will use it to determine which function version to execute.

Sometimes, pattern matching is not enough to determine which function to execute, so we will also be using guard clauses in our function definitions. Guard clauses allow us to only execute a given function if some condition regarding its ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required