apfl/examples/examples.apfl

61 lines
1 KiB
Text
Raw Normal View History

2021-12-10 20:22:16 +00:00
pipe := {
x -> x
x f?callable ~more ->
pipe (f x) ~more
x [f?callable ~args] ~more ->
pipe (f ~args x) ~more
}
map := {
_ [] -> []
f [x ~xs] ->
[(f x) ~(map f xs)]
}
filter := {
_ [] -> []
f [x ~xs] ->
if (f x) {
[x ~(filter f xs)]
} {
filter f xs
}
}
is-odd := { x ->
== 1 (mod x 2)
}
partial := { f ~a1 ->
{ ~a2 -> f ~a1 ~a2 }
}
reduce := {
_ carry [] -> carry
f carry [x ~xs] ->
reduce f (f x carry) xs
}
sum := partial reduce + 0
pipe ~[
(range 1 10) # [1 2 3 4 5 6 7 8 9]
[filter is-odd] # [1 3 5 7 9]
[map (partial * 10)] # [10 30 50 70 90]
sum # 250
]
#####
# As each closure is unique, a closure can be used as a symbol, i.e. a unique
# value which only usage is to be able to be compared with itself.
symbol := { ->
sym := { -> sym }
sym # technically not neccessary, but this way it's more obvious we're returning sym
}
a := (symbol)
b := (symbol)
assert{ != a b }