Skip to content

oliver-kriska/claude-elixir-phoenix / targets/dsh/skills/elixir-idioms

elixir-idioms

Verified

OTP/BEAM patterns and Elixir idioms — GenServer, Supervisor, Task, Registry, pattern matching, with chains, pipes. Use when designing processes or debugging BEAM issues.

references/pattern-matching.md

View on GitHub
# Pattern Matching Reference

## Function Heads (Preferred)

```elixir
def process(%{status: :active} = user), do: activate(user)
def process(%{status: :inactive} = user), do: deactivate(user)
def process(_user), do: {:error, :unknown_status}
```

## Pin Operator

Match against existing values:

```elixir
expected = :ok
^expected = get_result()  # Only matches if returns :ok

# Essential for dynamic map keys
key = :user_id
%{^key => value} = data
```

## is_non_struct_map/1 Guard (Elixir 1.17+)

Structs ARE maps, so `is_map/1` matches both:

```elixir
# DO: Distinguish plain maps from structs
def process(data) when is_non_struct_map(data), do: handle_map(data)
def process(%User{} = user), do: handle_user(user)

# DON'T: This matches structs too!
def process(%{} = data), do: handle_data(data)  # Matches User struct!
```

## Custom Guards

```elixir
defguard is_positive_integer(n) when is_integer(n) and n > 0

def process(n) when is_positive_integer(n), do: n * 2
```

## Binary Pattern Matching

```elixir
# UTF-8 prefix matching
"hello " <> rest = "hello world"  # rest = "world"

# Protocol parsing
<<header::8, length::32-big, payload::binary-size(length), rest::binary>> = data

# ANTI-PATTERN: Small sub-binary keeps large parent alive
<<small::binary-size(100), _::binary>> = one_gb_binary
# DO: Copy if keeping only the small part
small = :binary.copy(small)
```

## Guards: Allowed Operations

Guards must be pure, deterministic:

- Type checks: `is_atom/1`, `is_binary/1`, `is_integer/1`, `is_list/1`, `is_map/1`
- Comparisons: `==`, `!=`, `<`, `>`, `<=`, `>=`
- Arithmetic: `+`, `-`, `*`, `/`, `abs/1`, `div/2`, `rem/2`
- Value access: `hd/1`, `tl/1`, `elem/2`, `tuple_size/1`, `map_size/1`, `length/1`

Guards use `and`/`or`/`not`, never short-circuit operators (they require boolean operands)

```elixir
# CORRECT
def process(n) when is_integer(n) and n > 0, do: n * 2

# WRONG - compile error
def process(n) when is_integer(n) && n > 0, do: n * 2
```
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 322kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall