ludus/sandbox.ld

112 lines
1.7 KiB
Plaintext
Raw Normal View History

2023-09-16 17:48:50 +00:00
fn fib {
"Tells you a fib number."
(0) -> 0
(1) -> 1
(n) -> add (
fib (dec (n))
fib (sub (n, 2))
)
}
fn unwrap {
((:some, value)) -> value
((:ok, value)) -> value
}
fn default (default_value) -> fn (maybe) -> when maybe is {
(:ok, value) -> value
(:err, _) -> default_value
nil -> default_value
value -> value
}
fn some (value) -> (:some, value)
fn ok (value) -> (:ok, value)
let foo = unwrap ((:ok, 42))
print (:foo, foo)
let bar = unwrap ((:some, 23))
print (:bar, bar)
2023-09-16 17:48:50 +00:00
let baz = do 69 > default (12) > print (:baz, _)
let quux = do nil > default (12) > print (:quux, _)
& unwrap ((:err, "message"))
2023-06-02 22:10:40 +00:00
2023-06-01 23:27:55 +00:00
fn map {
(f) -> fn mapper (xs) -> map (f, xs)
(f, xs) -> {
let n = count (xs)
loop (0, []) with (i, ys) -> if eq (i, n)
then ys
2023-09-16 17:48:50 +00:00
else recur (
inc (i)
conj (ys, f (nth (i, xs))))
2023-06-01 23:27:55 +00:00
}
}
2023-06-01 21:06:33 +00:00
2023-06-01 23:27:55 +00:00
fn reduce {
(f) -> fn reducer {
(xs) -> reduce (f, xs)
(xs, init) -> reduce (f, xs, init)
}
(f, xs) -> {
let first_x = first (xs)
let more_xs = rest (xs)
reduce (f, more_xs, first_x)
}
(f, xs, init) -> {
let n = count (xs)
loop (0, init) with (i, acc) -> if eq (i, n)
then acc
else {
let curr = nth (i, xs)
let next = f (acc, curr)
recur (inc (i), next)
}
}
}
fn filter {
(f) -> fn filterer (xs) -> filter (f, xs)
(f, xs) -> {
let n = count (xs)
loop (0, []) with (i, ys) -> when {
eq (i, n) -> ys
f (nth (i, xs)) -> recur (inc (i), conj (ys, nth (i, xs)))
else -> recur (inc (i), ys)
}
}
}
2023-09-16 17:48:50 +00:00
& fn shuffle
ref x = 4
set! (x, "foo")
set! (x, :foo)
deref (x)
let greater_than_two? = gt (_, 2)
2023-06-02 22:04:03 +00:00
2023-07-04 16:42:56 +00:00
fn square (x) -> mult (x, x)
2023-06-01 23:27:55 +00:00
let xs = [1, 2, 3]
2023-09-16 17:48:50 +00:00
let ys = #{:a 1, :b 2}
ys :a
:a (ys)
let y = 1
do y > inc > square > sub (_, 3)