-
Notifications
You must be signed in to change notification settings - Fork 10
Open
Description
I have the following code to consume all stdin from user and abstract them over a genrator API:
let read_lineedit () =
print_string "> ";
read_line ()
let gen_lines () =
let cache = ref [] in
let eof_reached = ref false in
let query_cache i =
let l = List.length !cache in
if l <= i then
None
else
Some (List.nth !cache (l - i - 1))
in
let gen_at i =
match query_cache i with
| Some(g) -> g
| None ->
if !eof_reached
then Gen.empty
else begin
while (List.length !cache) <= i && not !eof_reached do
cache :=
begin try
Gen.of_string (read_lineedit())
with End_of_file ->
eof_reached := true; Gen.empty
end :: !cache
done;
List.hd !cache
end
in
Gen.init gen_at |> Gen.flattenThis is problematic, however, because the outer generator is infinite, and even we know inner generators can no longer produce any value, the outer generator still keep trying to generate inner generators.
Metadata
Metadata
Assignees
Labels
No labels