# \#pattern-matching

**URL:** https://forum.elixirforum.com/tag/pattern-matching/51.md

[Latest](https://forum.elixirforum.com/latest.md) · [Categories](https://forum.elixirforum.com/categories.md) · [Tags](https://forum.elixirforum.com/tags.md)

---

## [Function Parameter Pattern Matching on multiple candidates?](https://forum.elixirforum.com/t/function-parameter-pattern-matching-on-multiple-candidates/74886)

<div class="topic-metadata">

**Author:** [@spammy](https://forum.elixirforum.com/u/spammy)\
**Replies:** 8\
**Last updated:** [April 5, 2026, 2:43pm UTC](https://forum.elixirforum.com/t/function-parameter-pattern-matching-on-multiple-candidates/74886 "2026-04-05T14:43:41Z")

</div>

Is it possible to match multiple parameters to a single function body? So for: def foo("a" = word) do word end def foo("b" = word) do word end def foo("c" = word) do "not a or b!" end have a single definition for…

---

## [Binary Pattern Matching in Elixir](https://forum.elixirforum.com/t/binary-pattern-matching-in-elixir/72868)

<div class="topic-metadata">

**Author:** [@PJUllrich](https://forum.elixirforum.com/u/PJUllrich)\
**Replies:** 0\
**Last updated:** [October 11, 2025, 12:15pm UTC](https://forum.elixirforum.com/t/binary-pattern-matching-in-elixir/72868 "2025-10-11T12:15:31Z")

</div>

An in-depth guide on how to pattern match against binary data in Elixir.

---

## [Defining function with same arity but different map values in one of param](https://forum.elixirforum.com/t/defining-function-with-same-arity-but-different-map-values-in-one-of-param/72087)

<div class="topic-metadata">

**Author:** [@spacemonk](https://forum.elixirforum.com/u/spacemonk)\
**Replies:** 8\
**Last updated:** [August 15, 2025, 4:33am UTC](https://forum.elixirforum.com/t/defining-function-with-same-arity-but-different-map-values-in-one-of-param/72087 "2025-08-15T04:33:17Z")

</div>

Hi! I might be asking a silly one, sorry for that and thanks for your time! my aim is to implement quite simple logic: the get\_or\_create\_movie could be called with first param with “type” = “movie”, in that case I do n…

---

## [Understanding the match operator](https://forum.elixirforum.com/t/understanding-the-match-operator/69871)

<div class="topic-metadata">

**Author:** [@Parashoe](https://forum.elixirforum.com/u/Parashoe)\
**Replies:** 16\
**Last updated:** [March 10, 2025, 2:45pm UTC](https://forum.elixirforum.com/t/understanding-the-match-operator/69871 "2025-03-10T14:45:50Z")

</div>

I’m not fully understanding how matching works with function arguments. In Functions and Pattern Matching it is explained (IMO poorly) that everything in the argument is matched independently. As a result this experiment…

---

## [Function call expecting list works with a tuple](https://forum.elixirforum.com/t/function-call-expecting-list-works-with-a-tuple/68904)

<div class="topic-metadata">

**Author:** [@donnieparka](https://forum.elixirforum.com/u/donnieparka)\
**Replies:** 7\
**Last updated:** [January 22, 2025, 4:57pm UTC](https://forum.elixirforum.com/t/function-call-expecting-list-works-with-a-tuple/68904 "2025-01-22T16:57:09Z")

</div>

defmodule Navigator do def navigate(dir) do expanded\_dir = Path.expand(dir) go\_through(\[expanded\_dir\]) end def go\_through(\[\]), do: nil def go\_through(\[content | rest\]) do print\_and\_navigate(content…

---

## [TaggedTupleShorthand - Field Punning in Elixir](https://forum.elixirforum.com/t/taggedtupleshorthand-field-punning-in-elixir/68298)

<div class="topic-metadata">

**Author:** [@christhekeele](https://forum.elixirforum.com/u/christhekeele)\
**Replies:** 3\
**Last updated:** [December 23, 2024, 7:28am UTC](https://forum.elixirforum.com/t/taggedtupleshorthand-field-punning-in-elixir/68298 "2024-12-23T07:28:41Z")

</div>

Hey all! I’m releasing TaggedTupleShorthand today as an experiment with Elixir syntax. Field Punning The aim of the library is to support a commonly-requested Elixir feature1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17: S…

---

## [Credo check for encouraging pattern matching by struct in the parameters](https://forum.elixirforum.com/t/credo-check-for-encouraging-pattern-matching-by-struct-in-the-parameters/68265)

<div class="topic-metadata">

**Author:** [@gperales](https://forum.elixirforum.com/u/gperales)\
**Replies:** 1\
**Last updated:** [December 20, 2024, 7:41pm UTC](https://forum.elixirforum.com/t/credo-check-for-encouraging-pattern-matching-by-struct-in-the-parameters/68265 "2024-12-20T19:41:01Z")

</div>

Hi folks! With the new release of Elixir 1.18, I would like to enforce pattern-matching by struct in our projects to take advantage of the type inference. To do so, I’m working on a Credo check and I would like feedback…

---

## [DynamicSupervisor: how to start child iff it doesn't exist?](https://forum.elixirforum.com/t/dynamicsupervisor-how-to-start-child-iff-it-doesnt-exist/67858)

<div class="topic-metadata">

**Author:** [@James\_E](https://forum.elixirforum.com/u/James_E)\
**Replies:** 9\
**Last updated:** [December 6, 2024, 12:52am UTC](https://forum.elixirforum.com/t/dynamicsupervisor-how-to-start-child-iff-it-doesnt-exist/67858 "2024-12-06T00:52:57Z")

</div>

I’ve got this bit of code; I’m wondering why create\_or\_open\_room always creates a new room, even when there is an existing child process with the same ID: If a child specification with the specified ID already exists, c…

---

## [Boolean expression pattern matching with tuples](https://forum.elixirforum.com/t/boolean-expression-pattern-matching-with-tuples/66545)

<div class="topic-metadata">

**Author:** [@cd-slash](https://forum.elixirforum.com/u/cd-slash)\
**Replies:** 3\
**Last updated:** [October 6, 2024, 3:20pm UTC](https://forum.elixirforum.com/t/boolean-expression-pattern-matching-with-tuples/66545 "2024-10-06T15:20:20Z")

</div>

I have a case clause (contrived for the purpose of this question): level = 4 has\_children\_or\_no\_children = :no\_children case {level, has\_children\_or\_no\_children} do {3, :has\_children} -\> 1 {4, :no\_children} -\> …

---

## [Return variables and their matches in ets select](https://forum.elixirforum.com/t/return-variables-and-their-matches-in-ets-select/63903)

<div class="topic-metadata">

**Author:** [@Leo-Nogismus](https://forum.elixirforum.com/u/Leo-Nogismus)\
**Replies:** 3\
**Last updated:** [May 31, 2024, 5:37pm UTC](https://forum.elixirforum.com/t/return-variables-and-their-matches-in-ets-select/63903 "2024-05-31T17:37:09Z")

</div>

I was wondering if there is a way to return both the variables (e.g., :“$1”), and its matched value with a single call to :ets.select(table, match\_spec). Assuming, for example, that we have an ets table called :foo, whi…

---

## [How to pattern match list of structs from mysql results for multiple conditions?](https://forum.elixirforum.com/t/how-to-pattern-match-list-of-structs-from-mysql-results-for-multiple-conditions/63001)

<div class="topic-metadata">

**Author:** [@peppy](https://forum.elixirforum.com/u/peppy)\
**Replies:** 5\
**Last updated:** [April 19, 2024, 9:53am UTC](https://forum.elixirforum.com/t/how-to-pattern-match-list-of-structs-from-mysql-results-for-multiple-conditions/63001 "2024-04-19T09:53:22Z")

</div>

I’m trying to build a simple function to see if a user is authorized to join a conversation. Basically, it needs to query the mysql database to get a list of all users in the conversation, and then compare the userid on …

---

## [Pattern Match with Japanese](https://forum.elixirforum.com/t/pattern-match-with-japanese/62660)

<div class="topic-metadata">

**Author:** [@sora1](https://forum.elixirforum.com/u/sora1)\
**Replies:** 2\
**Last updated:** [April 3, 2024, 1:01am UTC](https://forum.elixirforum.com/t/pattern-match-with-japanese/62660 "2024-04-03T01:01:33Z")

</div>

version elixir: 1.16.0 erlang: 26.2.1 Hi! When I used pattern match with japanese words like this below String.match?("あ", ~r/\[りんご\]/) It returns true, But it shouldn\`t. I tried other language like Ruby, and it retu…

---

## [Why can't I use a variable to the left of \`\<\>\` in a pattern match? Compiler or Theory?](https://forum.elixirforum.com/t/why-cant-i-use-a-variable-to-the-left-of-in-a-pattern-match-compiler-or-theory/61547)

<div class="topic-metadata">

**Author:** [@spencer.christensen](https://forum.elixirforum.com/u/spencer.christensen)\
**Replies:** 8\
**Last updated:** [February 8, 2024, 11:18pm UTC](https://forum.elixirforum.com/t/why-cant-i-use-a-variable-to-the-left-of-in-a-pattern-match-compiler-or-theory/61547 "2024-02-08T23:18:06Z")

</div>

iex(26)\> "hello " \<\> matched = "hello world" "hello world" iex(27)\> matched "world" iex(28)\> matched \<\> " world" = "hello world" \*\* (ArgumentError) the left argument of \<\> operator inside a match should always be a liter…

---

## [Pattern matching against arbitrary sized tuple](https://forum.elixirforum.com/t/pattern-matching-against-arbitrary-sized-tuple/17263)

<div class="topic-metadata">

**Author:** [@ollran](https://forum.elixirforum.com/u/ollran)\
**Replies:** 6\
**Last updated:** [January 24, 2024, 2:13pm UTC](https://forum.elixirforum.com/t/pattern-matching-against-arbitrary-sized-tuple/17263 "2024-01-24T14:13:21Z")

</div>

How can I pattern match against an arbitrary sized tuple? This works: {status, \_, \_} = {:ok, 1, 2} But what if I don’t know the structure of the tuple on the right side and I only care about the first element? This doe…

---

## [What is the best pattern matching function's arguments or in function's body?](https://forum.elixirforum.com/t/what-is-the-best-pattern-matching-functions-arguments-or-in-functions-body/59647)

<div class="topic-metadata">

**Author:** [@gameline](https://forum.elixirforum.com/u/gameline)\
**Replies:** 15\
**Last updated:** [November 12, 2023, 4:18pm UTC](https://forum.elixirforum.com/t/what-is-the-best-pattern-matching-functions-arguments-or-in-functions-body/59647 "2023-11-12T16:18:36Z")

</div>

Hi, I was surprised I couldn’t find any argument on this on the net so here I am. When you make recursive function that deal with list and want to pattern match argument against \[\] or \[head|tail\] you can: function \[\],…

---

## [Object literals vs pattern matching or alternatives?](https://forum.elixirforum.com/t/object-literals-vs-pattern-matching-or-alternatives/59465)

<div class="topic-metadata">

**Author:** [@Gonza](https://forum.elixirforum.com/u/Gonza)\
**Replies:** 16\
**Last updated:** [November 7, 2023, 10:58am UTC](https://forum.elixirforum.com/t/object-literals-vs-pattern-matching-or-alternatives/59465 "2023-11-07T10:58:18Z")

</div>

Hi! Quick question from a guy coming from Javascript: Whenever I have to handle some conditional behavior, like ‘do something depending on a well-defined flag’, instead of using a switch/case construct or a bunch of if/…

---

## [Need a suggestion to create a pattern match by accepting optional parameters](https://forum.elixirforum.com/t/need-a-suggestion-to-create-a-pattern-match-by-accepting-optional-parameters/57558)

<div class="topic-metadata">

**Author:** [@shahryarjb](https://forum.elixirforum.com/u/shahryarjb)\
**Replies:** 7\
**Last updated:** [August 11, 2023, 12:24pm UTC](https://forum.elixirforum.com/t/need-a-suggestion-to-create-a-pattern-match-by-accepting-optional-parameters/57558 "2023-08-11T12:24:50Z")

</div>

Hello, I have been developing a simple macro and now I want to improve it and let it accept nested option. For example: guardedstruct do field(:id, String.t(), derive: "sanitize(trim, strip\_tags) validate(string)") …

---

## [Efficient comparision of large data structures](https://forum.elixirforum.com/t/efficient-comparision-of-large-data-structures/35449)

<div class="topic-metadata">

**Author:** [@mpope](https://forum.elixirforum.com/u/mpope)\
**Replies:** 20\
**Last updated:** [August 8, 2023, 9:08am UTC](https://forum.elixirforum.com/t/efficient-comparision-of-large-data-structures/35449 "2023-08-08T09:08:24Z")

</div>

Say I have a very large data structure who’s size is in the 10s of MB. This structure is arbitrarily complex, consisting of many tuples and proplists and maps, but in a roughly tree-like structure. What is the most eff…

---

## [List as functions argument never matched](https://forum.elixirforum.com/t/list-as-functions-argument-never-matched/54270)

<div class="topic-metadata">

**Author:** [@rrrr](https://forum.elixirforum.com/u/rrrr)\
**Replies:** 2\
**Last updated:** [March 2, 2023, 3:15pm UTC](https://forum.elixirforum.com/t/list-as-functions-argument-never-matched/54270 "2023-03-02T15:15:59Z")

</div>

I got 2 warnings and “unexpected” behavior when trying to match argument as a list. Is this code incorrect? How can I match a argument as a List? (It’s first code i am writing by myself :innocent: I was trying to be clo…

---

## [Handling different Joken error messages with json()](https://forum.elixirforum.com/t/handling-different-joken-error-messages-with-json/53770)

<div class="topic-metadata">

**Author:** [@ghenry](https://forum.elixirforum.com/u/ghenry)\
**Replies:** 5\
**Last updated:** [February 8, 2023, 10:14pm UTC](https://forum.elixirforum.com/t/handling-different-joken-error-messages-with-json/53770 "2023-02-08T22:14:06Z")

</div>

Hi all, A beginner here. I have the following plug code from an example Auth0 repo: def call(conn, \_default) do with {:ok, token} when is\_binary(token) \<- get\_token(conn), {:ok, claims} \<- Token.verify\_an…

---

## [CompileError: "invalid use of \_. "\_" represents a value to be ignored in a pattern and cannot be used in expressions"](https://forum.elixirforum.com/t/compileerror-invalid-use-of-represents-a-value-to-be-ignored-in-a-pattern-and-cannot-be-used-in-expressions/44818)

<div class="topic-metadata">

**Author:** [@Maxximiliann](https://forum.elixirforum.com/u/Maxximiliann)\
**Replies:** 10\
**Last updated:** [January 30, 2023, 3:03pm UTC](https://forum.elixirforum.com/t/compileerror-invalid-use-of-represents-a-value-to-be-ignored-in-a-pattern-and-cannot-be-used-in-expressions/44818 "2023-01-30T15:03:30Z")

</div>

This case statement is perfectly functional, but it repeats code: case foo do {:pass, \_} = reason -\> Logger.info(reason) {:ok, \_, \_} -\> status = Keyword.get(opts, :status) some\_func(status) Logger.…

---

## [Pattern matching a map as a parameter of a function](https://forum.elixirforum.com/t/pattern-matching-a-map-as-a-parameter-of-a-function/53335)

<div class="topic-metadata">

**Author:** [@BlasterMaster118](https://forum.elixirforum.com/u/BlasterMaster118)\
**Replies:** 4\
**Last updated:** [January 22, 2023, 3:30pm UTC](https://forum.elixirforum.com/t/pattern-matching-a-map-as-a-parameter-of-a-function/53335 "2023-01-22T15:30:12Z")

</div>

I have the following test code to test pattern matching on functions sending a map as a parameter defmodule TestMapAsParam do def resolve( %{"timezone" =\> timezone, "store\_number" =\> store\_number} ) …

---

## [Ensuring module implements behaviour](https://forum.elixirforum.com/t/ensuring-module-implements-behaviour/49703)

<div class="topic-metadata">

**Author:** [@aochagavia](https://forum.elixirforum.com/u/aochagavia)\
**Replies:** 10\
**Last updated:** [December 28, 2022, 11:33pm UTC](https://forum.elixirforum.com/t/ensuring-module-implements-behaviour/49703 "2022-12-28T23:33:26Z")

</div>

Context A feature of Elixir I find myself using often when defining functions is to pattern match on structs. This way I can ensure the function is being called with the data I expect, get nice autocompletion in my IDE a…

---

## [Bumblebee - New Text Generation Model](https://forum.elixirforum.com/t/bumblebee-new-text-generation-model/52476)

<div class="topic-metadata">

**Author:** [@TwistingTwists](https://forum.elixirforum.com/u/TwistingTwists)\
**Replies:** 3\
**Last updated:** [December 14, 2022, 7:44am UTC](https://forum.elixirforum.com/t/bumblebee-new-text-generation-model/52476 "2022-12-14T07:44:07Z")

</div>

I plan to use the following model {:ok, bertqca} = Bumblebee.load\_model({:hf, "voidful/bart-eqg-question-generator"}) with tokenizer {:ok, tokenizer} = Bumblebee.load\_tokenizer({:hf, "facebook/bart-base"}) But on ru…

---

## [Mixed pattern matching](https://forum.elixirforum.com/t/mixed-pattern-matching/50442)

<div class="topic-metadata">

**Author:** [@blackened](https://forum.elixirforum.com/u/blackened)\
**Replies:** 4\
**Last updated:** [September 22, 2022, 1:11pm UTC](https://forum.elixirforum.com/t/mixed-pattern-matching/50442 "2022-09-22T13:11:19Z")

</div>

Suppose I have these two lists: list1 = \["one", "two", "three"\] and list2 = \["two", "four", "three"\]. What would be an Elixir-way function to achieve the following: each item of list 2 will be compared to list 1 in the …

---

## [Best approach to replace early exits the elixir way](https://forum.elixirforum.com/t/best-approach-to-replace-early-exits-the-elixir-way/50152)

<div class="topic-metadata">

**Author:** [@artem](https://forum.elixirforum.com/u/artem)\
**Replies:** 4\
**Last updated:** [September 10, 2022, 6:45pm UTC](https://forum.elixirforum.com/t/best-approach-to-replace-early-exits-the-elixir-way/50152 "2022-09-10T18:45:08Z")

</div>

Hi all I am still in my early days with Elixir and relatively new to functional languages (did one hobby project in Elm), so most interested in figuring the elixir ways for this or that. Quite often I am in the situati…

---

## [What does %User{} = user means?](https://forum.elixirforum.com/t/what-does-user-user-means/50068)

<div class="topic-metadata">

**Author:** [@menw](https://forum.elixirforum.com/u/menw)\
**Replies:** 3\
**Last updated:** [September 7, 2022, 11:07am UTC](https://forum.elixirforum.com/t/what-does-user-user-means/50068 "2022-09-07T11:07:33Z")

</div>

Hi, I’m new in ecto and elixir. Well, I’m learning about phoenix, ecto and found this sample code def change\_registration(%User{} = user, params) do User.registration\_changeset(user, params) end Does %User{} = us…

---

## [Trying to understand certain concepts through a Mastermind game sub-problem](https://forum.elixirforum.com/t/trying-to-understand-certain-concepts-through-a-mastermind-game-sub-problem/49849)

<div class="topic-metadata">

**Author:** [@blackened](https://forum.elixirforum.com/u/blackened)\
**Replies:** 4\
**Last updated:** [September 2, 2022, 12:44am UTC](https://forum.elixirforum.com/t/trying-to-understand-certain-concepts-through-a-mastermind-game-sub-problem/49849 "2022-09-02T00:44:34Z")

</div>

I am a complete Elixir beginner. I am trying to understand certain concepts with the following Mastermind sub-problem. Suppose I have two lists code = \[0, 3, 2 ,3, 4, 5\] and guess = \[0, 4, 2, 4, 3, 3\]. My approach is, …

---

## [Elixir way of pattern-matching against constants in function signature](https://forum.elixirforum.com/t/elixir-way-of-pattern-matching-against-constants-in-function-signature/49090)

<div class="topic-metadata">

**Author:** [@jarmo](https://forum.elixirforum.com/u/jarmo)\
**Replies:** 6\
**Last updated:** [August 7, 2022, 11:21pm UTC](https://forum.elixirforum.com/t/elixir-way-of-pattern-matching-against-constants-in-function-signature/49090 "2022-08-07T23:21:22Z")

</div>

How is Elixir way of pattern matching against other modules attribute value in function signatures? For example, let’s imagine we have module like the following: defmodule Bar do @const 42 def const, do: @const end…

---

## [Way too much detail about matching in the head vs accessing maps in the body](https://forum.elixirforum.com/t/way-too-much-detail-about-matching-in-the-head-vs-accessing-maps-in-the-body/49167)

<div class="topic-metadata">

**Author:** [@al2o3cr](https://forum.elixirforum.com/u/al2o3cr)\
**Replies:** 2\
**Last updated:** [July 27, 2022, 12:14am UTC](https://forum.elixirforum.com/t/way-too-much-detail-about-matching-in-the-head-vs-accessing-maps-in-the-body/49167 "2022-07-27T00:14:27Z")

</div>

(forking off from this thread because this is 1000% off-topic from where that thread started) Here, I’m just curious about something. I wonder wether there is a performance gain between getting “sub-parameters” in a f…

[Next page](https://forum.elixirforum.com/tag/pattern-matching/51.md?match_all_tags=true&page=1&tags%5B%5D=pattern-matching)
