-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAsync.hs
More file actions
65 lines (52 loc) · 1.65 KB
/
Async.hs
File metadata and controls
65 lines (52 loc) · 1.65 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
data Async s o r = Emit s o
| Done r
newtype Task i o r = Task { exec :: i -> Async (Task i o r) o r }
data Event = Key Char
data Command = InsertChar Char
| DeleteChar
| Operation Operator Motion
deriving Show
data Operator = Copy
| Change
| Delete
deriving Show
data Motion = LineNext
| LinePrev
deriving Show
type Handle = Task Event
type Future = Handle ()
type Stream a = Handle a ()
normal :: Stream (Maybe Command)
normal = Task $ \e -> case e of
Key 'i' -> Emit insert Nothing
Key 'c' -> Emit (pending Change motion) Nothing
Key 'q' -> Done ()
pending :: Operator -> Future (Maybe Motion) -> Stream (Maybe Command)
pending op t = Task $ \e -> case exec t e of
Emit t' () -> Emit (pending op t') Nothing
Done mot -> Emit normal $ Operation op <$> mot
insert :: Stream (Maybe Command)
insert = Task $ \e -> case e of
Key c -> Emit insert $ Just $ InsertChar c
motion :: Future (Maybe Motion)
motion = Task $ \e -> case e of
Key 'j' -> Done $ Just LineNext
Key 'k' -> Done $ Just LinePrev
Key _ -> Done Nothing
runTask :: [i] -> Task i o r -> ([o], Either (Task i o r) r)
runTask [] task = ([], Left task)
runTask (i : is) t = case exec t i of
Emit task' o -> let (os, r) = runTask is task'
in (o : os, r)
Done r -> ([], Right r)
runEvents :: (Show o) => Handle o r -> IO r
runEvents task = do
line <- getLine
let events = map Key line
(os, r) = runTask events task
print os
case r of
Left task' -> runEvents task'
Right res -> pure res
main :: IO ()
main = runEvents normal