mirror of
https://github.com/unisonweb/unison.git
synced 2024-10-26 11:07:48 +03:00
51 lines
841 B
Plaintext
51 lines
841 B
Plaintext
--State2 ability
|
|
|
|
structural type Optional a = None | Some a
|
|
|
|
structural ability State s where
|
|
put : s -> {State s} ()
|
|
get : {State s} s
|
|
|
|
state : s -> Request (State s) a -> (s, a)
|
|
state s = cases
|
|
{ State.get -> k } -> handle k s with state s
|
|
{ State.put snew -> k } -> handle k () with state snew
|
|
{ a } -> (s, a)
|
|
|
|
modify : (s ->{} s) ->{State s} ()
|
|
modify f =
|
|
s = State.get
|
|
s2 = f s
|
|
State.put s2
|
|
|
|
increment : '{State Nat} ()
|
|
increment = '(modify ((+) 1))
|
|
|
|
second : (a, b) -> b
|
|
second = cases (_,b) -> b
|
|
|
|
first : (a, b) -> a
|
|
first = cases (a,_) -> a
|
|
|
|
ex : Text
|
|
ex =
|
|
result : (Nat, Text)
|
|
result = handle
|
|
State.put (11 + 1)
|
|
x = State.get
|
|
State.put (5 + 5)
|
|
"hello"
|
|
with state 10
|
|
|
|
second result
|
|
|
|
> ex
|
|
|
|
modify2 : (s -> s) ->{State s} ()
|
|
modify2 f =
|
|
s = State.get
|
|
s2 = f s
|
|
State.put s2
|
|
|
|
---
|