# Dispatch On Structure

> For heterogeneous data: pattern matching on type/structure, interpreter eval loops, handling different expression types.

- **Type:** Skill
- **Install:** `agentstack add skill-jimmc414-claude-code-plugin-marketplace-dispatch-on-structure`
- **Verified:** Pending review
- **Seller:** [jimmc414](https://agentstack.voostack.com/s/jimmc414)
- **Installs:** 0
- **Category:** [AI & ML](https://agentstack.voostack.com/c/ai-and-ml)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [jimmc414](https://github.com/jimmc414)
- **Source:** https://github.com/jimmc414/claude-code-plugin-marketplace/tree/master/plugins/norvig-patterns/skills/dispatch-on-structure

## Install

```sh
agentstack add skill-jimmc414-claude-code-plugin-marketplace-dispatch-on-structure
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# dispatch-on-structure

## When to Use
- Interpreters: different behavior per expression type
- Processing heterogeneous data
- Pattern matching (before Python 3.10 match)
- AST walkers
- Multi-type handlers

## When NOT to Use
- Homogeneous data (use single code path)
- Object-oriented dispatch is clearer (use methods)
- Simple type checking suffices

## The Pattern

Check structure/type of input and dispatch to appropriate handler.

```python
def process(x):
    """Dispatch based on structure of x."""
    if isinstance(x, int):
        return handle_int(x)
    if isinstance(x, list) and len(x) == 0:
        return handle_empty()
    if isinstance(x, list) and x[0] == 'quote':
        return handle_quote(x[1])
    if isinstance(x, list):
        return handle_list(x)
    raise TypeError(f"Unknown type: {type(x)}")
```

## Example (from pytudes lis.py)

```python
def eval(x, env=global_env):
    """Evaluate an expression in an environment."""

    # Dispatch on structure of x
    if isinstance(x, Symbol):           # Variable reference
        return env[x]

    elif not isinstance(x, List):       # Constant literal
        return x

    elif x[0] == 'quote':               # (quote exp)
        (_, exp) = x
        return exp

    elif x[0] == 'if':                  # (if test conseq alt)
        (_, test, conseq, alt) = x
        exp = conseq if eval(test, env) else alt
        return eval(exp, env)

    elif x[0] == 'define':              # (define var exp)
        (_, var, exp) = x
        env[var] = eval(exp, env)

    elif x[0] == 'lambda':              # (lambda (var...) body)
        (_, parms, body) = x
        return Procedure(parms, body, env)

    else:                               # (proc arg...)
        proc = eval(x[0], env)
        args = [eval(exp, env) for exp in x[1:]]
        return proc(*args)

# Differentiation dispatch (Differentiation.ipynb)
def D(y, x):
    """Differentiate y with respect to x."""
    if y == x:
        return 1
    if not isinstance(y, Expression):
        return 0
    if len(y.args) == 1:          # Unary: sin, cos, etc.
        return D_unary(y, x)
    if len(y.args) == 2:          # Binary: +, *, etc.
        return D_binary(y, x)
    raise ValueError(f"Unknown arity: {y}")
```

## Key Principles
1. **Order matters**: Check specific cases before general
2. **Destructure inline**: `(_, test, conseq, alt) = x`
3. **isinstance for types**: Check type before accessing
4. **Sentinel values**: Check `x[0] == 'quote'` for tagged lists
5. **Fallback/error**: Handle unknown cases explicitly

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [jimmc414](https://github.com/jimmc414)
- **Source:** [jimmc414/claude-code-plugin-marketplace](https://github.com/jimmc414/claude-code-plugin-marketplace)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** yes

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: flagged — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-jimmc414-claude-code-plugin-marketplace-dispatch-on-structure
- Seller: https://agentstack.voostack.com/s/jimmc414
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
