Skip to content

[FR] allow marking generator as exhausted non-deterministically?  #26

@glyh

Description

@glyh

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.flatten

This 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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions