Files
lux/std/result.lux
Brandon Lucas 9ee7148d24 feat: add module import examples and standard library
Module system was already implemented - this adds examples and stdlib:

examples/modules/:
- math_utils.lux: Reusable math functions (square, cube, factorial)
- string_utils.lux: String utilities (repeat, exclaim, greet)
- main.lux: Basic module import example
- main_selective.lux: Selective imports {fn1, fn2}
- main_wildcard.lux: Wildcard imports (module.*)
- use_stdlib.lux: Using the standard library

std/:
- prelude.lux: Core utilities (identity, compose, flip, not, and, or)
- io.lux: I/O helpers (println, readLine, debug)
- option.lux: Option utilities (some, none, map, flatMap, filter)
- result.lux: Result utilities (ok, err, mapOk, mapErr)

Import syntax supports:
- import path/to/module       (access as module.fn)
- import path/to/module as x  (access as x.fn)
- import path/to/module.{a,b} (access as a, b directly)
- import path/to/module.*     (all exports directly)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-13 17:28:23 -05:00

44 lines
995 B
Plaintext

// Result type utilities
// For working with computations that may fail
/// Wrap a value in Ok
pub fn ok<T, E>(value: T): Result<T, E> = Ok(value)
/// Wrap an error in Err
pub fn err<T, E>(error: E): Result<T, E> = Err(error)
/// Check if result is Ok
pub fn isOk<T, E>(r: Result<T, E>): Bool =
match r {
Ok(_) => true,
Err(_) => false
}
/// Check if result is Err
pub fn isErr<T, E>(r: Result<T, E>): Bool =
match r {
Ok(_) => false,
Err(_) => true
}
/// Get value or default
pub fn unwrapOr<T, E>(r: Result<T, E>, default: T): T =
match r {
Ok(v) => v,
Err(_) => default
}
/// Map over the success value
pub fn mapOk<T, U, E>(r: Result<T, E>, f: fn(T): U): Result<U, E> =
match r {
Ok(v) => Ok(f(v)),
Err(e) => Err(e)
}
/// Map over the error value
pub fn mapErr<T, E, F>(r: Result<T, E>, f: fn(E): F): Result<T, F> =
match r {
Ok(v) => Ok(v),
Err(e) => Err(f(e))
}