From: Brian H. <bri...@ql...> - 2003-04-15 15:59:14
|
A question and a comment: 1) There are several data structures already extant that should have enum_of functions for- strings, arrays, lists, hash tables, sets, maps, stacks, queues, and buffers. So, where do we put these functions? In the enum package, or do we fork all 8 standard libraries just to add a single function? Personally, I think psqueue and xarray and bitarray should have enum_of and of_enum functions in their code. I don't want to be updating enum every time I add a new data structure. On the other hand, forking 8 libraries just to add 2 functions each strikes me as being a little extreme. 2) I dislike having the same function both return the current element and increment to the next element. First off, this can cause bugs, as unexpected next's moves you forward. These bugs are probably less like in Ocaml than in Java or C++ (in C++, mixing next calls with macros was a recipe for disaster), due to the habit of throwing let ... in clauses freely, but they can still happen. Second, this allows you to do act like you have an ungetc- you simply don't call the increment function until you know you're done with the current element. Maybe two different functions- current, which returns whatever the last call to next returns, and next (same as now). Calling current before calling next throws an exception. You could advance the pointer simply by going: let _ = next enum in (); Or maybe a third function, advance. Brian On Tue, 15 Apr 2003, Nicolas Cannasse wrote: > Hi "ext"List. > > I just finished as promised the Enum module. It provides powerfull things > such as lazy-maps ( a la Haskell ) but is mainly used in order to provide an > abstract enumeration so each data structure has to implement its of_enum / > enum / append_enum abstract functions. > > Example : > > let l = List.init 10 (fun x -> x) in > let e = List.enum l in > let e = Enum.map (fun x -> x+1) e in > let e = Enum.append e (List.enum l) in > let e = Enum.map string_of_int e in > (* here we haven't still called any function ! all is lazy up to the > construction of the final list *) > let l = Array.of_enum e in > Array.iter print_endline l > > All Enum module functions are tail-rec, and I have added the of_enum / enum > / append_enum methods to the ExtList module of the SourceForge CVS. ( copied > below ) > > PS : this enable also a 0 cost representation of String as char lists ! As > well as free String concatenation and so on... > > Nicolas Cannasse > > ---------------------- > let enum l = > let lr = ref l in > Enum.make > ~next:(fun () -> > match !lr with > | [] -> raise Enum.No_more_elements > | h :: t -> > lr := t; > h > ) > ~count:(fun () -> > length !lr > ) > > let of_enum e = > let dum = [ Obj.magic () ] in > let _ = Enum.fold (fun x acc -> > let r = [ x ] in > Obj.set_field (Obj.repr acc) 1 (Obj.repr r); > r) dum e in > tl dum > > let append_enum l e = > append l (of_enum e) > |