Example gallery

Every program in examples/, by topic, with the first line of its opening comment. Run one with trine run examples/NAME.tr (the ones that reach files, the network, programs, C or Python say which --allow-... flags they need). Generated by tools/gallery.py; a test keeps it current.

Start here

examplewhat it shows
[calc.tr](../examples/calc.tr)A recursive data type: a tiny expression language with an evaluator and simplifier.
[collections.tr](../examples/collections.tr)Maps and sets: value semantics (a copy never changes behind your back), updated in place
[hello.tr](../examples/hello.tr)No type annotations anywhere: every type below is inferred.
[records.tr](../examples/records.tr)Records: constructors with named fields. Read with .field, copy-with-changes with .with(...).

Types, traits and generics

examplewhat it shows
[generics.tr](../examples/generics.tr)Generic types: Option and Result come built in; ? passes failures up.
[traits.tr](../examples/traits.tr)Traits: one generic definition works for every type that implements what it needs.
[tuples.tr](../examples/tuples.tr)Tuples: a few values travelling together, without declaring a type.

Text and data

examplewhat it shows
[fstrings.tr](../examples/fstrings.tr)f-strings and characters.
[json.tr](../examples/json.tr)A JSON parser and printer: recursive-descent, mutually recursive types, error positions.
[json_library.tr](../examples/json_library.tr)The JSON library (M2): parse_json, to_json, to_json_pretty and the json_* accessors, in every
[text.tr](../examples/text.tr)Text basics: parsing numbers, trimming, prefixes, fixed-decimal formatting.
[time_library.tr](../examples/time_library.tr)The time library (M2): timestamps (seconds since 1970, as now(sys) gives) as dates, formatted

Performance

examplewhat it shows
[fbip.tr](../examples/fbip.tr)Functional but in-place: code that rebuilds a uniquely owned tree reuses the tree's own memory.
[kernels.tr](../examples/kernels.tr)Pure array kernels: grid builds a matrix element by element, sum reduces in order.
[matmul.tr](../examples/matmul.tr)Matrix multiply: the future home of the first schedule demo
[sorting.tr](../examples/sorting.tr)Sorting: sort orders a list, sort_by orders it by a key, reverse turns it around.

Concurrency

examplewhat it shows
[async.tr](../examples/async.tr)async functions and par_map over records and closures (M4)
[tasks.tr](../examples/tasks.tr)Scoped tasks: spawn(fn() => ...) runs work alongside the rest of the function; wait(t)
[tools/fetch.tr](../examples/tools/fetch.tr)An HTTP fetcher (M2): get a URL, show the status, and pretty-print JSON or pick a field.
[tools/fetch_all.tr](../examples/tools/fetch_all.tr)Fetch many URLs at once (M4): each fetch is an async task, so they all wait on the network

Generators

examplewhat it shows
[generators_basics.tr](../examples/generators_basics.tr)Generators: a function that yields values one at a time, consumed by a for loop as they come.
[generators_more.tr](../examples/generators_more.tr)generator values, parameters, maps and strings in loop bodies, early returns from inlined loops
[generators_tree.tr](../examples/generators_tree.tr)A recursive generator: walking a binary tree in order, without building a list.

The outside world

examplewhat it shows
[capabilities.tr](../examples/capabilities.tr)Capability-gated I/O: a program can only touch what it is handed.
[tools/log_analyzer.tr](../examples/tools/log_analyzer.tr)A log analyzer (M2): web server access logs (the Common Log Format) from standard input or a
[tools/todo.tr](../examples/tools/todo.tr)A command-line tool (M2): a to-do list kept as JSON in a directory you grant.

Calling C and Python

examplewhat it shows
[ffi/zlib.tr](../examples/ffi/zlib.tr)Calling zlib from Trine (M8): C strings, byte buffers C writes into, and unsafe: blocks.
[python/pandas_report.tr](../examples/python/pandas_report.tr)pandas from Trine (M8): a DataFrame built from Trine data, grouped and summarized in Python,

Modules

examplewhat it shows
[modules_demo.tr](../examples/modules_demo.tr)Modules: import "file.tr" (or ... as name) gives the file's pub definitions as

More

examplewhat it shows
[comprehensions.tr](../examples/comprehensions.tr)M1 conveniences: comprehensions, updates like +=, nested index assignment, repetition,
[ffi.tr](../examples/ffi.tr)C math from Trine: extern fn declares a libm function on numbers (no permission needed for pure math).
[higher_order.tr](../examples/higher_order.tr)Generic functions with zero annotations: map/filter/fold are inferred polymorphic
[immediates.tr](../examples/immediates.tr)Small values without allocation: Ints, Bools and nullary constructors are immediates, and Option of them is cheap.
[lists.tr](../examples/lists.tr)Lists of every element kind. Natively, each kind uses its own storage (raw int64, raw double,
[loops.tr](../examples/loops.tr)break and continue: leave a loop early, or skip to its next round.
[primes.tr](../examples/primes.tr)Sieve of Eratosthenes with in-place list updates (unique owner => no copies).
[raytrace.tr](../examples/raytrace.tr)An ASCII ray tracer: spheres, diffuse lighting, hard shadows. Floats + data types + loops.
[server.tr](../examples/server.tr)An HTTP-style request handler for a todo service. Raw requests are parsed, routed,
[shapes.tr](../examples/shapes.tr)Algebraic data types + exhaustive pattern matching.
[slices.tr](../examples/slices.tr)Slices and string functions: take part of a list or a string, search, replace, change case.
[values.tr](../examples/values.tr)Mutable value semantics: no aliasing surprises, no lifetimes.