Nanyx

All examples

Complete Nanyx programs from the repository, grouped by theme.

Each page in this section is a complete program from the examples directory of the Nanyx repository, shown as it is there. They are meant to be read for the shape of idiomatic Nanyx, or copied as a starting point. For step-by-step walkthroughs with a goal in mind, see the Cookbook instead.

First steps

Short programs that show the everyday shape of Nanyx code.

  • Zipping in for loops — A script module whose for loops walk several lists at once.
  • Fizzbuzzbaz — Fizzbuzz with the rules supplied at runtime, built from pipes and a returned lambda.
  • Eight queens — A recursive backtracking search with a local rec function and tagged results.
  • String casing — Convert between camel, pascal, kebab, snake, title and sentence case.
  • Regex matching with custom patterns — Custom patterns that wrap regular expressions so a match reads like a literal.
  • Semantic version parsing — Parse a version string into a record with optional tag and build metadata.

Modelling data

Records, tagged unions and protected types standing in for classes and refinement types.

  • Constrained values — Protected nominal types whose only constructors check an invariant.
  • Vending machine — An immutable state machine whose operations return the next state or a typed error.
  • Volleyball scoring — Custom patterns decide what the latest point means, so scoring is a plain match.
  • Bitcoin address classification — Classify addresses and derive the script hash an Electrum-style server indexes by.
  • Argument parsing — Walk a list of command-line arguments into a config record, with proc reporting the first error.
  • Traits and typeclasses — Structural record types for is-a abstractions, contexts for typeclass-style instances, and instance synthesis.
  • From a class to a record — Translate a caching manager class into a record, attached functions and contexts.

Contexts and effects

Contexts as implicit parameters: dependency injection, effects and state.

  • Contexts as effects — A Caesar cipher cracker, then reader and state effects expressed as contexts.
  • Nim — The game of Nim from the effect handlers paper, reworked for Nanyx contexts.
  • SQL utilities — Database access through a connection context the caller supplies with use.
  • Todo app: shared types — The types and protocol a todo client and server have in common.
  • Todo app: server — A server whose store is a context, so it runs over a database or an in-memory map.

Processing data

Tables, trees, images and generated source.

  • CSV from a table literal — A table literal of records written out as CSV, then read back and parsed with proc.
  • Device pairing from XML — Extract a pairing payload from an XML tree, with proc collecting the required elements.
  • Pixel sorting — A glitch-art effect that reorders the pixels of each image row by colour.
  • Source generator — Generate C# source with raw strings and interpolation side by side.

Talking to services

HTTP clients, JSON and error chaining with proc.

Larger programs

Longer examples that bring several features together.