Bytes, matemática y 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 es binario crudo, distinto de text. bytes(...) y decode(...) son inversos:
bytes("Hi") -- bytes UTF-8
bytes("4869", "hex") -- decodificar hex → bytes
bytes("SGk=", "base64") -- decodificar base64 → bytes
decode(b) -- bytes → text (UTF-8 estricto)
decode(b, "hex") -- bytes → texto hex
sha256(x) -- digest crudo (bytes); hex con decode(sha256(x), "hex")
text(b) muestra una repr hex como bytes(48656c6c6f) — no decodifica. Un secret nunca se materializa a través de bytes(...).
Matemática
Operadores + - / % ; la división es siempre float*. Constantes: pi, tau, e, inf, nan. La torre numérica también tiene decimal (1.50d, exacto) y complex.
Arrays numéricos y álgebra lineal
array([...]) construye un array numérico; + - / * son elemento a elemento (con broadcasting). Álgebra lineal (2D, vía faer):
norm(array([3, 4])) -- 5
dot(array([1, 2, 3]), array([4, 5, 6])) -- 32
matmul(a, b) -- producto matricial
solve(A, b) det(A) inv(A) eig(A) svd(A)