From: Nicolas C. <war...@fr...> - 2004-11-09 19:02:58
|
> Anybody has any idea of some lazy lists implementation in Ocaml? Well partition could be easily added to Enum Something like : val partition : ('a -> bool) -> 'a Enum.t -> 'a Enum.t * 'a Enum.t The implementation itself is quite easy : let partition f t = let mem_true = Queue.create() in let mem_false = Queue.create() in let next b () = try Queue.pop (if b then mem_true else mem_false) with Queue.Empty -> let rec loop() = let x = t.next() in if f x = b then x else begin Queue.push x (if b then mem_false else mem_true); loop(); end; in loop() in from (next true) , from (next false) But I'm not sure we should add it to Enum module because it creates a unwelcomed dependency on the Queue module. Regards, Nicolas Cannasse |