tobleron

tobleron

Pattern Matching Function (Without Equal Sign)

I was going through elixirschool.com and I am stuck to understand what the author means by specifically this kind of pattern matching. The other kinds I get it, but this one doesn’t make sense.

Functions and Pattern Matching

Behind the scenes, functions are pattern-matching the arguments that they’re called with.

Say we needed a function to accept a map but we’re only interested in using a particular key. We can pattern-match the argument on the presence of that key like this:

defmodule Greeter1 do
  def hello(%{name: person_name}) do
    IO.puts "Hello, " <> person_name
  end
end

This was the output from iex. A bunch of numbers?

{:module, Greeter1,
 <<70, 79, 82, 49, 0, 0, 4, 52, 66, 69, 65, 77, 65, 116, 85,
   56, 0, 0, 0, 133, 0, 0, 0, 14, 15, 69, 108, 105, 120,
   105, 114, 46, 71, 114, 101, 101, 116, 101, 114, 50, 8,
   95, 95, 105, 110, 102, 111, ...>>, {:hello, 1}}

So can anyone elaborate what’s happening here?

Marked As Solved

NobbZ

NobbZ

No, that match will fail as soon as you pass something into the function that is not a map, or if it is a map and does not have a key :name.

iex(1)> defmodule Greeter1 do
...(1)>   def hello(%{name: person_name}) do
...(1)>     IO.puts "Hello, " <> person_name
...(1)>   end
...(1)> end
{:module, Greeter1,
 <<70, 79, 82, 49, 0, 0, 4, 248, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 162,
   0, 0, 0, 18, 15, 69, 108, 105, 120, 105, 114, 46, 71, 114, 101, 101, 116,
   101, 114, 49, 8, 95, 95, 105, 110, 102, 111, ...>>, {:hello, 1}}
iex(2)> Greeter1.hello(1)
** (FunctionClauseError) no function clause matching in Greeter1.hello/1

    The following arguments were given to Greeter1.hello/1:

        # 1
        1

    iex:2: Greeter1.hello/1
iex(2)> Greeter1.hello(%{})
** (FunctionClauseError) no function clause matching in Greeter1.hello/1

    The following arguments were given to Greeter1.hello/1:

        # 1
        %{}

    iex:2: Greeter1.hello/1
iex(2)> Greeter1.hello(%{sur_name: "Random"})
** (FunctionClauseError) no function clause matching in Greeter1.hello/1

    The following arguments were given to Greeter1.hello/1:

        # 1
        %{sur_name: "Random"}

    iex:2: Greeter1.hello/1
iex(2)> Greeter1.hello(%{name: "Random"})
Hello, Random
:ok
iex(3)> Greeter1.hello(%{name: "Random", sur_name: "Guy"})
Hello, Random
:ok 

Also Liked

asummers

asummers

That’s the compiled module byte code. You need to call Greeter1.hello(%{name: "something"}) to actually invoke the function.

NobbZ

NobbZ

Thats where the pattern match happens.

NobbZ

NobbZ

Basically its the binary representation of the compiled module, you can ignore it when you see it in IEx.

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement