Snippets

Small programs worth stealing

One idea each. Copy a snippet, open it in the playground, or grab a share link that carries the code in the URL — no account, no server.

Word frequency

Counts words with a map, then pulls the top entries.

text
f wordCount(words: [S] +R) -> {S:I} +R
	counts: {S:I} +R = {}
	words.on(width => counts.set(width, counts.get(width) + 1))
	r => counts

Running total

A fold over a heap list. No intermediate lists.

collections
f runningTotal(nums: [I] +R) -> I
	r => nums.reduce((acc, num) => acc + num)

Vectorized scale

+V on the element type turns the map into vector instructions.

simd
f scaleAll(arr: [I +V] +R) -> [I +V] +R
	r => arr.map(val => val * 3)

Immutable config

-M is a promise not to mutate, so this shares without a lock.

mutation
d Config
	name: S
	version: I

f describe(cfg: Config) -> S
	r => "{{cfg.name}} v{{cfg.version}}"

Distance between points

A class becomes a plain C struct; the method takes a pointer.

classes
d Point
	xPos: N
	yPos: N

f distance(first: Point +R, second: Point +R) -> N
	r => hypot(first.xPos - second.xPos, first.yPos - second.yPos)

GPU brighten

+R(GPU) puts pixels in device memory; the map becomes a WGSL shader.

gpu
f brighten(pixels: [I] +R(GPU)) -> [I] +R(GPU)
	r => pixels.map((px) => px * 3 + 1)

Quadratic roots

Math lowers to libm — identical natively and in WebAssembly.

math
f discriminant(first: N, second: N, config: N) -> N
	r => second * second - 4.0 * first * config

f rootOne(first: N, second: N, config: N) -> N
	r => (0.0 - second + sqrt(discriminant(first, second, config))) / (2.0 * first)

Filter then sum

Three stages, one pass over the data.

collections
f evenSquares(nums: [I] +R) -> I
	r => nums.filter(num => num % 2 == 0).map(num => num * num).sum()

Function as a value

+F is a callable producing the base type — a real C function pointer.

functions
f double(xPos: I) -> I
	r => xPos * 2

f applyTo(fn: I +F(I), val: I) -> I
	r => fn(val)

Atomic counter

A global counter — +G +M makes it MVCC-managed. All writes go through <<. CAS under the hood.

concurrency
d Metrics
	requests: I +G +M = 0
	errors:   I +G +M = 0

f on_request() -> none
	Metrics << { requests: Metrics.requests + 1 }

f on_error() -> none
	Metrics << { errors: Metrics.errors + 1 }

f error_rate() -> N
	r => Metrics.errors / Metrics.requests

Bank transfer — transaction

Multi-object atomic commit. Only pure code inside — effects before and after. Retries on conflict automatically.

transactions
d Ledger
	checking: Q +G +M = 0.0
	savings:  Q +G +M = 0.0

f move_to_savings(amount: Q) -> none
	amount <= 0.0 ? r => none
	<< (
		Ledger.checking < amount ? x Rollback("insufficient")
		Ledger << {
			checking: Ledger.checking - amount,
			savings: Ledger.savings + amount
		}
	)
	log("moved " + amount.__string__())

Rate limiter — sliding window

Global constants (+G) need no annotation. The mutable window needs +G +M and <<. Two opt-ins, both visible.

concurrency
d RateLimit
	max_per_sec: I +G = 100
	window_ms:   I +G = 1000
	count:       I +G +M = 0
	window_start: I +G +M = 0

f allow(now_ms: I) -> L
	now_ms - RateLimit.window_start > RateLimit.window_ms ? RateLimit << { count: 1, window_start: now_ms }
	RateLimit.count >= RateLimit.max_per_sec ? r => false
	RateLimit << { count: RateLimit.count + 1 }
	r => true

Vectorizable reduction

Local +M accumulator in a loop — GCC/Clang auto-vectorizes this into SIMD. The -M default on locals forces the right shape.

performance
f dot_product(aa: [N], bb: [N]) -> N
	sum: N +M = 0.0
	[1..aa.len].on(ii => (
		sum = sum + aa[ii] * bb[ii]
		none
	))
	r => sum

Element +M hole-punch

Container frozen (no push), elements writable. Fine-grained access control — one annotation, no runtime cost.

mutability
d Particle
	xx: N +M = 0.0
	yy: N +M = 0.0
	vx: N +M = 0.0
	vy: N +M = 0.0

f step(parts: [Particle +M], dt: N) -> none
	[1..parts.len].on(ii => (
		parts[ii].xx = parts[ii].xx + parts[ii].vx * dt
		parts[ii].yy = parts[ii].yy + parts[ii].vy * dt
		none
	))

Share your own

The playground encodes whatever you write into the link itself, so sharing a snippet is just sending a URL. Nothing is uploaded anywhere.

Write one in the playground