Files
lux/std/prelude.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

38 lines
1.1 KiB
Plaintext

// Standard Prelude - commonly used functions and types
// This module can be imported to get essential utilities
/// Identity function - returns its argument unchanged
pub fn identity<T>(x: T): T = x
/// Constant function - returns first argument, ignores second
pub fn const<A, B>(a: A, b: B): A = a
/// Function composition
pub fn compose<A, B, C>(f: fn(B): C, g: fn(A): B): fn(A): C =
fn(x: A): C => f(g(x))
/// Flip argument order of a binary function
pub fn flip<A, B, C>(f: fn(A, B): C): fn(B, A): C =
fn(b: B, a: A): C => f(a, b)
/// Apply a function to a value (useful for pipelines)
pub fn apply<A, B>(f: fn(A): B, x: A): B = f(x)
/// Check if two integers are equal
pub fn eq(a: Int, b: Int): Bool = a == b
/// Check if first is less than second
pub fn lt(a: Int, b: Int): Bool = a < b
/// Check if first is greater than second
pub fn gt(a: Int, b: Int): Bool = a > b
/// Negate a boolean
pub fn not(b: Bool): Bool = if b then false else true
/// Logical and
pub fn and(a: Bool, b: Bool): Bool = if a then b else false
/// Logical or
pub fn or(a: Bool, b: Bool): Bool = if a then true else b