logo       
Google Custom Search
    AddThis Social Bookmark Button
-->

More enum conversions?: msg#00008

Subject: More enum conversions?
Currently enum conversion functions are only provided for the data
structures included in ExtLib itself.  Might it be a good idea to add
them for some or all of the remaining parts of the OCaml standard library?

In particular, I've often found myself wanting to convert from the
builtin array type, and there doesn't seem to be a function to handle
that yet.  So at the very least I'd propose adding something along the
lines of:

  val enum_of_array : 'a array -> 'a Enum.t
  val array_of_enum : 'a Enum.t -> 'a array

  let enum_of_array a = (* stolen from DynArray.enum *)
      let rec make start =
          let idxref = ref 0 in
          let next () =
              if !idxref >= Array.length a then
                  raise Enum.No_more_elements
              else
                  let retval = Array.unsafe_get a !idxref in
                  incr idxref;
                  retval
          and count () =
              if !idxref >= Array.length a then 0
              else Array.length a - !idxref
          and clone () =
              make !idxref
          in
          Enum.make ~next:next ~count:count ~clone:clone
      in
      make 0

  let array_of_enum e =
      match Enum.peek e with
      | None -> [||]
      | Some elt ->
          let a = Array.make (Enum.count e) elt in
          Enum.iteri (Array.unsafe_set a) e;
          a


-------------------------------------------------------
This SF.Net email is sponsored by Oracle Space Sweepstakes
Want to be the first software developer in space?
Enter now for the Oracle Space Sweepstakes!
http://ads.osdn.com/?ad_id=7412&alloc_id=16344&op=click


<Prev in Thread] Current Thread [Next in Thread>