summaryrefslogtreecommitdiff
path: root/lib/modes.ml
blob: 3d0e3545119caa11ff49c61b3e0e70c70b85ebe1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
type mode = Normal | Insert
type t = mode
type state = int
type 'a state_monad = state -> 'a * state

let run (f : 'a state_monad) (s : state) : 'a = f s |> fst
let return (a : 'a) : 'a state_monad = fun s -> (a, s)

let ( >>= ) (f : 'a state_monad) (g : 'a -> 'b state_monad) : 'b state_monad =
 fun s ->
  let a, s' = f s in
  g a s'

let draw () : unit state_monad = return ()
let get_keypress () : char state_monad = return 'a'
let handle_key (_ : char) : unit state_monad = return ()
let rec loop () = () |> draw >>= get_keypress >>= handle_key >>= loop
let test = run (loop ()) 0