Skip to content

Instantly share code, notes, and snippets.

@maximvl
Last active August 29, 2015 14:07
Show Gist options
  • Save maximvl/2f0797027528f5dcb9f0 to your computer and use it in GitHub Desktop.
Save maximvl/2f0797027528f5dcb9f0 to your computer and use it in GitHub Desktop.
let lwt_call f =
let t1,w1 = Lwt.wait () in
Lwt.ignore_result
(let res = f () in
Lwt.wakeup w1 res;
Lwt.return_unit);
t1
;;
let read_direction dir =
lwt_call (fun () ->
match Graphics.read_key () with
',' -> 0
| 'e' -> 3
| 'o' -> 6
| 'a' -> 9
| _ -> dir)
;;
let timeout_direction dir =
lwt_call (fun () ->
print_endline "before timeout";
Lwt_unix.sleep delay ;
print_endline "after timeout";
dir )
;;
let get_direction dir =
let t1 = read_direction dir
and t2 = timeout_direction dir in
Lwt.choose [t1;t2]
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment