Bytes, math & arrays
-- Doc example: bytes, math, and numeric arrays / linear algebra (all pure).
intent: "doc example: bytes, math and arrays"
print("decode(bytes(\"Hi\")) = " + decode(bytes("Hi")) + ", norm([3,4]) = " + text(norm(array([3, 4]))))
test "bytes <-> text are inverses; hex encoding"
let b be bytes("Hi")
assert_eq(decode(b), "Hi")
assert_eq(decode(bytes("Hi"), "hex"), "4869")
assert_eq(decode(bytes("4869", "hex")), "Hi")
test "math operators"
assert_eq(2 ** 10, 1024)
assert_eq(10 % 3, 1)
test "numeric arrays + linear algebra"
assert_eq(norm(array([3, 4])), 5)
assert_eq(dot(array([1, 2, 3]), array([4, 5, 6])), 32)
Bytes
bytes is raw binary, distinct from text. bytes(...) and decode(...) are inverses:
bytes("Hi") -- UTF-8 bytes
bytes("4869", "hex") -- decode hex → bytes
bytes("SGk=", "base64") -- decode base64 → bytes
decode(b) -- bytes → text (UTF-8 strict)
decode(b, "hex") -- bytes → hex text
sha256(x) -- raw digest (bytes); hex via decode(sha256(x), "hex")
text(b) shows a hex repr like bytes(48656c6c6f) — it does not decode. A secret never materializes through bytes(...).
Math
Operators + - / % ; division is always float*. Constants: pi, tau, e, inf, nan. Numeric tower also has decimal (1.50d, exact) and complex.
Numeric arrays & linear algebra
array([...]) builds a numeric array; + - / * are elementwise (with broadcasting). Linear algebra (2D, via faer):
norm(array([3, 4])) -- 5
dot(array([1, 2, 3]), array([4, 5, 6])) -- 32
matmul(a, b) -- matrix product
solve(A, b) det(A) inv(A) eig(A) svd(A)