Pattern matching in Elixir

Pattern matching is a method used to check whether a specific sequence of tokens or characters exists among a given set of data.

It’s like matching a sequence of characters like cde in an other string like abcdefgh.

Pattern matching is extensively used and has many use cases.

Use cases of pattern matching

  1. Matching email patterns
  2. Matching phone number patterns
  3. Performing search based on string patterns
  4. Parsing values
  5. Cleaning datasets

Pattern matching in Elixir

Elixir is a general-purpose, dynamically typed and functional programming language that runs on Erlang’s VM.

The match operator

In Elixir, = is the match operator. It is used to match both sides of the equals symbol.

iex> x = 1
1
iex> 1 = x
1
iex> 5 = x
** (MatchError) no match of right hand side value: 1

In the example above, x stores 1 and the match operator 1 = x matches the value of x with 1. It works fine in this case but gives a MatchError when it matches to 5 = x which is false.

Pattern matching with tuples

The pattern in tuples can also be matched. Tuples are fix-sized containers used to store multiple elements. The match operator can be used with tuples as well.

{a,b,c} = {"Monday", "Tuesday", "Wednesday"}
IO.puts a # Displays "Monday"
IO.puts b # Displays "Tuesday"
IO.puts c # Displays "Wednesday"
{a,b,c} = {"Monday", "Tuesday", "Friday"}
IO.puts a # Displays "Monday"
IO.puts b # Displays "Tuesday"
IO.puts c # Displays "Friday"

In the above code, two tuples are equal to each other. The corresponding values in both tuples are equal. For example, in {a,b,c} = {"Monday", "Tuesday", "Wednesday"}, the pattern matches a with Monday, b with Tuesday and so on.

Pattern matching with lists

Lists are an immutable data type used to store a collection of values. Immutable means that the values cannot be changed once stored in lists.

Pattern matching in lists is explained using the code example below.

[a,b,c] = ["Monday", "Tuesday", "Wednesday"]
IO.puts a # Displays "Monday"
IO.puts b # Displays "Tuesday"
IO.puts c # Displays "Wednesday"

In above code example, the pattern of values from the left list [a,b,c] are matched with the right list ["Monday", "Tuesday", "Wednesday"]

[head | tail] = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
IO.puts head # Displays "Monday"
IO.puts tail # Displays "TuesdayWednesdayThursdayFriday"

Lists can also be matched with head/tail. The head is matched with the first element of the list and the tail is matched with the rest of the elements.

The pattern of a string can also be matched with the list like shown below:

[head | tail] = 'educative'
IO.puts head # Displays 101 (ASCII of e)
IO.puts tail # Displays "ducative"

The head corresponds the ASCII value of the first character in a string whereas the tail corresponds to the rest of the string.

Pattern matching with maps

Maps are a key-value based data structure in Elixir.

person = %{name: "Smith", age: 32, gender: "Male"}
IO.puts person.name # Displays "Smith"
IO.puts person.age # Displays "32"
IO.puts person.gender # Displays "Male"

In the above code, a map named person is created with some key-pair values of name, age and gender. The individual values are matched and accessed with person.name, person.age and person.gender.

Pattern matching inside functions

Patterns can also be matched inside functions in Elixir. During a function call, a pattern can be matched inside the function.

game = fn
"yes" -> "You are allowed to play game for one hour only"
"no" -> "You are not allowed to play any game"
end
IO.puts game.("yes") # Displays "You are allowed to play game for one hour only"
IO.puts game.("no") # Displays "You are not allowed to play any game"

A function named game is written with two function arguments i.e. yes and no. The pattern is matched while calling the function to display the respective _lause. IO.puts game.("yes") displays the clause for the yes argument and IO.puts game.("no") displays the clause for the no argument .

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved