recursive
Functions
pub fn block(initial: a, cb: fn(a, fn(a) -> b) -> b) -> b
Fixed-point combinator. Allows any block to run recursively
// Factorial
io.debug({
use it, f <- recursive.block(10)
case it {
0 -> 1
1 -> 1
_ -> it * f(it - 1)
}
})
pub fn block2(a: a, b: b, cb: fn(a, b, fn(a, b) -> c) -> c) -> c
Two argument Fixed-point combinator. Allows any block to run recursively
// Tail recursive factorial
io.debug({
use it, res, f <- recursive.block2(10, 1)
case it {
0 -> res
1 -> res
_ -> f(it - 1, res * it)
}
})
pub fn block3(
a: a,
b: b,
c: c,
cb: fn(a, b, c, fn(a, b, c) -> d) -> d,
) -> d
Three argument Fixed-point combinator. Allows any block to run recursively
// Fibonacci
io.debug({
use it, curr, prev, f <- recursive.block3(9, 1, 0)
case it {
0 -> curr
_ -> f(it - 1, curr + prev, curr)
}
})
pub fn block4(
a: a,
b: b,
c: c,
d: d,
cb: fn(a, b, c, d, fn(a, b, c, d) -> e) -> e,
) -> e
Four argument Fixed-point combinator. Allows any block to run recursively
pub fn block5(
a: a,
b: b,
c: c,
d: d,
e: e,
cb: fn(a, b, c, d, e, fn(a, b, c, d, e) -> f) -> f,
) -> f
Five argument Fixed-point combinator. Allows any block to run recursively
pub fn func(cb: fn(a, fn(a) -> b) -> b) -> fn(a) -> b
Fixed-point combinator. Allows anonymous functions to run recursively
let factorial = recursive.func(fn (it, f) {
case it {
0 -> 1
1 -> 1
_ -> it * f(it - 1)
}
})
io.debug(factorial(10))
pub fn func2(cb: fn(a, b, fn(a, b) -> c) -> c) -> fn(a, b) -> c
Two argument Fixed-point combinator. Allows anonymous functions to run recursively
let factorial = recursive.func2(fn (it, res, f) {
case it {
0 -> res
1 -> res
_ -> f(it - 1, res * it)
}
})
io.debug(factorial(10, 1))
pub fn func3(
cb: fn(a, b, c, fn(a, b, c) -> d) -> d,
) -> fn(a, b, c) -> d
Three argument Fixed-point combinator. Allows anonymous functions to run recursively
let fibonacci = recursive.func3(fn (it, curr, prev, f) {
case it {
0 -> curr
_ -> f(it - 1, curr + prev, curr)
}
})
io.debug(fibonacci(9, 1, 0))
pub fn func4(
cb: fn(a, b, c, d, fn(a, b, c, d) -> e) -> e,
) -> fn(a, b, c, d) -> e
Four argument Fixed-point combinator. Allows anonymous functions to run recursively
pub fn func5(
cb: fn(a, b, c, d, e, fn(a, b, c, d, e) -> f) -> f,
) -> fn(a, b, c, d, e) -> f
Five argument Fixed-point combinator. Allows anonymous functions to run recursively