Biblioteca estándar
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 "base58/base32 round-trip; bytes <-> exact integers"
assert_eq(decode(bytes(decode(bytes("Hi"), "base58"), "base58")), "Hi")
assert_eq(decode(bytes("Hi"), "base32"), "JBUQ")
-- bytes_to_int/int_to_bytes: big-endian, exact (protocol/signature integers)
assert_eq(bytes_to_int(bytes("0400", "hex")), 1024)
assert_eq(int_to_bytes(1024), bytes("0400", "hex"))
assert_eq(int_to_bytes(7, 4), bytes("00000007", "hex"))
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
bytes("SGk", "base64url") -- decodificar base64url (URL-safe -_, padding opcional; JWT/tokens) → bytes
bytes("StV1DL6", "base58") -- decodificar base58 (alfabeto Bitcoin/Solana) → bytes
bytes("JBSWY3DP", "base32") -- decodificar base32 RFC 4648 (convención Algorand) → bytes
decode(b) -- bytes → text (UTF-8 estricto)
decode(b, "hex") -- bytes → texto hex (también base64 / base64url / base58 / base32)
sha256(x) -- digest crudo (bytes); hex con decode(sha256(x), "hex")
bytes_to_int(b) -- bytes big-endian → entero exacto no-negativo (vacío → 0)
int_to_bytes(n, size?) -- entero → bytes big-endian: mínimos, o con padding de ceros a size
int_to_bytes_le(n, size) -- entero → bytes little-endian de exactamente size (structs binarios, p.ej. u32/u64 LE de Solana)
text(b) muestra una repr hex como bytes(48656c6c6f) — no decodifica. Un secret nunca se materializa a través de bytes(...). bytes_to_int es exacto a cualquier ancho (un valor de 32 bytes jamás pasa por float), que es lo que piden los enteros de firmas/protocolos — mirá 38-blockchain.
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.
Funciones (puras, sin capability; reales o complex donde se indica):
- magnitud / selección (preservan el tipo):
abs,sign,min,max,clamp(x, lo, hi)—abs(complex)es el módulo. - raíces / potencias:
sqrt,cbrt,hypot,pow. exp / log:exp,ln,log10,log2,log_base(x, base)(no haylogpelado — es una soft keyword). - redondeo:
floor,ceil,round,trunc,round_to(x, dígitos). - trigonometría (radianes):
sin,cos,tan,asin,acos,atan,atan2(y, x),radians,degrees; hiperbólicas:sinh,cosh,tanh,asinh,acosh,atanh. - teoría de números (enteros):
gcd,lcm,factorial. - introspección:
is_nan,is_infinite,is_finite; predicados de tipois_decimal,is_complex,is_array,is_bytes,type_of. - agregados sobre una lista (o
array):sum,product,mean,std,var,median,percentile(x, p),histogram(x, bins?)→{counts, edges}. - funciones especiales (sólo reales):
gamma,lgamma,erf,erfc,beta. - complejos:
complex(re, im),real,imag,conj,arg;sqrt/exp/ln/trig aceptan un complejo y devuelven uno (sqrt(complex(-1, 0))→0+1i;complex(0, 1) ** 2→-1+0i). Argumento real → resultado real (sqrt(-1)→ NaN). - enteros ↔ bytes:
bytes_to_int(bytes),int_to_bytes(n, len)(big-endian),int_to_bytes_le(n, len).
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)
Constructores y forma: arange(inicio, fin), linspace(inicio, fin, n), zeros(n), ones(n), eye(n), full(n, valor), reshape(a, [filas, cols]), shape, ndim, size, transpose, trace.