Back to Skills

elixir-pattern-matching

verified

Use when Elixir pattern matching including function clauses, case statements, with statements, and destructuring. Use for elegant control flow.

View on GitHub

Marketplace

han

TheBushidoCollective/han

Plugin

jutsu-elixir

Technique

Repository

TheBushidoCollective/han
60stars

jutsu/jutsu-elixir/skills/elixir-pattern-matching/SKILL.md

Last Verified

January 24, 2026

Install Skill

Select agents to install to:

Scope:
npx add-skill https://github.com/TheBushidoCollective/han/blob/main/jutsu/jutsu-elixir/skills/elixir-pattern-matching/SKILL.md -a claude-code --skill elixir-pattern-matching

Installation paths:

Claude
.claude/skills/elixir-pattern-matching/
Powered by add-skill CLI

Instructions

# Elixir Pattern Matching

Master pattern matching in Elixir to write elegant, declarative code.
This skill covers function patterns, case statements, guards, and
destructuring across various data structures.

## Basic Pattern Matching

```elixir
# Simple assignment is pattern matching
x = 1
1 = x  # This works because x matches 1

# Pattern matching with tuples
{:ok, value} = {:ok, "success"}
value  # => "success"

# Will raise MatchError if patterns don't match
# {:error, _} = {:ok, "success"}  # MatchError

# Pin operator to use existing value
x = 1
^x = 1  # Works
# ^x = 2  # MatchError

# Ignore values with underscore
{:ok, _} = {:ok, "any value"}
{_, _, third} = {1, 2, 3}
third  # => 3
```

## Function Pattern Matching

```elixir
defmodule Calculator do
  def add(a, b), do: a + b

  def factorial(0), do: 1
  def factorial(n) when n > 0, do: n * factorial(n - 1)

  def describe_tuple({:ok, value}) do
    "Success: #{value}"
  end

  def describe_tuple({:error, reason}) do
    "Error: #{reason}"
  end

  def describe_tuple(_) do
    "Unknown tuple format"
  end
end

# Usage
Calculator.factorial(5)  # => 120
Calculator.describe_tuple({:ok, "done"})  # => "Success: done"
```

## Guards in Pattern Matching

```elixir
defmodule NumberChecker do
  def check(x) when is_integer(x) and x > 0 do
    "Positive integer"
  end

  def check(x) when is_integer(x) and x < 0 do
    "Negative integer"
  end

  def check(0), do: "Zero"

  def check(x) when is_float(x), do: "Float"

  def check(_), do: "Not a number"
end

defmodule Validator do
  def valid_email?(email) when is_binary(email) do
    String.contains?(email, "@")
  end

  def valid_email?(_), do: false

  def in_range?(num, min, max)
      when is_number(num) and num >= min and num <= max do
    true
  end

  def in_range?(_, _, _), do: false
end
```

## Case Statements

```elixir
defmodule ResponseHandler do
  def handle(response) do
    case response do
      {:ok, data} ->
        {:success, data}

      {:error, 

Validation Details

Front Matter
Required Fields
Valid Name Format
Valid Description
Has Sections
Allowed Tools
Instruction Length:
8499 chars