Examples
Real programs from the repo, each with the output it really prints.
fn add ['a': int32, 'b': int32] -> int32 {
return 'a' plus 'b';
}
MAIN {
let 'sum' := add[10, 20];
stdkit::print.useful["sum: ", 'sum', newln];Add8 linesfn loud ['name': str.utf8, 'answer': bool] -> bool {
stdkit::print.useful["checked ", 'name', newln];
return 'answer';
}
MAIN {
let 'ok' := loud["first", false] and loud["second", true]; # both printand runs both sides18 linesfn loud ['name': str.utf8, 'answer': bool] -> bool {
stdkit::print.useful["checked ", 'name', newln];
return 'answer';
}
MAIN {
let 'xs' := [3, 5, 8];and.then / or.else19 linesMAIN {
let 'sample': not_std.int24 = -8_000_000;
let 'flags': not_std.uint4 = 9;
let 'big': not_std.int100 = 1_000_000_000_000_000_000;
stdkit::print.useful['sample', " ", 'flags', " ", 'big', " ", ('flags' plus.wrap 9), newln];
}Any width7 linesMAIN ['argc': int32, 'argv': ptr[ptr[uint8]]] {
stdkit::print.stored["argc = ", 'argc', newln];
let mut 'i': int32 = 1;
while 'i' < 'argc' {
no_check let 'word' := ptr.read[ptr.at['argv', 'i']];
let mut 'k': int32 = 0;Args24 linesfn show ['b': uint8] { stdkit::print.useful['b', newln]; }
fn wide ['w': uint32] -> uint32 { return 'w'; }
MAIN {
show[0 minus.wrap 1];
let 'v': uint64 = to.uint64[wide[2 shl 3]];
stdkit::print.useful['v', " ", wide[not.bit 0], newln];Argument context12 linesbare.never [fn, struct]; # every pub fn and struct in this Limb: the path is required
pub const 'unit' := 1; # a const takes no mark: its path is always written
pub bare.never module shapes { # everything in this block module
pub fn area ['side': int32] -> int32 {
return 'side' times 'side';
}Bare marks25 linesMAIN {
let 'f': bin32 = 0.1;
let 'h': bin16 = 0.1;
let 'd': bin64 = 0.1;
stdkit::print.stored['f', newln]; # 0.100000001490116119384765625
stdkit::print.stored['h', newln]; # 0.0999755859375Bin widths31 linesMAIN {
let 'a': uint8 = 12; # 0000 1100
let 'b': uint8 = 10; # 0000 1010
stdkit::print.useful['a' and.bit 'b', newln]; # 0000 1000 = 8
stdkit::print.useful['a' or.bit 'b', newln]; # 0000 1110 = 14
stdkit::print.useful['a' xor 'b', newln]; # 0000 0110 = 6Bitwise14 linesMAIN {
let 'v': uint32 = 5;
let 'keep': uint32 = 'v' and.bit.wrap (0 minus 1); # -1's bits are all ones: 'v' unchanged
let 'none': uint32 = 'v' and.bit.wrap (1 minus 7); # -6's bits and 5's: 0
let 'flip': uint32 = 'v' xor.wrap (1 minus 7); # 4294967295
stdkit::print.useful['keep', " ", 'none', " ", 'flip', newln];Bitwise wrap9 linesMAIN {
let 'correct?': bool = true;
let 'wrong?' := not 'correct?';
if 'correct?' and (not 'wrong?') {
stdkit::print.stored["fine", newln];
}Bools10 linesfn fill ['count': int32] -> list[int32][0, 5] or fail[too_many] {
if 'count' > 5 { fail too_many; }
let mut 'out' := list.empty;
for 'i' in range[from 1, to 'count', by 1] { list.append[lend.mut 'out', 'i' times 'i']; }
return give 'out';
}Bounded21 linesMAIN {
let mut 'small': map[str.utf8[1, 3 bytes], int32[0, 10]][0, 2] = map.empty;
map.set[lend.mut 'small', "ab", 5];
map.set[lend.mut 'small', "cd", 7];
let 'n' := map.length[lend 'small']; # uint64[0, 2]
let 'tiny': int8 = 'n'; # proven by the boundBounded map12 linesMAIN {
let 'a': brain16 = 0.1; # a literal rounds to the nearest: 0.10009765625
let 'b': brain16 = 0.2;
let 'c': brain16 = 'a' plus 'b';
stdkit::print.stored['a', " ", 'c', newln]; # what is stored
stdkit::print.useful['a', " ", 'c', newln]; # the shortest text that rounds back to itBrain1616 linesMAIN {
let 'i': int64 = 9223372036854775803;
let 'b' := to.brain16['i'];
let 'd' := to.useful.deci64['b'];
let 'n' := to.int64['d'] otherwise -1;
stdkit::print.useful['b', " ", 'd', " ", 'n', newln];Brain16 useful large9 linesMAIN {
let 'good': list[uint8] = [104, 195, 169, 108, 108, 111];
let 'bad': list[uint8] = [104, 255, 105];
let 'a' := to.str.utf8['good'] otherwise "?";
let 'b' := to.str.utf8['bad'] otherwise "not UTF-8";
stdkit::print.stored['a', newln, 'b', newln];Bytes to text10 lineslayout.C enum Key { a = 65 b = 66 escape = 256 }
extern.C fn IsKeyDown ['key': Key] -> bool;
fn name ['k': Key] -> str.utf8 {
match 'k' {
a { return "A"; }
b { return "B"; }C enums19 lineslayout.C enum Key { a = 65 b = 66 escape = 256 }
fn on_key ['code': int32] -> int32 {
let 'k' := to.Key['code'] otherwise { out_of_range { return 0; } };
match 'k' {
a { return 1; }
b { return 2; }C enum convert16 lineslayout.C struct Rect { w: int32[0, 4096] h: int32[0, 4096] }
extern.C fn DrawRect ['r': Rect];
MAIN {
let 'r' := Rect[w = 640, h = 480];
stdkit::print.useful['r'.w, "x", 'r'.h, " ", size.of[Rect], newln];
}C struct ranges9 lineslayout.C enum Mode { emacs = 0 vi = 1 }
extern.C fn malloc ['bytes': uint64] -> ptr.mut[Mode];
extern.C mut 'edit_mode': int32;
fn mode_at ['p': ptr[Mode]] -> int32 {
no_check let 'm' := ptr.read['p'] otherwise Mode.emacs;
match 'm' {C variables25 linesextern.C fn qsort ['base': ptr.mut[int32], 'n': uint64, 'size': uint64,
'cmp': fn[ptr[int32], ptr[int32]] -> int32];
fn ascending ['a': ptr[int32], 'b': ptr[int32]] -> int32 {
no_check return ptr.read['a'] minus ptr.read['b']; # C hands pointers to two elements
}
fn descending ['a': ptr[int32], 'b': ptr[int32]] -> int32 {Callbacks19 linescase.insensitive;
const 'Limit' := 3;
struct Point {
X: int32
Y: int32
}Case file18 linesenum Status { empty claimed: str.utf8 locked }
fn label ['s': Status] -> str.utf8 {
let immut 'text' := match 's' {
empty { is "free"; }
claimed 'who' { let immut 'prefix' := "taken by "; is 'prefix' ++ 'who'; }
locked { is "locked"; }is gives a case its value14 linescase.insensitive [fn, var];
case.insensitive enum Mood {
Happy
Grumpy
}
case.sensitive struct Exact {Case marks23 linescase.insensitive [fn, var];
fn Größe [] -> int32 {
return 7;
}
MAIN {
let 'Café' := GRÖSSE[];Case unicode12 linesMAIN {
let 'a' := `A`;
stdkit::print.useful[char.code['a'], newln]; # 65 — the number under it
let 'seven' := to.int32[to.str.utf8[`7`]] otherwise -1; # 7 — a digit's value is a text parse
stdkit::print.useful['seven', newln];
let 'b' := char.point.from_code[66] otherwise `?`;Chars19 linesfn draw ['y': bin16] { stdkit::print.useful["drawing ", 'y', newln]; }
MAIN {
let 'x': bin32 = 70000.0;
if 'x' > 65_504.0 {
stdkit::print.stored["too big", newln];
} else {Check tiers25 linesMAIN {
let 'a' := 1; # trailing: from here to the end of the line
let 'b' := 2; #wl this whole line is a comment, including the code before the #
let 'c' := 3;
let 'd' := 4;
let 'e' := 5; #2ld this line and two more downComments12 linesfn fetch [lent 'url': str.utf8] -> str.utf8 { return "page of " ++ 'url'; }
MAIN {
let 'results' := channel[str.utf8];
let 'a' := "a.com";
concur with [lend 'results', give 'a'] { channel.send[lend 'results', fetch[lend 'a']]; }
concur with [lend 'results'] { channel.send[lend 'results', fetch["b.com"]]; }Concurrency15 linesbare.never [fn, struct];
pub const 'max' := 8;
const 'pi': bin64 = 3.141_592_653_589_793;
const 'twice_max' := 'max' times 2;
const 'greeting' := "hello " ++ "world";
pub struct P { x: int32 }Consts15 linesfn g ['p': bool] -> uint8 { return 0; }
MAIN {
let 'x': int64 = 0;
let 'v': uint8 = g[0 > (-1) minus 'x']; # the (-1) minus 'x' is int64 arithmetic, not uint8
let 'w': uint32 = 2 shl 3; # but a bare literal still takes its width from here
stdkit::print.useful['v', " ", 'w', newln];Context stops at a call9 linesMAIN {
let 'w': uint32 = 3;
let 'b': bool = (5 minus.wrap 'w') > 1; # a uint32 subtraction, 'w' says so, not a bool one
let 'c': bool = 1 < (0 minus.wrap 'w'); # wraps to 4294967293
let 'n': uint64 = 5;
let 'd': bool = 'n' > (0 minus.wrap 1); # but the other side sizes it: 0 minus.wrap 1 wraps in uint64Context stops at a comparison10 linesMAIN {
for 'i': uint64 in range[from 3, to 1, by -1] { stdkit::print.useful['i', " "]; }
stdkit::print.stored[newln];
for 'j': uint64 in range[from 3, to 0, by -1] { stdkit::print.useful['j', " "]; } # ends at 0: must not underflow
stdkit::print.stored[newln];
let 'xs' := [10, 20, 30];Countdown11 linesMAIN {
let 'a': deci64 = 0.1;
let 'b': deci64 = 'a' plus 0.2;
stdkit::print.stored['b', " ", 'b' equal_to 0.3, newln];
let 'price': deci64 = 19.99;
let 'sub': deci64 = 'price' times 3;deci: exact decimals37 linesMAIN {
let 'a': deci64 = 1.00;
stdkit::print.stored['a' plus (1.5 times 2), " ", 'a' plus 1.5 times 2, newln];
let 'f': bin32 = 0.1;
stdkit::print.stored[to.useful.deci64['f'], " ", to.stored.deci64['f'], newln];
let mut 'm': map[deci64, int32] = map.empty;deci beside other numbers12 linesMAIN {
let 't': deci64 = 7.50;
let 'm': deci64 = -7.50;
stdkit::print.stored['t' mod.floor 2, " ", 'm' mod.floor 2, " ", 't' mod.floor -2, newln];
let 'r': deci64 = 1.05;
stdkit::print.stored['r' pow 10, " ", 'r' pow 0, " ", 'r' pow -1, newln];deci, continued22 linesfn g ['x': uint32] -> uint32 {
return 'x';
}
MAIN {
let 'v': uint32 = g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g[g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]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]];
stdkit::print.useful['v', newln];Deep calls10 linesMAIN {
let 'x': bin64 = 0.00000000000000000001;
let 'n': bin64 = -0.00000000000000000001;
let 'two': bin64 = 2.0;
let 'turn': bin64 = 360.0;
stdkit::print.useful[('x' div.ceil 'two'), " ", ('x' mod.ceil 'two'), " ", ('n' div.floor 'turn'), " ", ('n' mod.floor 'turn'), newln];Div edges17 linesMAIN {
let 'a': int8 = -128;
stdkit::print.useful[('a' div.trunc -1), " ", ('a' div.floor -1), " ", ('a' mod.floor -1), newln];
let 'b': int16 = 'a' div.near -1; # lands in an int16: fits
stdkit::print.useful['b', newln];
}Div lands8 linesMAIN {
stdkit::print.useful[(7 div.floor 2), " ", (7 div.ceil 2), " ", (7 div.trunc 2), " ", (7 div.near 2), newln];
stdkit::print.useful[(-7 div.floor 2), " ", (-7 div.ceil 2), " ", (-7 div.trunc 2), " ", (-7 div.near 2), newln];
stdkit::print.useful[(7 mod.floor 2), " ", (7 mod.ceil 2), " ", (7 mod.trunc 2), " ", (7 mod.near 2), newln];
stdkit::print.useful[(-7 mod.floor 2), " ", (-7 mod.ceil 2), " ", (-7 mod.trunc 2), " ", (-7 mod.near 2), newln];
stdkit::print.useful[(5 div.near 2), " ", (5 mod.near 2), newln]; # 2.5 is a tie: to the even 2, 1 leftDiv rounding20 linesMAIN {
let 'a': int8 = -128;
stdkit::print.useful[('a' div.trunc.wrap -1), " ", ('a' div.floor.wrap -1), " ", ('a' div.trunc -1), newln];
let 'm': int128 = -170141183460469231731687303715884105728;
stdkit::print.useful[('m' div.trunc.wrap -1), " ", ('m' div.near.wrap 2), newln];
}Div wrap9 linesMAIN {
stdkit::print.useful[7 div.floor 2, " ", 7 mod.floor 2, newln];
stdkit::print.useful[-7 div.floor 2, " ", -7 mod.floor 2, newln]; # -4 and 1: the clock never goes negative
stdkit::print.useful[7 div.floor -2, " ", 7 mod.floor -2, newln]; # -4 and -1: the result has the divisor's sign
let 'hour' := -1;
stdkit::print.useful[('hour' mod.floor 12) plus 1, newln]; # an hour before 1 o'clock is 12Division10 linesfn twice ['d': duration] -> duration {
return 'd' plus 'd';
}
MAIN {
let 'w': duration = 2 seconds;
stdkit::print.useful[twice['w'] equal_to 4 seconds, newln];Duration bare9 linesMAIN {
let 'xs': list[int32] = [];
list.append[lend.mut 'xs', 7];
list.append[lend.mut 'xs', 8];
stdkit::print.useful['xs', " ", list.length[lend 'xs'], newln];
let 'ys' := list.empty;Empty10 linesenum Status {
empty
claimed: str.utf8
locked
}
struct Locker {Enums & match33 linesMAIN {
let 'price' := *19.99;
let 'tenth' := *0.1;
let 'total' := 'tenth' plus *0.2; # exactly 0.3
let 'half': deci32 = *0.5; # a typed deci takes it at its width
let 'speed' := 0.1; # a binExact literal10 linesbare.never [fn];
module text {
extern.C {
pub fn strlen ['s': ptr[uint8]] -> uint64;
fn write ['fd': int32, 'buf': ptr[uint8], 'len': uint64] -> int64;
}Extern block pub23 linesextern.C 'lib_version': int32; # C: extern int lib_version;
extern.C mut 'lib_verbose': int32; # C: extern int lib_verbose; (we may write it)
MAIN {
no_check let 'v' := 'lib_version';
stdkit::print.useful["version ", 'v', newln];
no_check change 'lib_verbose' = 1;Extern data11 linesextern.C fn strlen ['s': ptr[uint8]] -> uint64[0, 10]; # C promises at most 10; checked at the door
MAIN {
let 'short' := "hello";
let 'long' := "this is longer than ten";
no_check let 'a' := strlen[lend 'short'] otherwise 99;
no_check let 'b' := strlen[lend 'long'] otherwise 99; # 23 is outside 0..10: fails out_of_rangeExtern range12 linesstruct Locker {
owner: str.utf8
items: list[str.utf8]
}
fn stash [lent.mut 'l': Locker, 'item': str.utf8] -> bool or fail[full, bad_item: str.utf8] {
if list.length[lend 'l'.items] at_least 2 { fail full; }Failures43 linesMAIN {
let 'x': deci64 = 3.141592653589793;
let 'y' := to.deci32['x'] otherwise 0;
let 'z' := to.int64['y' times 1000000] otherwise -1;
let 'b': bin64 = to.bin64["not a number"] otherwise 0;
stdkit::print.stored['y', " ", 'z', " ", 'b', newln];Fallback type9 linesextern.C fn write ['fd': int32, 'buf': ptr[uint8], 'len': uint64] -> int64;
extern.C fn strlen ['s': ptr[uint8]] -> uint64;
extern.C fn getenv ['name': ptr[uint8]] -> ptr[uint8];
extern.C fn malloc ['bytes': uint64] -> ptr.mut[uint8];
extern.C fn free ['p': ptr.mut[uint8]];
layout.C struct Timeval {Talking to C35 linesfn say ['n': int32] {
let 'by3' := ('n' mod.floor 3) equal_to 0;
let 'by5' := ('n' mod.floor 5) equal_to 0;
if 'by3' and 'by5' {
stdkit::print.stored["FizzBuzz", newln];
} else if 'by3' {FizzBuzz22 linesMAIN {
let 'xs': list[int32] = [1, 2];
for 'x' in lend 'xs' {
let 'v': int64 = to.int64['x' plus.wrap 10];
let 'w': int64 = to.int64[not.bit 'x'];
stdkit::print.useful['v', " ", 'w', newln];For item type12 linesMAIN {
let mut 'prices': list[int32] = [10, 20, 30];
for 'x' in lend.mut 'prices' {
change 'x' = 'x' times 2;
}
stdkit::print.stored['prices', newln];for over lend.mut10 linesMAIN {
let mut 'xs': list[int32] = [1, 2, 3];
let mut 'seen': list[int32] = [];
for 'x' in lend.mut 'xs' {
change 'x' = 'x' times 10;
list.append[lend.mut 'seen', 'xs'[pos 1]];Reading during a lend.mut walk12 linesMAIN {
let mut 'acc': int64 = 0;
for no_cheat 'i': int64 in range[from 1, to 1000, by 1] {
change 'acc' = 'acc' plus 'i';
}
let mut 'more': int64 = 0;for no_cheat: no cheating14 linesMAIN {
let 'xs': list[int64] = [1, 2, 3, 4];
let mut 'total': int64 = 0;
for para 'x' in lend 'xs' {
change 'total' = 'total' plus 'x';
}for para and for seq13 linesMAIN {
let 'xs' := ["ann", "bob", "cy"];
for 'i', 'x' in lend 'xs' {
stdkit::print.useful['i', ". ", 'x', newln];
}
let 'scores' := [70, 95, 88];Position and item17 linesMAIN {
let 'n' := 3;
let 'line' := format["{} items at {:.2} each, 0x{:x}\n", 'n', 1.5, 255];
print.useful['line'];
print.weird.useful <- "hex: " <- format["{:x}", 'n' times 100] <- newln;
print.useful.stdout["said ", "stdout", newln];Format value10 linesstruct Locker { number: int32 owner: str.utf8 }
fn by_number [lent 'a': Locker, lent 'b': Locker] -> bool { return 'a'.number < 'b'.number; }
fn by_owner [lent 'a': Locker, lent 'b': Locker] -> bool { return str.bytes[lend 'a'.owner] < str.bytes[lend 'b'.owner]; }
fn first_by [lent 'xs': list[Locker], 'less': fn[lent Locker, lent Locker] -> bool] -> str.utf8 {
let 'best' := 1;
for 'i' in range[from 2, to list.length[lend 'xs'], by 1] {Functions as values38 linesinterface Number <T> {
op minus ['a': <T>, 'b': <T>] -> <T>;
op < ['a': <T>, 'b': <T>] -> bool;
}
op ~= <T: Number> ['a': <T>, 'b': <T>] -> bool {
if 'a' < 'b' { return 'b' minus 'a' < 10; }Generic op22 linesinterface Number {
int32
int64
bin64
}
fn max <T: Number> ['a': <T>, 'b': <T>] -> <T> {Generics39 linespub const 'passing' := 60;
struct Student {
name: str.utf8
scores: list[int32]
}
fn average [lent 's': Student] -> int32 or fail[no_scores] {Gradebook46 linesMAIN {
stdkit::print.stored["Hello, World!", newln];
}Hello, World3 linesMAIN {
let 'text' := stdkit::input.line[] otherwise "(no input)"; # a boundary: fails with `closed`
stdkit::print.stored["got: ", 'text', newln];
let 'n' := to.int32[0, 100]['text'] otherwise 50;
stdkit::print.useful["n = ", 'n', newln];
}Input10 linesinterface Number {
int32
int64
bin64
}
interface Ordered <T> {Interface types41 linesinterface Ordered <T> {
op < ['a': <T>, 'b': <T>] -> bool;
op equal_to ['a': <T>, 'b': <T>] -> bool;
}
interface Printable <T> {
fn show [lent 'x': <T>] -> str.utf8;Interfaces39 linesstruct Item { name: str.utf8 grams: int32[1, 50_000] }
fn heaviest [...'items': Item] -> Item or fail[none] {
if list.length[lend 'items'] equal_to 0 { fail none; }
let mut 'best' := list.remove[lend.mut 'items', pos 1];
for 'it' in lend 'items' {
if 'it'.grams > 'best'.grams { change 'best' = copy 'it'; }Inventory44 linesMAIN {
let 'n' := 3;
let 's' := "n=" ++ to.stored.str.utf8['n' plus 4]; # arithmetic, convert, then join
stdkit::print.stored['s', newln];
stdkit::print.stored["count: " ++ to.stored.str.utf8[10], newln]; # a number is converted, in its stored form
let 'x': bin64 = 0.1;Join13 linesinterface Key <T> { op equal_to ['a': <T>, 'b': <T>] -> bool; fn hash [lent 'v': <T>] -> uint64; }
struct Point { x: int32 y: int32 } # structural: fields decide
struct User { name: str.utf8 } # its own rule: names compare lowercased
op equal_to ['a': User, 'b': User] -> bool { return stdkit::str.lower[lend 'a'.name] equal_to stdkit::str.lower[lend 'b'.name]; }
fn hash [lent 'v': User] -> uint64 { let 'low' := stdkit::str.lower[lend 'v'.name]; return str.bytes[lend 'low']; }
User satisfies Key;Keys22 linesfn to [] -> int32 { return 5; }
fn true [] -> int32 { return 1; }
MAIN {
let 'x' := my.to[];
let 'y' := my.true[];
stdkit::print.useful['x', " ", 'y', newln];Keyword fn9 linesfn pick [@x] [lent 'a': list[int32], lent @x 'b': list[int32]] -> lent @x list[int32] {
return lend 'b';
}
MAIN {
let 's': list[int32] = [1, 2];
let 'r' := pick[lend 's', lend 's'];Lent twice10 linesfn longer [@x] [lent @x 'a': str.utf8, lent @x 'b': str.utf8] -> lent @x str.utf8 {
if str.bytes[lend 'a'] at_least str.bytes[lend 'b'] { return lend 'a'; }
return lend 'b';
}
fn pick [@x, @y] [lent @x 'a': str.utf8, lent @y 'b': str.utf8] -> lent @x str.utf8 {
return lend 'a';Lifetimes: @x24 linesMAIN {
let mut 'l': list[int32] = [1, 2];
list.append[lend.mut 'l', 3];
let 'a' := to.array[give 'l']; # [1, 2, 3], can no longer grow
let mut 'm' := to.list[copy 'a']; # 'a' is still here
list.append[lend.mut 'm', 4];list and array13 linesMAIN {
let mut 'scores' := list.fill[value 0, count 5];
change 'scores'[pos 2] = 7;
stdkit::print.stored['scores', newln];
let 'n': int64 = 3;
let 'flags': list[bool] = list.fill[value false, count 'n'];list.fill12 linesMAIN {
let 'i': uint8 = 5;
let 'xs': list[uint8] = [0 minus.wrap 1, 0 minus.wrap 'i', 250 plus.wrap 10];
let 'ys': list[uint32] = [2 shl 3, not.bit 0];
let 'zs': list[list[uint8]] = [[0 minus.wrap 2,], [1,]];
let 'big': list[uint64] = [9692729768110883998 minus 3166240359165959856,];List item context12 linesMAIN {
let 'bs': list[bin64] = [1, 2];
let 'more': list[bin64] = list.fill[value 1, count 3];
let 'ds': list[deci64] = list.fill[value 0.25, count 3];
stdkit::print.stored['bs', newln];
stdkit::print.stored['more', newln];List literal family10 linesMAIN {
let 'xs' := [1, 2.5];
let 'ys': list[bin64] = [2.5, 1];
stdkit::print.useful['xs', newln];
stdkit::print.useful['ys', newln];
stdkit::print.useful['xs'[pos 1] plus ('xs'[pos 2] times 2.0), newln];List literal mixed14 linesfn count <T: any> [lent 'items': list[<T>]] -> int32 {
let 'n' := 0;
for 'item' in lend 'items' {
change 'n' = 'n' plus 1;
}
return 'n';Lists18 linesfn sum [lent 'items': list[int32]] -> int32 {
let 'total' := 0;
for 'n' in lend 'items' {
change 'total' = 'total' plus 'n';
}
return 'total';Lists231 linespub const 'capacity' := 12;
struct Locker {
immut number: int32
owner: str.utf8
items: list[str.utf8]
}Lockers43 linesfn double ['n': int32] -> int32 { return 'n' times 2; }
fn apply ['v': int32, 'f': fn[int32] -> int32] -> int32 { return 'f'['v']; }
MAIN {
stdkit::print.useful[double[4], " ", apply[5, double], newln];
stdkit::print.weird.useful <- "weird: " <- double[2] <- newln;
}Long arrow7 linesMAIN {
let 'i' := 0;
while 'i' < 3 {
stdkit::print.useful["while ", 'i', newln];
change 'i' = 'i' plus 1;
}Loops23 linesextern.C {
fn write ['fd': int32, 'buf': ptr[uint8], 'len': uint64] -> int64;
fn malloc ['bytes': uint64] -> ptr.mut[uint8];
fn free ['p': ptr.mut[uint8]];
}
layout.C struct Header { magic: uint32 flags: uint8 length: uint64 }Low level56 linesenum E1 { c1: int32 c2: uint8 c3 }
MAIN {
let 'e' := E1.c1[5];
match 'e' {
c1 'd' { stdkit::print.useful[not.bit 'd', " ", 'd' times.wrap 1000000000, newln]; }
c2 'b' { }A match arm's data14 linesenum Status { empty claimed: str.utf8 locked }
fn label ['s': Status] -> str.utf8 {
let 'text' := match 's' {
empty { is "free"; }
claimed 'who' { is "taken by " ++ 'who'; }
locked { return "(locked)"; }match gives a value21 linesMAIN {
let 'angle': bin64 = -370.0;
let 'turn': bin64 = 360.0;
let 'a': bin64 = 7.5;
let 'b': bin64 = -2.0;
let 'h': brain16 = -5.0;Mod bin15 linesbare.never [fn, struct];
const 'scale' := 10;
module counter {
pub const 'start' := 1;
pub struct Count { n: int32 }
pub fn bump ['c': Count] -> Count {Modules29 linesfn twice [immut 'n': int32] -> int32 {
return 'n' times 2;
}
MAIN {
let 'plain' := 1;
change 'plain' = 'plain' plus 1;Mutability12 linesenum Order { cancelled paid }
enum Ship { pickup post }
MAIN {
let 'o' := Order.paid;
let 's' := Ship.post;
match 'o' {Nested match14 linesextern.C fn write ['fd': int32, 'buf': ptr[uint8], 'len': uint64] -> int64;
MAIN {
let 'msg' := "hello";
NO_FUCKING_CHECKS let 'n' := write[1, lend 'msg', str.bytes[lend 'msg']];
NO_FUCKING_CHECKS {
let 'a': int32 = 10;NO_FUCKING_CHECKS14 linesextern.C fn write ['fd': int32, 'buf': ptr[uint8], 'len': uint64] -> int64;
fn emit ['m': uint64, 'neg': bool] {
let mut 'buf': list[uint8] = [];
if 'neg' { list.append[lend.mut 'buf', 45]; }
let mut 'v': uint64 = 'm';
let mut 'ds': list[uint8] = [];Not bit fill45 lineson.target.os windows {
fn where [] -> str.utf8 { return "windows"; }
} else {
fn where [] -> str.utf8 { return "not windows"; }
}
MAIN {On target26 linesMAIN {
let 'pos': int32[1, max] = 1;
let 'score': int32[min, 100] = 50;
let 'small': int8[1, max] = 120;
let 'whole': uint8[0, 255] = 200; # the whole type: harmless, allowed (owner)
let 'also': int32[min, max] = -7;One ended9 linesstruct Vec { x: bin64 y: bin64 }
op dot ['a': Vec, 'b': Vec] -> bin64 { return 'a'.x times 'b'.x plus 'a'.y times 'b'.y; }
fn fold [lent 'xs': list[int32], 'start': int32, 'f': fn[int32, int32] -> int32] -> int32 {
let mut 'acc' := 'start';
for 'x' in lend 'xs' { change 'acc' = 'f'['acc', 'x']; }
return 'acc';Operators as values27 linesstruct Vec { x: bin64 y: bin64 }
op <+> ['a': Vec, 'b': Vec] -> Vec {
return Vec[x = 'a'.x plus 'b'.x, y = 'a'.y plus 'b'.y];
}
op dot ['a': Vec, 'b': Vec] -> bin64 {
return 'a'.x times 'b'.x plus 'a'.y times 'b'.y;Operators30 linesMAIN {
let 'small': int16 = 5;
let 'big': int64 = (20000 plus 20000) plus 'small'; # 40005: the true answer, and it fits int64
let 's': int16 = 30000;
let 'twice': int64 = 's' plus 's'; # 60000, though no int16 holds it
let 'tiny': int8 = (100 plus 100) minus 150; # 50: the 200 in between is fineOverflow sites21 linesfn count [lent 'items': list[str.utf8]] -> int32 {
let 'n' := 0;
for 'item' in lend 'items' {
change 'n' = 'n' plus 1;
}
return 'n';Ownership34 linesMAIN {
let 'xs' := [1, 2, 3];
let 'count' := fn [] -> uint64 with [copy 'xs'] { return list.length[lend 'xs']; };
let 'again' := copy 'count'; # its own copy of the list inside
let 'moved' := give 'count'; # 'count' is gone after this
let 'inc' := fn ['a': int32] -> int32 { return 'a' plus 1; };Owning function12 linesMAIN {
let mut 'phones': map[str.utf8, int32] = map["ann" = 5551, "bob" = 5552]; # a literal: key = value
map.set[lend.mut 'phones', "ann", 5553]; # same key: replaced
stdkit::print.useful[map.length[lend 'phones'], " entries", newln];
let 'a' := map.get[lend 'phones', "ann"] otherwise 0;
let 'c' := map.get[lend 'phones', "cat"] otherwise 0; # missing → 0Phonebook23 linesfn pi_digits ['n': int32] {
let 'len' := (10 times 'n') div.floor 3 plus 1;
let mut 'a' := list.empty;
for 'i' in range[from 1, to 'len', by 1] { list.append[lend.mut 'a', 2]; }
let mut 'nines' := 0;
let mut 'predigit' := 0;Digits of pi36 linesMAIN {
stdkit::print.useful[(2 pow (3 pow 2)), newln];
stdkit::print.useful[((2 pow 3) pow 2), newln];
let 'kilo' := 2 pow 10;
stdkit::print.useful['kilo', newln];
stdkit::print.useful[(10 minus 3 minus 2), newln];Power7 linesMAIN {
stdkit::print.useful[(1 plus 2 times 3), newln];
stdkit::print.useful[(2 times (3 pow 2)), newln];
stdkit::print.useful[-(2 pow 2), newln];
stdkit::print.useful[((-2) pow 2), newln];
stdkit::print.useful[(10 minus 4 minus 3), newln];Precedence14 linesstruct Vec { x: bin64 y: bin64 }
precedence.low_first {
level.when_chained.refused 1 { equal_to ~= }
level.when_chained.left_first 2 { plus }
level.when_chained.left_first 3 { times dot }
level.when_chained.left_first 4 { scale }Precedence levels33 linesMAIN {
stdkit::print.useful.auto_newln["Hello, World!"];
let 'n' := 3;
stdkit::print.useful.auto_newln["n is ", 'n'];
stdkit::print.useful["the same, by hand: ", 'n', newln];
}print.useful.auto_newln7 linesMAIN {
let 'name' := "rope";
let 'count' := 3;
let 'price': bin64 = 12.5;
let 'pay': deci64 = 37.5 times 12.40;
let 'tenth': bin64 = 0.1;print.format19 linesMAIN {
let 'x': bin64 = 0.1;
stdkit::print.stored['x', newln];
stdkit::print.useful['x', newln];
let 'y': bin16 = 1000;
stdkit::print.useful['y', newln];print vs print.useful13 linesMAIN {
let 'tenth': bin64 = 0.1;
let 'n' := 3;
stdkit::print.weird.useful <- "tenth is " <- 'tenth' <- ", n is " <- 'n' <- newln;
stdkit::print.weird.stored <- "stored: " <- 'tenth' <- newln;
}print.weird10 linesMAIN {
let 'name' := "Ann";
let 'count' := 6;
let 'x': bin64 = 0.1;
stdkit::print.stored["x is ", 'x', newln];
stdkit::print.useful["x is ", 'x', newln];Prints20 linesextern.C fn malloc ['bytes': uint64] -> ptr.mut[uint8];
extern.C fn free ['p': ptr.mut[uint8]];
layout.C struct Point {
x: int32
y: int32
}ptr.cast18 linesstruct Job { name: str.utf8 }
fn run ['job': Job] { stdkit::print.useful["ran ", 'job'.name, newln]; }
MAIN {
let mut 'queue' := [Job[name = "a"], Job[name = "b"], Job[name = "c"]];
run[list.remove[lend.mut 'queue', pos 1]]; # straight into a call
let 'next' := list.remove[lend.mut 'queue', pos 1]; # or keptQueue12 linesMAIN {
for 'i' in range[from 1, to 3, by 1] {
stdkit::print.useful['i', " "];
}
stdkit::print.stored[newln];
for 'k' in range[from 0, to 10, by 5] {Range33 linesstruct Percent { value: uint8[0, 100] }
fn scale ['p': Percent, 'total': int32[0, 1000]] -> int32[0, 1000] {
return ('total' times to.int32['p'.value]) div.floor 100;
}
MAIN {
let 'half' := Percent[value = 50];Ranges24 linesenum Pick { quick slow }
fn f [] -> uint64 {
let 'v': uint16 = match Pick.quick {
quick { return 8997704354644785536 shr 42; }
slow { is 1; }
};return inside a match15 linesfn is_even ['n': int32] -> bool {
return ('n' mod.floor 2) equal_to 0;
}
fn clamp ['n': int32, 'low': int32, 'high': int32] -> int32 {
if 'n' < 'low' {
return 'low';Scores49 linesMAIN {
let immut 'a' := 1;
shadow 'a' := 2; # an immut can be shadowed: nothing is changed
let 'b' := 3; # no word: it may not change either
let no_shadow mut 'total' := 0; # changeable, and 'total' always means this one
change 'total' = 'total' plus 'a' plus 'b';Sealed12 linesMAIN {
let 'x' := 1;
if true {
shadow 'x' := 2;
stdkit::print.useful['x', newln];
}shadow10 linesMAIN {
let 'xs': list[uint8] = [10, 20, 30,];
stdkit::print.useful[(list.length[lend 'xs'] shl 1) > 5, " ", ('xs'[pos 1] shr 2) equal_to 2, newln];
}Shift length item6 linesMAIN {
let 'b': uint8 = 255;
let 'x': int64 = 1;
let 'up': uint8 = 'b' shl.masked 1; # 510 cut to 8 bits: 254
let 'far': int64 = 'x' shl.masked 70; # 70 masked to 6: 64
let 'down': uint8 = 'b' shr.masked 9; # 9 masked to 1: 127Shift masked12 linesfn g [] -> int8 { return 10; }
MAIN {
if 3 > (g[] shl 1) { stdkit::print.useful["yes", newln]; } else { stdkit::print.useful["no", newln]; }
}Shl call width6 linesinterface Priced <T> {
fn price [lent 'x': <T>] -> int32;
}
enum Stock {
out
few: int32Shop64 linesfn double ['n': int32] -> int32 { return 'n' times 2; }
MAIN {
stdkit::print.weird.useful <- double[3] <- newln;
}Short arrow6 linesfn is_ascii [lent 'bytes': array[uint8]] -> bool {
let mut 'acc': simd[uint8, 16] = simd.fill[value 0];
for 'i' in range[from 1, to array.length[lend 'bytes'], by 16] {
change 'acc' = 'acc' or.bit simd.load[lend 'bytes', pos 'i'];
}
return simd.max['acc'] < 128;SIMD23 linesstruct Particle { place: simd[bin32, 4] vel: simd[bin32, 4] }
fn step ['p': Particle] -> Particle {
return Particle[place = 'p'.place plus 'p'.vel, vel = 'p'.vel];
}
MAIN {
let 'p' := Particle[place = simd.fill[value 0.0], vel = simd.fill[value 0.5]];SIMD fields15 linesMAIN {
let 's': simd[uint8, 16] = simd.fill[value 3];
stdkit::print.useful[simd.max['s'] shl 1, newln];
}SIMD fold shift6 linesMAIN {
let 'a': simd[int32, 4] = simd.fill[value 4];
let 'x': int32 = 'a'[pos 1];
let 'u': simd[uint8, 16] = simd.fill[value 200];
let 'w' := 'u' plus.wrap 'u';
stdkit::print.stored['x', " ", 'w'[pos 1], " ", 'w'[pos 16], newln];SIMD lanes13 linesfn count_on ['m': simd[bool, 4]] -> bool {
return simd.all['m'];
}
MAIN {
let 'xs': array[int32] = [5, 20, 30, 40];
let 'a': simd[int32, 4] = simd.load[lend 'xs', pos 1];SIMD mask15 linesMAIN {
on.target.chip x86_64 {
let 'w': simd[int32, 8] = simd.fill[value 1];
} else {
let 'v': simd[int32, 4] = simd.fill[value 1];
print.useful[simd.sum['v'], newln];SIMD ruled out11 linesMAIN {
let 'xs': array[int32] = [10, 20, 30, 40];
let 'v': simd[int32, 4] = simd.load[lend 'xs', pos 1];
let 'r' := simd.shuffle['v', [4, 3, 2, 1]]; # reversed
let 'pairs' := simd.shuffle['v', [2, 1, 4, 3]]; # neighbours swapped
let 'first' := simd.shuffle['v', [1, 1, 1, 1]]; # lane 1 everywhereSIMD shuffle11 linesMAIN {
let 'v': simd[int32, 4] = simd.fill[value 1200000000];
let 's': int64 = simd.sum['v'];
let 'w': int32 = simd.sum.wrap['v'];
let 'u': simd[int32, 4] = simd.fill[value -500000000];
let 'l': int32 = simd.sum.lane['u'];SIMD sum forms11 linesMAIN {
let mut 'xs': list[int32] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let 'n' := list.length[lend 'xs'];
let mut 'acc': simd[int32, 4] = simd.fill[value 0];
let mut 'i': uint64 = 1;
while 'i' at_most 'n' {SIMD tail25 linesMAIN {
let 'xs' := [10, 20, 30, 40, 50];
let 'mid' := list.slice[lend 'xs', pos 2, pos 4];
stdkit::print.useful['mid', " ", list.length[lend 'mid'], newln];
let 'fixed': array[int32] = [1, 2, 3];
stdkit::print.useful[array.slice[lend 'fixed', pos 1, pos 2], newln];Slicing11 linesfn ratio ['n': int32[1, max]] -> int32 { return 100 div.floor 'n'; }
MAIN {
let immut 'xs': list[int32] = [10, 20, 30, 40];
let mut 'total': int32 = 0;
for temp 'i': uint64 in range[from 1, to 4, by 1] {
change 'total' = 'total' plus 'xs'[pos 'i'];Written out15 linesfn sum [...'nums': int32] -> int32 {
let mut 'total' := 0;
for 'n' in lend 'nums' { change 'total' = 'total' plus 'n'; }
return 'total';
}
fn show [lent 'label': str.utf8, lent ...'parts'] {Spread33 linesMAIN {
let 's' := "a b c";
let 'parts' := str.split[lend 's', " "];
let 'same' := stdkit::str.split[lend 's', " "];
print.useful[list.length[lend 'parts'], " ", stdkit::list.length[lend 'same'], newln];
stdkit::print.useful[round[2.5], newln];Std bare10 linesMAIN {
let 'a': int32 = 2 stdkit::plus 3 stdkit::times 4;
let 'b': int32 = 7 stdkit::div.floor 2;
let 'c': uint32 = 1 stdkit::shl 3;
stdkit::print.useful['a', " ", 'b', " ", 'c', newln];
}Std op path9 linesMAIN {
stdkit::print.stored["a\nb", newln]; # newln inside quotes is a backslash and an n
stdkit::print.stored["say \"hi\"", newln]; # \" is the one escape that is needed
stdkit::print.stored["back\\slash", newln]; # and \\ for the backslash itself
stdkit::print.stored["tab here", newln]; # a tab is typed, not escaped
let 'nl' := newln; # the newline is the bare newln outside quotesStrings8 linesstruct Locker {
name: str.utf8
immut number: int32
capacity: int32
occupied: bool
}Structs37 linesfn add ['a': int32, 'b': int32] -> int32 { return 'a' plus 'b'; }
fn wrong ['a': int32, 'b': int32] -> int32 { return 'a' plus 'b' plus 1; }
test fn adds_up [] {
must add[2, 3] equal_to 5;
must add[2, 3] > 4;
}Tests: test fn and must27 linesMAIN {
let 's' := "héllo 👨👩👧 wörld";
stdkit::print.useful[str.bytes[lend 's'], " bytes, ", str.points[lend 's'], " points, ", str.graphemes[lend 's'], " graphemes", newln];
let 'vowels' := 0;
for 'g' in str.each.grapheme[lend 's'] {
if 'g' equal_to `o` or 'g' equal_to `e` { change 'vowels' = 'vowels' plus 1; }Text27 linesfn greet [lent 'name': str.utf8[1, 10 graphemes]] { stdkit::print.useful["hi ", 'name', newln]; }
MAIN {
let 'thai': str.utf8[1, 10 graphemes] = "เกี่ยว"; # 4 graphemes (เ|กี่|ย|ว), 6 points, 18 bytes: fits
let 'ascii': str.utf8[0, 5 bytes] = "hello"; # 5 bytes: fits
greet[lend 'thai'];
greet[lend 'ascii'];Text bound14 linesMAIN {
let 'a' := to.int32["42"] otherwise 0;
let 'b' := to.int32["99999999999"] otherwise -1; # too big for int32
let 'c' := to.int64["99999999999999999999999999999999999999999999"] otherwise -1; # too big for anything
let 'd' := to.uint8["-1"] otherwise 0; # below what uint8 holds
let 'e' := to.int32["12x"] otherwise -2; # not a number at allText to number16 linesMAIN {
let 'plain': str.utf8 = "hello";
let 'accented': str.utf8 = "héllo";
let 'a' := to.str.ascii['plain'] otherwise "?";
let 'b' := to.str.ascii['accented'] otherwise "not ascii";
stdkit::print.stored['a', newln, 'b', newln];To ASCII13 linesMAIN {
let 'a' := to.int8.bits[128]; # 128's bits in an int8: -128
let 'b' := to.uint8.bits[-1]; # -1's bits in a uint8: 255
let 'c': uint8 = 200;
let 'd' := to.int8.bits['c']; # 200's bits: -56
stdkit::print.useful['a', " ", 'b', " ", 'd', newln];To bits10 linesenum Size {
small
big
}
MAIN {
let 'x': bin64 = 0.1;To text forms20 linesMAIN {
let 'b': bin64 = 10000000000.0;
let 'd': deci128 = 99999999999999999999.0;
let 'small': deci64 = 42.0;
let 'x' := to.int32['b'] otherwise -1;
let 'y' := to.int64['d' times 1000] otherwise -2;Too big to int12 linesextern.C fn exit ['code': int32];
fn judge ['n': int32] -> fail[small] { if 'n' < 10 { fail small; } }
MAIN {
let mut 'seen': int32 = 0;
{
concur { judge[3] otherwise fail; } otherwise { small { change 'seen' = 'seen' plus 1; } };Two tasks fail12 linesfn sum [...'nums'] -> bin64 {
let 'total': bin64 = 0;
for 'n' in lend 'nums' { change 'total' = 'total' plus to.bin64['n']; }
return 'total';
}
fn count_ints [...'nums': int32] -> uint64 {Variadic24 linesfn fetch [lent 'url': str.utf8] -> str.utf8 or fail[no_network] {
if 'url' equal_to "down.com" { fail no_network; }
return "page of " ++ 'url';
}
MAIN {
let 'inbox' := channel[str.utf8][0, 10];whichever30 linesMAIN {
let 'x': bin64 = 2.0;
let 'y': bin64 = 2.7;
let 'a' := to.int32['x'] otherwise -1; # whole: 2
let 'b' := to.int32['y'] otherwise -1; # not whole: fails → -1
let 'c' := to.int32[stdkit::round['y']] otherwise -1; # said: 3Whole19 linesMAIN {
let 'a': not_std.uint256 = 2 pow 200 plus 12345;
let 'b': not_std.uint256 = (2 pow 255) minus 1;
stdkit::print.useful[('a' and.bit 'b'), newln];
stdkit::print.useful[('a' xor 'b'), newln];
stdkit::print.useful[('a' shl 55), " ", ('a' shr 190), newln];Wide bits18 linesMAIN {
let 'max': not_std.uint256 = 115792089237316195423570985008687907853269984665640564039457584007913129639935;
let 'half': not_std.uint256 = 'max' div.floor 2;
let 'top': not_std.uint256 = 2 pow 255;
stdkit::print.useful['half', newln];
stdkit::print.useful[('top' minus 1 plus 'top'), newln];Wide integers16 linesMAIN {
let 'small': not_std.int300 = 5;
let 'huge': not_std.int300 = 2 pow 200;
let 'a' := to.deci128['small'] otherwise 0.0;
let 'b' := to.deci128['huge'] otherwise 0.0;
stdkit::print.useful['a', " ", 'b', newln];Wide to deci9 linesMAIN {
let 'v': uint32 = 3;
let 'a': uint32 = 2 shl 3;
let 'b': uint32 = ('v' mod.floor 16) shl 2;
let 'c': uint32 = not.bit ('v' plus 1);
let 'd': uint32 = ('v' plus 1) plus.wrap 5;Widths10 linesMAIN {
let 'a': int32 = 7;
let 'b': int32 = 3;
stdkit::print.useful[('a' plus 'b'), " ", ('a' minus 'b'), " ", ('a' times 'b'), " ", ('a' pow 'b'), " ", ('a' stdkit::pow 2), " ", -'a', newln];
let 'h': uint8 = 200;
stdkit::print.useful[('h' plus.wrap 100), " ", ('h' times.wrap 2), " ", ('h' pow.wrap 2), newln];Word operators11 linesfn hash [lent 'text': str.utf8] -> uint32 {
let 'h': uint32 = 2_166_136_261; # FNV-1a
for 'b' in str.each.byte[lend 'text'] {
change 'h' = 'h' xor 'b'; # FNV-1a: xor, then a wrapping multiply
change 'h' = 'h' times.wrap 16_777_619; # wraps instead of overflowing
}Wrapping23 linesMAIN {
let 'z': bin64 = 0.0;
let 'n': bin64 = -2.0;
let 'e30': bin64 = 0.000000000000000000000000000001;
let 'e60': bin64 = 'e30' times 'e30';
let 'e120': bin64 = 'e60' times 'e60';Zero everywhere26 linesMAIN {
let 'q': bin64 = 0.5 div.ceil (-1.75);
let 'r': bin64 = 0.5 div.ceil.ieee (-1.75);
let 'b': brain16 = 0.5 div.trunc (-1.75);
stdkit::print.useful['q', " ", 'r', " ", 'b', " ", 'q' equal_to 'r', newln];
}Zero quotient8 linesModulesexamples/modules.cpls · 29 lines
Modules as blocks in one file (a Limb of one root file).