Commits

Anonymous committed 534cb20

Cd_List: map_to_array

Comments (0)

Files changed (1)

       ]
     ;
 
+    value map_to_array f lst =
+      let len = List.length lst in
+      if len = 0
+      then [| |]
+      else
+        match lst with
+        [ [] -> assert False
+        | [h :: t] ->
+            let r = Array.make len (f h) in
+            loop 1 t
+            where rec loop i lst =
+              match lst with
+              [ [] -> r
+              | [h :: t] ->
+                  ( r.(i) := f h
+                  ; loop (i + 1) t
+                  )
+              ]
+        ]
+    ;
+
     value reduce : Monoid.t 'a -> t 'a -> 'a
     = fun mon lst ->
         fold_left mon#plus mon#zero lst