ElixirWiki/PatternMatching

From Elixir Wiki
Jump to navigation Jump to search

Elixir Pattern Matching[edit]

Pattern matching is a fundamental feature in the Elixir programming language. It allows developers to match specific patterns in data structures, making it a powerful tool for manipulating and transforming data. Elixir's pattern matching can be used in a variety of scenarios, including assignments, function clauses, and function heads.

Syntax[edit]

In Elixir, pattern matching is denoted using the `=` operator. Here's a basic example:

```elixir {mood, temperature, day_of_week} = {:happy, 25, :friday} ```

In the example above, the tuple `{:happy, 25, :friday}` is being matched against the pattern `{mood, temperature, day_of_week}`. This means that `mood` will be bound to `:happy`, `temperature` will be bound to `25`, and `day_of_week` will be bound to `:friday`.

Simple Patterns[edit]

Pattern matching in Elixir can be done with various data types, including atoms, integers, floats, and strings. Here are some examples:

  • Matching an atom:

```elixir

hello = :hello

```

  • Matching an integer:

```elixir 42 = 42 ```

  • Matching a float:

```elixir 3.14 = 3.14 ```

  • Matching a string:

```elixir "elixir" = "elixir" ```

Pattern Matching in Function Clauses[edit]

Pattern matching plays a crucial role in defining different function clauses. Elixir allows multiple function clauses with different patterns and matching different arguments. Here's an example:

```elixir defmodule Math do

 def area({:rectangle, width, height}), do: width * height
 def area({:circle, radius}), do: 3.14 * radius * radius

end ```

In the code snippet above, the `Math` module defines two different function clauses for calculating the area of different shapes. The first clause matches the pattern `{:rectangle, width, height}`, while the second clause matches the pattern `{:circle, radius}`.

Pattern Matching Variables[edit]

Pattern matching variables in Elixir are denoted by a leading underscore `_`. This allows developers to ignore or discard values during pattern matching. Here's an example:

```elixir {_, y, _} = {1, 2, 3} ```

In the example above, the value `1` and `3` are ignored during pattern matching, and `y` will be bound to `2`.

Guards in Pattern Matching[edit]

Elixir also supports guards in pattern matching. Guards are additional conditions that can be used to further constrain the matching. Here's an example using guards:

```elixir defmodule Math do

 def double_positive(number) when is_integer(number) and number > 0 do
   number * 2
 end

end ```

In the code snippet above, the `double_positive/1` function will only match when the argument is an integer that is greater than 0.

Conclusion[edit]

Pattern matching is an essential feature of Elixir, allowing developers to elegantly handle complex data structures. It is used extensively in assignments, function clauses, and function heads. By understanding and utilizing pattern matching effectively, Elixir developers can write cleaner and more concise code.

See Also[edit]