Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Jack Marchant
Jack Marchant

Posted on • Originally published atengine.expert360.com on

     

Elixir Pattern Matching in a nutshell

Before being introduced toElixir, a functional programming language built on top ofErlang, I had no idea what pattern matching was. Hopefully, by the end of this article you will have at least a rudimentary understanding of how awesome it is.

In most programming languages, you will assign a value to a variable using something like:

const myVariable = 'my value';console.log(myVariable); // 'my value'
Enter fullscreen modeExit fullscreen mode

Now, myVariable is bound to the value you assigned to it and you can continue living your life.

When you need to check the value of a variable, in most other languages you would use conditional “if statements”, which can get unreadable as soon as you add more than 2 or 3. This is because it’s difficult to see the flow of logic, especially if the function spans many lines.

Technically you can do the same thing in Elixir, but how the compiler interprets it is significantly different. The = sign is actually called the ‘match’ operator. It will use the value on the left and compare it to the value on the right to determine if they are a match.

Tuples are used frequently in Elixir code to enable returning multiple values from a function. Typically, you would come across a {status, value} tuple, for example:

{:ok, return\_value} = do\_stuff()
Enter fullscreen modeExit fullscreen mode

do_stuff() must return a tuple which matches that structure (otherwise Elixir will raise a ‘MatchError’), and return_value is now bound to the second item in the tuple returned from this function.

This is basically how pattern matching works, but the real beauty is how you use it in various contexts, for example:

  • When a function can return multiple values, such as the {status, value} tuple we came across earlier:
case do\_stuff() do {:ok, value} -> value {:error, \_} -> raise "Oh no!"end
Enter fullscreen modeExit fullscreen mode
  • In function heads you can pattern match on parameters, to only run when particular requirements are met:
def my\_func({:ok, value}), do: valuedef my\_func({:error, \_}), do: raise "Oops!"IO.puts my\_func({:ok, "hello"}) # "hello"
Enter fullscreen modeExit fullscreen mode
  • You can even match on lists:
[first, second, third] = [1, 2, 3]
Enter fullscreen modeExit fullscreen mode
  • And decompose data structures
%{value: value} = map\_func()
Enter fullscreen modeExit fullscreen mode

There are so many examples of pattern matching in Elixir because it’s incredibly useful and powerful, and also very performant when compared to traditional methods.

In a nutshell, that’s pattern matching!


Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I'm a Software Engineer who writes about writing code.
  • Location
    Sydney
  • Education
    University of Wollongong
  • Work
    Principal Software Engineer at Deputy
  • Joined

More fromJack Marchant

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp