Synsema docsENES

Índice de builtins

Un mapa categorizado de los builtins. Las firmas completas viven en cada página de tema; los centrales están doctested acá para que esta referencia no driftee.

builtins.syn
-- Doc reference: a few core builtins, doctested so the reference can't drift.
intent: "doc reference: core builtins"

print("upper(\"hi\") = " + upper("hi") + ",  join([a,b],\"-\") = " + join(["a", "b"], "-"))

test "collections"
    assert_eq(length([1, 2, 3]), 3)
    assert(contains([1, 2, 3], 2))
    assert_eq(slice([1, 2, 3, 4], 1, 3), [2, 3])     -- Python-style end-exclusive
    assert_eq(keys({"a": 1, "b": 2}), ["a", "b"])
    assert_eq(values({"a": 1, "b": 2}), [1, 2])

test "text"
    assert_eq(upper("hi"), "HI")
    assert_eq(trim("  x  "), "x")
    assert_eq(split("a,b,c", ","), ["a", "b", "c"])
    assert_eq(join(["a", "b"], "-"), "a-b")
    assert(starts_with("hello", "he"))

test "intentional ops"
    assert_eq(apply((n) => n * 2, [1, 2, 3]), [2, 4, 6])
    assert_eq(where([1, 2, 3, 4], (n) => n > 2), [3, 4])
    assert_eq(reduce([1, 2, 3], (a, b) => a + b, 0), 6)

test "intentional ops accept both orders (fn-first and list-first)"
    assert_eq(apply([1, 2, 3], (n) => n * 2), [2, 4, 6])     -- list-first also works
    assert_eq(where((n) => n > 2, [1, 2, 3, 4]), [3, 4])     -- fn-first also works
    assert_eq(reduce((a, b) => a + b, [1, 2, 3], 10), 16)    -- extra args stay at the end

test "unique and index_of"
    assert_eq(unique([3, 1, 3, 2, 1]), [3, 1, 2])            -- first-appearance order
    assert_eq(index_of([10, 20, 30], 20), 1)                 -- by item
    assert_eq(index_of([1, 2, 3], (n) => n > 1), 1)          -- by predicate
    assert_eq(index_of([1, 2], 9), nothing)                  -- absent → nothing, not -1

Por categoría