Skip to content

Instantly share code, notes, and snippets.

@kayceesrk
Created March 15, 2021 03:28
Show Gist options
  • Save kayceesrk/eb0ab496c22861f21b1d9484772e982d to your computer and use it in GitHub Desktop.
Save kayceesrk/eb0ab496c22861f21b1d9484772e982d to your computer and use it in GitHub Desktop.
let n = try int_of_string (Sys.argv.(1)) with _ -> 25
module MkGen (S :sig
type 'a t
val iter : ('a -> unit) -> 'a t -> unit
end) : sig
val gen : 'a S.t -> (unit -> 'a option)
end = struct
let gen : type a. a S.t -> (unit -> a option) = fun l ->
let module M = struct effect Yield : a -> unit end in
let open M in
let rec step = ref (fun () ->
match S.iter (fun v -> perform (Yield v)) l with
| () -> None
| effect (Yield v) k ->
step := (fun () -> continue k ());
Some v)
in
fun () -> !step ()
end
(* A generator for a list *)
module L = MkGen(struct
type 'a t = 'a list
let iter = List.iter
end)
type 'a tree =
| Leaf
| Node of 'a tree * 'a * 'a tree
let rec make = function
| 0 -> Leaf
| n -> let t = make (n-1) in Node (t,n,t)
let rec iter f = function
| Leaf -> ()
| Node (l, x, r) -> iter f l; f x; iter f r
(* A generator for a tree *)
module T = MkGen(struct
type 'a t = 'a tree
let iter = iter
end)
let main () =
let next = T.gen (make n) in
let rec consume () =
match next () with
| None -> ()
| Some _ -> consume ()
in
consume ()
let _ = main ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment