Strings and collections
Use case: build a log line
Section titled “Use case: build a log line”Interpolation uses ${…} inside double-quoted strings:
fn main(caps: Caps) -> int { let name = "Ada" let age = 36 print "${name} is ${age}" print "line1\n\t${1 + 2}" return 0}Prefer interpolation over chaining + for readable messages.
Use case: grow a list
Section titled “Use case: grow a list”// fragment-onlyfn main(caps: Caps) -> int { var xs: Array<int> = [1, 2] xs = array_push(xs, 3) print xs print xs[0] print len(xs) return xs[2]}Index assign works on var arrays: xs[i] = x.
Use case: filter and map
Section titled “Use case: filter and map”fn main(caps: Caps) -> int { let xs = [1, 2, 3, 4] let triples = array_map(xs, (x: int) -> x * 3) let evens = array_filter(xs, (x) -> x % 2 == 0) print triples print evens return len(evens)}Use case: string surgery
Section titled “Use case: string surgery”fn main(caps: Caps) -> int { let raw = " a,b,c " let t = str_trim(raw) let parts = str_split(t, ",") let joined = str_join(parts, "-") print joined let i = str_find(joined, "b") let piece = str_slice(joined, 0, i) print piece return len(joined)}Also: str_replace, str_to_upper / str_to_lower, str_starts_with / str_ends_with / str_contains, str_repeat / str_pad, parse_int / parse_float, to_string_int.
Use case: sort, pop, map keys
Section titled “Use case: sort, pop, map keys”// fragment-onlyfn main(caps: Caps) -> int { var xs = [3, 1, 2] xs = array_push(xs, 4) xs = array_pop(xs) xs = array_sort(xs) assert xs[0] == 1 assert array_contains(xs, 2) == true assert abs_int(-5) == 5
var m: Map<String, int> = map_new() m = map_set(m, "a", 1) assert map_contains_key(m, "a") == true let keys = map_keys(m) print keys return len(keys)}Use case: JSON round-trip
Section titled “Use case: JSON round-trip”// fragment-onlyfn main(caps: Caps) -> int { let parsed = json_parse("{\"n\":42}") return match parsed { Ok(v) => { print json_stringify(v) return 0 }, Err(_) => 0 - 1 }}JsonValue is opaque — parse / stringify / free. No Caps required for JSON. Full table: standard library.
Use case: string-keyed map
Section titled “Use case: string-keyed map”// fragment-onlyfn main(caps: Caps) -> int { var m = map_new() m = map_set(m, "a", 1) m = map_set(m, "b", 2) let v = map_get(m, "a") print v print len(m) return 0}v1: Map<String, V> only — not arbitrary key types.