Skip to content

Instantly share code, notes, and snippets.

@Nymphium
Created January 30, 2020 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nymphium/fa8b0f3044ffdd8b32fcc6db290fde8a to your computer and use it in GitHub Desktop.
Save Nymphium/fa8b0f3044ffdd8b32fcc6db290fde8a to your computer and use it in GitHub Desktop.
let run_continuer : type a. ((unit -> (unit -> a)) -> a) -> a =
fun f ->
let module M = struct
effect Save : unit -> (unit -> a);;
let save () = perform @@ Save ()
end
in
let open M in
match f save with
| effect (Save ()) k ->
let rec c k =
let k' = Obj.clone_continuation k in
continue k @@ fun () -> c k'
in c k
| x -> x
print_int @@ run_continuer @@ fun save ->
let i = ref 0 in
let c = save () in
if !i < 10 then
let () = incr i in
c ()
else
!i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment