From: Nicolas C. <war...@fr...> - 2003-06-04 02:50:32
|
> let default_has_next next count = > let lookahead = ref None in > let has_next' () = > match lookahead with > Some x -> true > | None -> try > lookahead := Some (next ()); > true > with No_more_elements -> false > and next' () = > match lookahead with > Some x -> > lookahead := None; > x > | None -> > next() > and count' () -> > match lookahead with > Some _ -> 1 + (count ()) > | None -> count() This is a little more complicated than that... Actually calling count() can make next being called ( if this is a count calling force ), and then the return of count() will be the total number of elements, including the lookhead, so you'll count 1 more element since you're adding 1. You could first call count and then match again the lookahead to see if still Some.... but that's pretty inneficient. You can have a look at current Enum.peek implementation, I think it resolves the problem in a nice and elegant way. Nicolas Cannasse |