Editing
Elixir Pattern Matching
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Elixir Pattern Matching == === Introduction === Pattern matching is a fundamental concept in the Elixir programming language. It allows developers to match data structures against patterns to perform specific actions based on the matched pattern. Pattern matching is heavily used in Elixir to destructure data, perform conditional branching, and normalize code. This article explains the key concepts and syntax of pattern matching in Elixir. === Syntax === In Elixir, pattern matching is performed using the `=` operator. The left-hand side of the `=` operator is the pattern, and the right-hand side is the value to match against. Here are some examples: ==== Matching Variables ==== ``` x = 42 ``` ==== Matching Values ==== ``` 42 = x ``` ==== Matching Tuples ==== ``` {1, 2, 3} = {a, b, c} ``` ==== Matching Lists ==== ``` [head | tail] = [1, 2, 3, 4, 5] ``` ==== Matching Maps ==== ``` %{name: "John", age: age} = %{name: "John", age: 30} ``` === Pattern Matching and Functions === Pattern matching plays a vital role in Elixir functions. When defining functions, multiple function clauses can be used, each having a different pattern to match against. Elixir will automatically select the appropriate function clause based on the matched pattern. === Advanced Pattern Matching === Elixir supports various advanced techniques for pattern matching, making it a powerful tool in functional programming. Some of these techniques include: ==== Guards ==== ``` defmodule Math do def factorial(n) when n < 0, do: "Invalid input" def factorial(0), do: 1 def factorial(n), do: n * factorial(n-1) end ``` ==== Pin Operator ==== ``` {a, ^a} = {1, 1} ``` ==== Ignoring Values ==== ``` {:ok, _} = {:ok, "Some value"} ``` ==== Pattern Matching in Function Heads ==== ``` defmodule MyList do def sum([head | tail]), do: head + sum(tail) def sum([]), do: 0 end ``` === Conclusion === Pattern matching is a powerful feature in Elixir that allows developers to manipulate and destructure data in a concise and expressive way. It is an essential tool for writing clear and maintainable code in Elixir. === See Also === * [[Elixir_Functions]] * [[Elixir_Lists]] * [[Elixir_Maps]] * [[Elixir_Guards]]
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information