<jupyter_start><jupyter_text>Pattern matching in `case` statements[`case` in the Elixir docs](https://elixir-lang.org/getting-started/case-cond-and-if.htmlcase) Compare values against a selection of patterns, until one works.<jupyter_code>case {1, 2, 3} do
  {4, 5, 6} ->
    "This clause won't match"
  {1, x, 3} ->
    "This clause will match and bind x to #{x} in this clause"
  _ ->
    "This clause would match any value"
end<jupyter_output><empty_output><jupyter_text>---Match an existing variable with the pin `^` operator.<jupyter_code>x = 1

case 10 do
  ^x -> "Won't match"
  _ -> "Will match"
end<jupyter_output><empty_output><jupyter_text>---Clauses also allow extra conditions with guards.<jupyter_code>case {1, 2, 3} do
  {1, x, 3} when x > 0 ->
    "Will match"
  _ ->
    "Would match, if guard condition were not satisfied"
end<jupyter_output><empty_output><jupyter_text>---Errors in guards do not leak, they just make the guard fail.<jupyter_code>hd(1) # this is an error
case 1 do
  x when hd(x) -> "Won't match"
  x -> "Got #{x}"
end<jupyter_output><empty_output><jupyter_text>---At least one clause must match, or an error is raised.<jupyter_code>case :ok do
  :error -> "Won't match"
end<jupyter_output><empty_output><jupyter_text>Anonymous FunctionsAnonymous functions can also have multiple clauses and guards.<jupyter_code>f = fn
  x, y when x > 0 -> x + y
  x, y -> x * y
end
f.(1, 3)
f.(-1, 3)<jupyter_output><empty_output><jupyter_text>---The number of arguments must match in each clause, or an error is raised.<jupyter_code>f2 = fn
  x, y when x > 0 -> x + y
  x, y, z -> x * y + z
end<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1331306990881459, 'fraction_numerical': 0.015805471124620062, 'mean_word_length': 4.111801242236025, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 46, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '481053', 'license': 'MIT', 'n_tokens_mistral': 637, 'n_tokens_neox': 605, 'n_words': 193, 'path': '4) Pattern Matching - Case.ipynb', 'repo_name': 'mudphone/EveryElixirPatternMatchingExampleEver', 'size': '1645'}