# \#case

**URL:** https://forum.elixirforum.com/tag/case/983.md

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

---

## [Scope of variable passed to function](https://forum.elixirforum.com/t/scope-of-variable-passed-to-function/69707)

<div class="topic-metadata">

**Author:** [@przemyxe0p](https://forum.elixirforum.com/u/przemyxe0p)\
**Replies:** 13\
**Last updated:** [March 1, 2025, 7:40pm UTC](https://forum.elixirforum.com/t/scope-of-variable-passed-to-function/69707 "2025-03-01T19:40:45Z")

</div>

Hi, I am wondering why my solution wil not work 1. source: Perfect Numbers in Elixir on Exercism 2. solution attempt: @spec classify(number :: integer) :: {:ok, atom} | {:error, String.t()} def classify(number) do …

---

## [Differing ways to specify default for Elixir case statement - are these the same?](https://forum.elixirforum.com/t/differing-ways-to-specify-default-for-elixir-case-statement-are-these-the-same/56510)

<div class="topic-metadata">

**Author:** [@hokie81](https://forum.elixirforum.com/u/hokie81)\
**Replies:** 1\
**Last updated:** [June 15, 2023, 2:03am UTC](https://forum.elixirforum.com/t/differing-ways-to-specify-default-for-elixir-case-statement-are-these-the-same/56510 "2023-06-15T02:03:33Z")

</div>

(pretty new to Elixir here…) sometimes I see: case \[...\] do {:ok, \[...\]} -\> \[...\] # using \_ as default {\_, other} -\> {:error, other} end other times: case \[...\] do {:ok, \[...\]} -\> \[...\] # not using \_ as def…

---

## [Am I over-optimizing unchanged tuple results?](https://forum.elixirforum.com/t/am-i-over-optimizing-unchanged-tuple-results/51175)

<div class="topic-metadata">

**Author:** [@halostatue](https://forum.elixirforum.com/u/halostatue)\
**Replies:** 11\
**Last updated:** [October 19, 2022, 5:34pm UTC](https://forum.elixirforum.com/t/am-i-over-optimizing-unchanged-tuple-results/51175 "2022-10-19T17:34:27Z")

</div>

Some years ago, I switched my case statement handling to something like this: case some\_operation() do {:ok, \_} = ok -\> ok {:error, reason} -\> # do something with reason end I did this because I recalled something …

---

## [Mix credo complains about having more than 9 case clauses](https://forum.elixirforum.com/t/mix-credo-complains-about-having-more-than-9-case-clauses/48410)

<div class="topic-metadata">

**Author:** [@docjazither](https://forum.elixirforum.com/u/docjazither)\
**Replies:** 3\
**Last updated:** [June 16, 2022, 11:41am UTC](https://forum.elixirforum.com/t/mix-credo-complains-about-having-more-than-9-case-clauses/48410 "2022-06-16T11:41:13Z")

</div>

Hi OGs, I have a function as follow: defp comparing\_statuses(duplicates) do statuses = count\_duplicates\_statuses(duplicates) case statuses do {"one", \_, "zero", \_, \_, \_} -\> iterate\_through\_duplic…

---

## [Is there a nicer way to write this case statement](https://forum.elixirforum.com/t/is-there-a-nicer-way-to-write-this-case-statement/47732)

<div class="topic-metadata">

**Author:** [@laiboonh](https://forum.elixirforum.com/u/laiboonh)\
**Replies:** 3\
**Last updated:** [May 13, 2022, 11:04am UTC](https://forum.elixirforum.com/t/is-there-a-nicer-way-to-write-this-case-statement/47732 "2022-05-13T11:04:18Z")

</div>

case Date.compare(date1, date2) do :lt-\> doA() :gt -\> doB() :eq -\> doA(); doB() end Thinking is there a way to write this more succinctly? Something like below but we know we cannot call a function…

---

## [Having trouble with \`formatter\` and \`case\`](https://forum.elixirforum.com/t/having-trouble-with-formatter-and-case/46136)

<div class="topic-metadata">

**Author:** [@bmitc](https://forum.elixirforum.com/u/bmitc)\
**Replies:** 6\
**Last updated:** [February 18, 2022, 7:54am UTC](https://forum.elixirforum.com/t/having-trouble-with-formatter-and-case/46136 "2022-02-18T07:54:58Z")

</div>

Hello! I am having issues with the Elixir formatter and a long case expression. What’s happening is that this: defmodule Test do def evaluate\_contract(%{from: from, to: to, value: value} = t, contract) do case con…

---

## [Why do I get variable is unused when using it in case do?](https://forum.elixirforum.com/t/why-do-i-get-variable-is-unused-when-using-it-in-case-do/40062)

<div class="topic-metadata">

**Author:** [@borybar](https://forum.elixirforum.com/u/borybar)\
**Replies:** 2\
**Last updated:** [May 28, 2021, 10:52am UTC](https://forum.elixirforum.com/t/why-do-i-get-variable-is-unused-when-using-it-in-case-do/40062 "2021-05-28T10:52:51Z")

</div>

Hi, I am trying to apply different calculations depending on gender using ‘case do’ however I get variable "user\_bmr" is unused. What am I not understanding? def show(conn, %{"id" =\> id}, current\_user) do bmr = Cpm…

---

## [Most elegant way for simple conditions an deconstructions](https://forum.elixirforum.com/t/most-elegant-way-for-simple-conditions-an-deconstructions/36121)

<div class="topic-metadata">

**Author:** [@stiegi](https://forum.elixirforum.com/u/stiegi)\
**Replies:** 6\
**Last updated:** [December 10, 2020, 12:42pm UTC](https://forum.elixirforum.com/t/most-elegant-way-for-simple-conditions-an-deconstructions/36121 "2020-12-10T12:42:54Z")

</div>

I would like to have your opinions about a very simple example, because I worked with imperative languages so far only, and I would like to get the mindset of declarative programming. So i have a function that is called…

---

## [Is it normal to use "case" all the time?](https://forum.elixirforum.com/t/is-it-normal-to-use-case-all-the-time/33792)

<div class="topic-metadata">

**Author:** [@mate](https://forum.elixirforum.com/u/mate)\
**Replies:** 3\
**Last updated:** [August 18, 2020, 7:25am UTC](https://forum.elixirforum.com/t/is-it-normal-to-use-case-all-the-time/33792 "2020-08-18T07:25:10Z")

</div>

Hello, I’d like to kindly ask about using case vs. other statements/conditions. I different languages (all OOP) I used if/else most of the time. I didn’t use case at all mainly because it needed break and I found if/el…

---

## [Turning case-do up to 11](https://forum.elixirforum.com/t/turning-case-do-up-to-11/28675)

<div class="topic-metadata">

**Author:** [@Oliver](https://forum.elixirforum.com/u/Oliver)\
**Replies:** 7\
**Last updated:** [February 8, 2020, 8:26pm UTC](https://forum.elixirforum.com/t/turning-case-do-up-to-11/28675 "2020-02-08T20:26:21Z")

</div>

Hi. I’m always looking for ways to make code more manageable, readable, and nice to look at. To this end, matching and the case-do statement have become my favorite tools in doing so. I often found myself writing code …

---

## [Iterating over a lists of list of tuple](https://forum.elixirforum.com/t/iterating-over-a-lists-of-list-of-tuple/25151)

<div class="topic-metadata">

**Author:** [@sagnik2911](https://forum.elixirforum.com/u/sagnik2911)\
**Replies:** 7\
**Last updated:** [September 5, 2019, 11:32pm UTC](https://forum.elixirforum.com/t/iterating-over-a-lists-of-list-of-tuple/25151 "2019-09-05T23:32:18Z")

</div>

Here nums is a list of \[list of {tuples}\]. nums = \[\[{5,2}\], \[{10,2},{4,5}\], \[{6,5},{3,10},{2,15}\]\] I want to print output like this: 10 5 2 20 10 2 4 5 30 6 5 3 10 2 15 Here, the first number is desired to be the pro…

---

## [Macro idea: total case statements](https://forum.elixirforum.com/t/macro-idea-total-case-statements/20834)

<div class="topic-metadata">

**Author:** [@Qqwy](https://forum.elixirforum.com/u/Qqwy)\
**Replies:** 3\
**Last updated:** [March 14, 2019, 2:16pm UTC](https://forum.elixirforum.com/t/macro-idea-total-case-statements/20834 "2019-03-14T14:16:52Z")

</div>

Hi everyone! I recently had an idea. This is by no means a revolutionary concep, and other people probably have thought about it before (but maybe not in the context of Elixir), but I think it might be a useful one. I …

---

## [Case with greater than](https://forum.elixirforum.com/t/case-with-greater-than/15936)

<div class="topic-metadata">

**Author:** [@otuv](https://forum.elixirforum.com/u/otuv)\
**Replies:** 4\
**Last updated:** [August 18, 2018, 5:46pm UTC](https://forum.elixirforum.com/t/case-with-greater-than/15936 "2018-08-18T17:46:47Z")

</div>

Hi, Simple thing but I cannot seem to find it anywhere. How do I do a case with greater than? As in: case Kernel.length my\_list do 0 → { :ok, :no\_data } \> 0 → { :ok, my\_list } \_ → { :error, :some\_general\_erro…
