logo       
Google Custom Search
    AddThis Social Bookmark Button

Enum module: msg#00010

Subject: Enum module
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)

Attachment: enum.ml
Description: Binary data

Attachment: enum.mli
Description: Binary data


Try Searching:
servers, voip, java, networking, microsoft ...
<Prev in Thread] Current Thread [Next in Thread>