From: Nicolas C. <war...@fr...> - 2003-05-28 09:06:56
|
> > I don't agree here : > > - BUT the internal next is most of the time "concatening" the next functions > > in a purely functional way, so for example after two Enum.maps and doing an > > Enum.iter, you'll have to check the None/Some three times ! > > For Enum.iter this is correct. However, for doing just about anything > else you need to try/catch the exception the same number of times you need > to check for None/Some. And I would expect map and fold to be at least as > important as iter. I don't think so... here's current Enum.map implementation : let map f t = { count = t.count; next = (fun () -> f (t.next())); } Every time we're catching only the exception one at the very end, since the map doesn't check for it, and the fold is only used to "finalize" an d Enum. Common usage of enums is the following : let e = DataStructure.enum d in let e = Enum.map f e in ... several maps / filter .... let d = DataStructure.of_enum d (* this one will call Enum.fold or Enum.iter *) In this sequence, there is only 1 catch for the exception if you're using Enum.iter or 2 if you're using Enum.fold , and it does not depends of the number of maps / filters you have done ! Here will be a map with next returning 'a option : let map f t = { count = t.count; next = (fun () -> match t.next() with None -> None | Some e -> Some (f e)); } and thus matching and allocating blocks all around... Exceptions are really cheap in ocaml, better than pattern matching since the caml stay unwinding is very efficient. Nicolas Cannasse |