forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.nula
More file actions
45 lines (31 loc) · 1.77 KB
/
Copy pathcore.nula
File metadata and controls
45 lines (31 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Nulang standard library — core combinators and utilities.
// Language version: 2.0.0-alpha
// Import: `import stdlib::core`
// ── Identity and constants ────────────────────────────────────────────────
/// Identity function. Returns its argument unchanged.
pub fn identity(x) { x }
/// Constant function: always returns the first argument, ignoring the second.
pub fn const_fn(x, _y) { x }
/// Always: given a value, returns a function that ignores its input and returns that value.
pub fn always(x) { fn(_y) { x } }
// ── Function combinators ──────────────────────────────────────────────────
/// Apply function f to argument x.
pub fn apply(f, x) { f(x) }
/// Function composition (3-arg form): (f ∘ g)(x) = f(g(x)).
pub fn compose(f, g, x) { f(g(x)) }
/// Returns a new function h(x) = f(g(x)).
pub fn compose_fn(f, g) { fn(x) { f(g(x)) } }
/// Flip argument order: flip(f, x, y) = f(y, x).
pub fn flip(f, x, y) { f(y, x) }
// ── Boolean combinators ───────────────────────────────────────────────────
/// Logical negation.
pub fn negate(b) { if b then false else true }
// ── Comparison helpers ────────────────────────────────────────────────────
/// Clamp x to the range [lo, hi].
pub fn clamp(x, lo, hi) {
if x < lo then lo
else if x > hi then hi
else x
}
/// True when x is between lo and hi (inclusive).
pub fn between(lo, hi, x) { lo <= x && x <= hi }