From: Jere Sanisalo <xm@xm...>  20040628 12:34:10

Hi, just a few thoughts on adding new modules to extlib. One useful feature would be a module that offers different kinds of iterators on number ranges. I've found myself writing some nasty for loops and I've been thinking that wouldn't it be useful is there was something like List.iter for a number range. Attached is a blanket base where to build more iterators (like floating point iterators with a given step or multidimenstional iterators). Also coming from a C++ background, I've found the lack of a scope finalizera bit disappointing. It would be nice to have a function such as this in extlib:  (** [run f fend] calls the function [f] and runs [fend] when exiting scope (either via end of exectuion or an exception). If [fend] throws an exception, it's thrown instead of any exception thrown by [f]. *) let run f fend = try let x = f () in fend (); x with x > fend (); raise x ;;  It might also be feasible to make a version which ignores any exceptions thrown by fend. Anyhow, here's the NumIter module I propositioned earlier:  NumIter.mli (** Numeric iterators. Contains functions to iterate over ranges of numbers. *) val iter : (int > unit) > int > int > unit (** [iter f s e] calls the function [f] for every number from [s] to [e]. *) val map_array : (int > 'a) > int > int > 'a array (** [map_array f s e] maps the number range to an array of values. *) val map_list : (int > 'a) > int > int > 'a list (** [map_list f s e] maps the number range to a list of values. *)   NumIter.ml let iter f s e = let step = if s < e then 1 else 1 in let rec do_it v = f v; if v <> e then do_it (v+step) in do_it s ;; let map_array f s e = let amt = 1 + (abs (es)) in let step = if s < e then 1 else 1 in let do_it i = f (s + i*step) in Array.init amt do_it ;; let map_list f s e = Array.to_list (map_array f s e) ;;  Of course the actual implementations should be optimized a bit more :). Also open for discussion is the fact if the range should be open or closed. The current implementation runs the function always at least once (if start and end are the same). This is the same behaviour as with for. But the numiters also iterate backwards (which is useful, as there is no for which runs downwards), so this might be an issue. Comments anyone?  Jere Sanisalo [xm@...]  http://www.xmunkki.org/ 