From 45a295d0522d79714df01a4196fd1751ed6ed43b Mon Sep 17 00:00:00 2001 From: Matthias Goergens Date: Tue, 26 Jan 2010 15:01:01 +0000 Subject: [PATCH] Adding extensions to some modules in the extended standard library: - to the Map module - to the List module - and some functional combinators (like compose with (++)) in Fun module Signed-off-by: Matthias Goergens --- stdext/Makefile | 4 ++-- stdext/fun.ml | 6 +----- stdext/fun.mli | 6 ------ stdext/listext.ml | 4 ++++ stdext/listext.mli | 3 +++ stdext/opt.ml | 11 +++++++++++ stdext/opt.mli | 1 + stdext/pervasiveext.ml | 9 +++++++++ stdext/pervasiveext.mli | 3 +++ 9 files changed, 34 insertions(+), 13 deletions(-) diff --git a/stdext/Makefile b/stdext/Makefile index 29380ef..3ba7b30 100644 --- a/stdext/Makefile +++ b/stdext/Makefile @@ -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 diff --git a/stdext/fun.ml b/stdext/fun.ml index 4088cac..8cd5d9a 100644 --- a/stdext/fun.ml +++ b/stdext/fun.ml @@ -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 diff --git a/stdext/fun.mli b/stdext/fun.mli index e563ed0..634d946 100644 --- a/stdext/fun.mli +++ b/stdext/fun.mli @@ -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 diff --git a/stdext/listext.ml b/stdext/listext.ml index dcdb5a9..e1f7532 100644 --- a/stdext/listext.ml +++ b/stdext/listext.ml @@ -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 diff --git a/stdext/listext.mli b/stdext/listext.mli index 335dea4..b1097b6 100644 --- a/stdext/listext.mli +++ b/stdext/listext.mli @@ -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 diff --git a/stdext/opt.ml b/stdext/opt.ml index 92d6b51..3175208 100644 --- a/stdext/opt.ml +++ b/stdext/opt.ml @@ -11,6 +11,15 @@ * 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) diff --git a/stdext/opt.mli b/stdext/opt.mli index 730c7f7..aafa63e 100644 --- a/stdext/opt.mli +++ b/stdext/opt.mli @@ -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 diff --git a/stdext/pervasiveext.ml b/stdext/pervasiveext.ml index 6db7189..4c1dadf 100644 --- a/stdext/pervasiveext.ml +++ b/stdext/pervasiveext.ml @@ -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 diff --git a/stdext/pervasiveext.mli b/stdext/pervasiveext.mli index bedf46d..49a734e 100644 --- a/stdext/pervasiveext.mli +++ b/stdext/pervasiveext.mli @@ -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 -- 2.39.5