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 pattern matching

In some of the previous recipes, we've been using the = operator. When we execute something like a = 1, we are not performing an assignment; we are, instead, binding the value 1 to a.

This is actually pattern matching in its simplest form. The = operator is, in fact, called the match operator.

Getting ready

Start a new IEx session in your console.

How to do it…

To exercise our pattern matching techniques, we will follow these steps:

  1. Let's create a keyword list with our friends' birthdays:
    iex(1)> birthday_list = [andrew: "October 2nd", jim: "May 1st", carrie: "September 23rd", Carla: "August 30th"]
    [andrew: "October 2nd", jim: "May 1st", carrie: "September 23rd",carla: "August 30th"]
    
  2. Now, we will be getting the first element of ...

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