Examples

Examples

Each example is a complete program in docs/examples/ that you can run and change. Run it from the repository root:

pixi run mojo run -I src docs/examples/<name>.mojo

The programs check their results with std.testing. Documentation tests also compile them through source and precompiled package imports at O0 and O3 with --Werror, then compare their output with the output shown on the site.

Start with pipelines

Example What it shows Page
quickstart.mojo The smallest complete program: a one-stage pipe Home
callable_pipeline.mojo A pipeline of plain functions over a consumed list, ending in a fold Pipelines tutorial
callable_pipeline_generic.mojo Forwarding a pipeline through a generic function Pipelines tutorial
chained_pipeline.mojo A ten-stage chain with piped(...).then(...): plain functions, closures and partials fp.functions
partial.mojo Binding leading arguments with partial; binding a keyword with a closure fp.functions

Iterate and fold

Example What it shows Page
iteration.mojo Lazy selection, ordered reduction and initial-first snapshots Iteration tutorial
iteration_chaining.mojo Consuming method chains, both list collectors and native enumerate Iteration tutorial
generic_fold.mojo One fold_left over scalars, SIMD vectors and a user-defined document fp.iteration
iteration_generic.mojo Returning map and scan_left adapters from generic functions Iteration tutorial
iteration_filtering_generic.mojo Returning filtering adapters from generic functions Iteration tutorial
terminal_generic.mojo Generic terminal wrappers that keep the callback's exact error Iteration tutorial
control_flow.mojo while_loop, fori_loop and carry/output scan fp.control

Handle results and errors

Example What it shows Page
results.mojo Stored domain errors versus raised callback failures Results tutorial
result_transform_generic.mojo Generic Result transformations Results tutorial
attempt_generic.mojo A generic transaction helper built on attempt Results tutorial
attempt_once.mojo Capturing the failure of a consuming thunk fp.data
result_once.mojo Consuming handlers in Result transformations and folds fp.data

Store values in place

Example What it shows Page
foundations.mojo Callbacks, folds, typed errors and a Choice matched together Values stored in place
optional.mojo Matching a standard Optional and a Result, with a guard Values stored in place

Declare and match data

Example What it shows Page
shapes.mojo A sum type matched with clauses, a guard, a context and a pair of values Matching tutorial
traffic_light.mojo Matching two values, and three with a plain one, by every combination of constructors Matching tutorial
expression.mojo Structural recursion over an inductive expression type: evaluation, rendering and rewriting, with Next and shared values Inductive types tutorial
file_tree.mojo Recursive fields in a List and an Optional Recursion tutorial

Use algebra and effects

Example What it shows Page
algebra_core.mojo pure, map, flat_map and traverse over four native carriers fp.algebra
algebra_choices.mojo Mapping, monadic bind, map2, ap and List's Cartesian combination Algebra tutorial
algebra_traversal.mojo traverse and sequence, early termination and empty input Algebra tutorial
effects_reader.mojo Pricing configuration with ask, local and a borrowed deferred callback Effects tutorial
effects_state.mojo Reading, updating and resetting a counter with State Effects tutorial
effects_writer.mojo Ordered logs with Writer, tell, listen and censor Effects tutorial
state_result.mojo How transformer order decides whether a failure keeps its state fp.effects

Build an application

Example What it shows Page
config_parser.mojo Parsing configuration lazily, collecting Results and validating with a fold that stops early Application tutorial
job_router.mojo Routing a queue of commands with guarded, raising clauses, a context and attempt Application tutorial