]> xenbits.xensource.com Git - xcp/xen-api-libs.git/commitdiff
Adding extensions to some modules in the extended standard library:
authorMatthias Goergens <matthias.goergens@citrix.com>
Tue, 26 Jan 2010 15:01:01 +0000 (15:01 +0000)
committerMatthias Goergens <matthias.goergens@citrix.com>
Tue, 26 Jan 2010 15:01:01 +0000 (15:01 +0000)
- to the Map module
- to the List module
- and some functional combinators (like compose with (++)) in Fun module

Signed-off-by: Matthias Goergens <matthias.goergens@citrix.com>
stdext/Makefile
stdext/fun.ml
stdext/fun.mli
stdext/listext.ml
stdext/listext.mli
stdext/opt.ml
stdext/opt.mli
stdext/pervasiveext.ml
stdext/pervasiveext.mli

index 29380efcd439dfd07a3e8758070dcbf16848bc8c..3ba7b303d64b7c1162e5c763e2d49d89aef519d0 100644 (file)
@@ -20,9 +20,9 @@ FEPP = camlp4o -I ../rpc-light -I $(shell ocamlfind query type-conv) pa_type_con
 OCAML_TEST_INC = -I $(shell ocamlfind query oUnit)
 OCAML_TEST_LIB = $(shell ocamlfind query oUnit)/oUnit.cmxa
 
-STDEXT_OBJS = filenameext stringext arrayext hashtblext listext pervasiveext threadext ring \
+STDEXT_OBJS = fun listext filenameext stringext arrayext hashtblext pervasiveext threadext ring \
        qring fring opt bigbuffer unixext range vIO trie config date encodings fe fecomms \
-       forkhelpers gzip sha1sum zerocheck base64 backtrace tar 
+       forkhelpers gzip sha1sum zerocheck base64 backtrace tar mapext
 
 INTF = $(foreach obj, $(STDEXT_OBJS),$(obj).cmi)
 LIBS = stdext.cma stdext.cmxa
index 4088caca3f58ce3eabe06d306447eae506c80cc9..8cd5d9aa617c134ac3b704f5a5ec31a4c9f17027 100644 (file)
@@ -14,9 +14,5 @@ let on op f x y = op (f x) (f y)
 let comp f g x = f (g x)
 let (++) f g x = comp f g x
 
-let comp2 f g a b = f (g a b)
+let comp2  f g a b = ((++) ++ (++)) f g a b
 let (+++) f g a b = comp2 f g a b
-
-let (|>) a f = f a
-
-let ($) f a = f a
index e563ed05caf89529c757b7d19444767b7f0b53d5..634d946cdd9ef24683d8ec7aea8f71c83ca02ed5 100644 (file)
@@ -4,9 +4,3 @@ val id : 'a -> 'a
 val flip : ('a -> 'b -> 'c) -> ('b -> 'a -> 'c)
 val on : ('b -> 'b -> 'c) -> ('a -> 'b) -> 'a -> 'a -> 'c
 val comp : ('b -> 'c) -> ('a -> 'b) -> ('a -> 'c)
-val comp2 : ('b -> 'c) -> ('a1 -> 'a2 -> 'b) -> ('a1 -> 'a2 -> 'c)
-val (+++) : ('c -> 'd) -> ('a -> 'b -> 'c) -> 'a -> 'b -> 'd
-val (++) : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c
-(** Forward pipe operator: facilitates left-to-right function composition. *)
-val (|>) : 'a -> ('a -> 'b) -> 'b
-val ($) : ('a -> 'b) -> 'a -> 'b
index dcdb5a9e97569e88ef34f9b5312b6cf8ba9c4c84..e1f7532ee1d76c2163ae91b22b51c4ac2a347880 100644 (file)
@@ -170,4 +170,8 @@ let set_difference a b = List.filter (fun x -> not(List.mem x b)) a
 
 let assoc_default k l d =
   if List.mem_assoc k l then List.assoc k l else d
+
+(* Like the Lisp cons *)
+let cons a b = a :: b
+
 end
index 335dea41f21dd2e28346d6e17536b43f1b7e0a81..b1097b67f7c326f4fd90e5984795b617eccf6d54 100644 (file)
@@ -169,4 +169,7 @@ module List :
         is not in the list. *)
     val assoc_default : 'a -> ('a * 'b) list -> 'b -> 'b
 
+    (* Like Lisp cons*)
+    val cons : 'a -> 'a list -> 'a list
+
   end
index 92d6b51227a37124bcbca2b2c2217e0cd063b01d..31752088b1ccac1614847b3049d0fb348b59bc5c 100644 (file)
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU Lesser General Public License for more details.
  *)
+
+(* Perhaps it's better to use `option' from the ocaml-extlib extension
+ * to the standard library instead?  (Although it would not suffice,
+ * since it's not a super-set of our `opt'.)
+ * (http://code.google.com/p/ocaml-extlib/)
+ *)
+
+open Pervasiveext
+
 let iter f = function
        | Some x -> f x
        | None -> ()
@@ -43,3 +52,5 @@ let fold_right f opt accu =
        match opt with
        | Some x -> f x accu
        | None -> accu
+
+let cat_options a = List.map unbox (List.filter is_boxed a)
index 730c7f7eba520497c91bca25379308d67dacea66..aafa63ef5301a7a61dce76ef63b0bf0f96940e19 100644 (file)
@@ -19,3 +19,4 @@ val is_boxed : 'a option -> bool
 val to_list : 'a option -> 'a list
 val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b option -> 'a
 val fold_right : ('a -> 'b -> 'b) -> 'a option -> 'b -> 'b
+val cat_options : 'a option list -> 'a list
index 6db7189511b4b5fa31e50fbef2300717be3a0162..4c1dadf3660e8ca6efc04360c1844c3a7b61afeb 100644 (file)
@@ -27,6 +27,8 @@ let finally fct clean_f =
        clean_f ();
        result
 
+(* Those should go into the Opt module: *)
+
 let maybe_with_default d f v =
        match v with None -> d | Some x -> f x
 
@@ -53,3 +55,10 @@ let ignore_int32 v = let (_: int32) = v in ()
 let ignore_string v = let (_: string) = v in ()
 let ignore_float v = let (_: float) = v in ()
 let ignore_bool v = let (_: bool) = v in ()
+
+(* To avoid some parens: *)
+(* composition of functions: *)
+let (++) f g x = Fun.comp f g x
+
+(* and application *)
+let ($) f a = f a
index bedf46d2ba9e2299d93a43b106423b9f82a72bbf..49a734e7766c9fa5463e30e67f97fdd89229fc44 100644 (file)
@@ -25,3 +25,6 @@ val ignore_int64 : int64 -> unit
 val ignore_string : string -> unit
 val ignore_float : float -> unit
 val ignore_bool : bool -> unit
+
+val (++) : ('b -> 'c) -> ('a -> 'b) -> ('a -> 'c)
+val ($) : ('a -> 'b) -> 'a -> 'b