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.

Last Post!

tobleron

tobleron

Thank you, now I understand!

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

We're in Beta

About us Mission Statement