The AffineScript standard library provides essential utilities and data structures.
Basic utilities and operations.
Functions:
id[T](x: T) -> T- Identity functionconst[A, B](x: A, _y: B) -> A- Constant functioncompose[A, B, C](f, g)- Function compositionflip[A, B, C](f)- Flip function argumentsmin(a, b),max(a, b),clamp(x, low, high)- Numeric operationsabs(x),sign(x)- Absolute value and signnot(x),and(a, b),or(a, b),xor(a, b)- Boolean operations
Example:
use Core::{min, max, abs};
let smallest = min(10, 20); // 10
let largest = max(10, 20); // 20
let absolute = abs(-42); // 42
Error handling utilities for Result[T, E] type.
Functions:
is_ok(r),is_err(r)- Check result statusunwrap(r),unwrap_or(r, default)- Extract valueunwrap_err(r)- Extract errormap(r, f),map_err(r, f)- Transform value or errorand_then(r, f)- Chain operations (flatMap)ok(r),err(r)- Convert to Option
Example:
use Result::{map, unwrap_or};
fn divide(a: Int, b: Int) -> Result[Int, String] {
return if b == 0 {
Err("division by zero")
} else {
Ok(a / b)
};
}
let result = divide(10, 2);
let doubled = map(result, |x| { return x * 2; });
let value = unwrap_or(doubled, 0); // 10
Optional value utilities for Option[T] type.
Functions:
is_some(opt),is_none(opt)- Check if value existsunwrap(opt),unwrap_or(opt, default)- Extract valueunwrap_or_else(opt, f)- Extract or compute defaultmap(opt, f),map_or(opt, default, f)- Transform valueand_then(opt, f)- Chain operations (flatMap)or(opt, other),or_else(opt, f)- Alternative valuesfilter(opt, pred)- Filter by predicateok_or(opt, err),ok_or_else(opt, f)- Convert to Result
Example:
use Option::{map, unwrap_or};
fn find_positive(x: Int) -> Option[Int] {
return if x > 0 { Some(x) } else { None };
}
let opt = find_positive(42);
let doubled = map(opt, |x| { return x * 2; });
let value = unwrap_or(doubled, 0); // 84
Mathematical functions and constants.
Constants:
PI= 3.14159...E= 2.71828...TAU= 6.28318... (2π)
Integer Functions:
abs(x),min(a, b),max(a, b),clamp(x, low, high)pow(base, exp)- Integer exponentiationgcd(a, b),lcm(a, b)- Greatest common divisor and least common multiplefactorial(n)- Factorialfib(n)- Fibonacci numberis_even(n),is_odd(n)- Parity checks
Float Functions:
abs_f(x),min_f(a, b),max_f(a, b),clamp_f(x, low, high)
Example:
use Math::{pow, gcd, factorial};
let squared = pow(5, 2); // 25
let divisor = gcd(48, 18); // 6
let perm = factorial(5); // 120
Import modules using the use statement:
// Import entire module
use Core;
let result = Core.abs(-10);
// Import specific functions
use Core::{min, max};
let smaller = min(5, 10);
// Import with alias
use Math as M;
let circle_area = M.PI * radius * radius;
The standard library uses these built-in types:
Result[T, E]- Success (Ok) or failure (Err)Option[T]- Present value (Some) or absent (None)Int- Integer numbersFloat- Floating-point numbersBool- Boolean values (true/false)String- Text strings
Implemented:
- ✅ Core utilities
- ✅ Result error handling
- ✅ Option optional values
- ✅ Math basic functions
TODO:
- String manipulation functions
- Array/List operations
- I/O functions (requires FFI)
- Transcendental math functions (sin, cos, sqrt, etc.)
- Date/Time utilities
- File system operations
To add new stdlib functions:
- Add function to appropriate module file
- Document with examples
- Update this README
- Add tests in
tests/stdlib/
Test standard library functions:
affinescript eval tests/stdlib/test_core.as
affinescript eval tests/stdlib/test_result.as
affinescript eval tests/stdlib/test_option.as
affinescript eval tests/stdlib/test_math.as