# Learn Vex through examples

Choose a task, then follow the linked program and the API contracts around it.
The examples cover data modeling, resource ownership, concurrent work,
compile-time programming, numerical code and application libraries.

Complete programs identify their filename, run command and expected behavior.
Library fragments show a component to embed in a larger application; keep the
surrounding imports, owners and error handling when adapting them.

## Start with a small application

| Task | What you practice | Example |
| --- | --- | --- |
| Build a two-file library and its tests | Exported interfaces, imports, immutable/mutable values and test discovery | [Your first project](first-project.md) |
| Calculate a credit with rejected inputs | A domain contract, typed failures and positive/negative tests | [Testing and benchmarking](tooling/testing.md) |
| Return either a value or a useful error | Result, match, early return and exit status | [Introduction](introduction.md#explicit-failure-values), [error handling](error-handling.md) |
| Build a command-line interface | Commands, flags, arguments and help text | [CLI library](../std/cli.md) |
| Read structured configuration | Owning parsed values, lookup and conversion errors | [JSON](../std/serde/json.md), [TOML](../std/serde/toml.md), [YAML](../std/serde/yaml.md) |

## Make ownership visible

| Task | What you practice | Example |
| --- | --- | --- |
| Validate a packet and return a borrowed payload | Bounded views, error paths and keeping the owner alive | [Ownership workflows](memory/ownership-workflows.md) |
| Build an independently owned report | Borrowed input, transformation and owned return values | [Ownership workflows](memory/ownership-workflows.md) |
| Call a small C function | A native header/provider, ABI selection and one cleanup owner | [Your first native call](ffi-first-native.md) |
| Work with file contents or mapped regions | I/O errors, resource cleanup, bytes and filesystem paths | [Files](../std/fs/file.md), [filesystem API](../std/fs/reference.md) |
| Protect and authenticate a message | Key and nonce lifecycle, byte lengths and authentication failure | [Encryption](../std/crypto/encryption.md) |

## Coordinate independent work

| Task | What you practice | Example |
| --- | --- | --- |
| Collect a batch containing successes and failures | Async jobs, tagged replies, Result and bounded channels | [Fallible task results](concurrency/batch-results.md) |
| Merge producers and shut down cleanly | Fan-in, completion counts, channel ownership and cancellation | [Fan-in](concurrency/fan-in.md) |
| Serve HTTP requests | Request/response ownership, routes, middleware and body limits | [Fiber service](../std/http/fiber.md), [middleware](../std/http/middleware.md) |
| Store and query application state | Connection/statement lifetimes, parameter binding and transaction scope | [Database connections](../std/db/connection.md), [ORM](../std/db/orm.md) |

## Move work into the compiler

| Task | What you practice | Example |
| --- | --- | --- |
| Calculate a bounded retry schedule | Const functions, strict compile-time values and overflow checks | [Compile-time recipes](advanced/comptime-recipes.md) |
| Process fields selected by their exact type | Typed reflection and statically checked field access | [Compile-time recipes](advanced/comptime-recipes.md) |
| Generate repeated declarations from a typed description | DeclSet, generated methods/types and semantic checking | [Structural generation](advanced/comptime-declarations.md) |
| Share serialization metadata across models | Named policies, field tags, overrides and reflection | [Policies](types/policies.md) |

## Work with numbers, images and protocols

| Task | What you practice | Example |
| --- | --- | --- |
| Calibrate a batch of sensor values | Multiple graph inputs, elementwise arithmetic, masks and reduction | [Graph functions](gpu/graph-functions.md) |
| Differentiate a numerical expression | Primal computation, forward-mode derivatives and sensitivities | [Autograd](advanced/autograd.md) |
| Process a tile and its boundary | Shape/index arithmetic and a scalar reference | [Fusion and tiled work](fusion/graph.md) |
| Measure a geographic journey or region | Coordinate order, units, distance and containment | [Geographic coordinates](../std/geo.md) |
| Transform an image in memory | Pixel layout, owning images and deterministic transforms | [Image processing](../std/vips.md) |
| Create PCM and describe it with a WAV header | Sample layout, buffer sizes and container metadata | [Media components](../std/media.md) |
| Inspect and reconstruct an RTP packet | Checked parsing, network byte order and packet bounds | [Real-time protocols](../std/webrtc.md) |
| Model a FIFO | Capacity, occupancy, simultaneous operations and parameter widths | [Hardware examples](../hdl/design-examples.md) |

The hardware example belongs to the companion VexHDL project. GPU execution,
native libraries and network peers each introduce their own environment; follow
the linked page's setup and ownership contract.

## Build a language tool

The [language-tooling guide](tooling/language-tools.md) includes complete
tokenizer and width-sensitive layout programs, a lossless syntax-tree fragment,
and a map of the ten IDE library families. Extend those building blocks with
grammar rules, source-labelled diagnostics, query metadata or editor messages.

When a compiler change needs a regression, use
[compiler testing tools](../references/compiler-testing-tools.md) to retain and
replay generated cases. Application benchmarks and compiler-phase benchmarks
measure different workloads; keep their inputs and results separate.

## Adapt an example without losing its contract

Change one dimension at a time: input size, error case, ownership boundary,
concurrency or target. Keep an expected result for each change. For a parser,
include empty/truncated input; for a pipeline, include shutdown; for a numerical
operation, compare to a reference with an explicit tolerance.

The [capability map](language-status.md) shows the broader language surface.
The [verification guide](verification-evidence.md) explains how to retain
the command, compiler identity and result for your adaptation.
