-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmutate.tao
34 lines (27 loc) · 924 Bytes
/
mutate.tao
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
import "../lib/main.tao"
## Mutations take a function that maps a value to another of the same type
## They don't output anything (unlike an effect such as 'get some user input')
effect mutate A = (A -> A) => ()
## TODO: Have the compiler auto-generate this bit instead of
## forcing the effect user to deal with intrinsics.
fn mutate A : (A -> A) -> mutate A ~ () = f => @suspend(f)
## Apply a series of mutations to a value
fn apply_mutation A : mutate A ~ () -> A -> A =
m, x => let (_, x) = (m, x)
handle mutate A with f, x => ((), x -> f)
in x
## A mutation that pushes an element to the end of a list
fn push A : A -> mutate [A] ~ () =
x => mutate(fn xs => xs ++ [x])!
## Example usage
def numbers = do {
push(1)!;
push(2)!;
push(3)!;
push(4)!;
}
## Debug-prints '[1, 2, 3, 4]' to the console
def main : io ~ () = []
-> apply_mutation(numbers)
-> debug
-> print!