Word frequency
Counts words with a map, then pulls the top entries.
f wordCount(words: [S] +R) -> {S:I} +R
counts: {S:I} +R = {}
words.on(width => counts.set(width, counts.get(width) + 1))
r => counts
Snippets
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.
Counts words with a map, then pulls the top entries.
f wordCount(words: [S] +R) -> {S:I} +R
counts: {S:I} +R = {}
words.on(width => counts.set(width, counts.get(width) + 1))
r => counts
A fold over a heap list. No intermediate lists.
f runningTotal(nums: [I] +R) -> I r => nums.reduce((acc, num) => acc + num)
+V on the element type turns the map into vector instructions.
f scaleAll(arr: [I +V] +R) -> [I +V] +R r => arr.map(val => val * 3)
-M is a promise not to mutate, so this shares without a lock.
d Config
name: S
version: I
f describe(cfg: Config) -> S
r => "{{cfg.name}} v{{cfg.version}}"
A class becomes a plain C struct; the method takes a pointer.
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)
+R(GPU) puts pixels in device memory; the map becomes a WGSL shader.
f brighten(pixels: [I] +R(GPU)) -> [I] +R(GPU) r => pixels.map((px) => px * 3 + 1)
Math lowers to libm — identical natively and in WebAssembly.
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)
Three stages, one pass over the data.
f evenSquares(nums: [I] +R) -> I r => nums.filter(num => num % 2 == 0).map(num => num * num).sum()
+F is a callable producing the base type — a real C function pointer.
f double(xPos: I) -> I r => xPos * 2 f applyTo(fn: I +F(I), val: I) -> I r => fn(val)
A global counter — +G +M makes it MVCC-managed. All writes go through <<. CAS under the hood.
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
Multi-object atomic commit. Only pure code inside — effects before and after. Retries on conflict automatically.
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__())
Global constants (+G) need no annotation. The mutable window needs +G +M and <<. Two opt-ins, both visible.
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
Local +M accumulator in a loop — GCC/Clang auto-vectorizes this into SIMD. The -M default on locals forces the right shape.
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
Container frozen (no push), elements writable. Fine-grained access control — one annotation, no runtime cost.
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 ))
The playground encodes whatever you write into the link itself, so sharing a snippet is just sending a URL. Nothing is uploaded anywhere.