EddTally

EddTally

Pattern matching a map in function parameters

Hello, I am just wondering whether this is possible, or am I doing something wrong.
If so, what would be the best way to pattern match this type of thing, all in one function with a case statement?

def test1(%{num: num}), do: num + 1
def test1(%{num: num, acc: acc}), do: num + acc

iex(8)> test1(%{num: 1})
2
iex(9)> test1(%{num: 1, acc: 5})
2

I expected to receive 6 on the second function call iex(9).

Marked As Solved

kokolegorille

kokolegorille

The order of the functions is highly important, as the first match wins…

You can switch test1 functions to make it work.

Also Liked

kokolegorille

kokolegorille

The usual rule of thumb is to go from the more specific, to the more generic…

Of course for each rule You can find exception :slight_smile:

sodapopcan

sodapopcan

It may also be helpful to clarify that maps, unlike lists, allow for partial matches, so:

%{num: num} = %{num: 1, acc: 5} # Match!
[a, b, c] = [1, 2, 3, 4] # No match!
gregvaughn

gregvaughn

Another option is to consider 1 as a default value of acc and then you can do it with a single function clause

def test1(%{num: num} = m), do: num + Map.get(m, :acc, 1)
kokolegorille

kokolegorille

I would say list does partial match too…

[a, b, c | rest] = [1, 2, 3, 4] # match!
gregvaughn

gregvaughn

You can’t do that with pattern matching. You have to write the one line function that uses if and Map.get to determine and then call x or y function.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement