Commits

Sébastien Ferré committed e7c7cb3

Add of functions 'fold_for', 'fold_for_down', and 'stream_map'.

  • Participants
  • Parent commits 47abb2a

Comments (0)

Files changed (1)

     | None -> e
     | Some e' -> fold_while f e'
 
+let fold_for : (int -> 'a -> 'a) -> int -> int -> 'a -> 'a =
+  fun f a b e ->
+    let res = ref e in
+    for x = a to b do
+      res := f x !res
+    done;
+    !res
+
+let fold_for_down : (int -> 'a -> 'a) -> int -> int -> 'a -> 'a =
+  fun f a b e ->
+    let res = ref e in
+    for x = a downto b do
+      res := f x !res
+    done;
+    !res
+
 let rec insert : ('a -> 'a -> bool) -> 'a -> 'a list -> 'a list =
   fun order x ->
     function
 	  owner := None;
 	  Mutex.unlock m;
 	  res
+
+(* utilities on streams *)
+
+let rec stream_map f = parser
+  | [<'x; str>] -> [<'f x; stream_map f str>]
+  | [<>] -> [<>]
+