From b81fafbb8fbcba0cd54ea93fd44e6bd69540324a Mon Sep 17 00:00:00 2001 From: Hugo Herbelin Date: Thu, 5 Apr 2018 11:33:30 +0200 Subject: CArray: adding combinators. --- clib/cArray.mli | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'clib/cArray.mli') diff --git a/clib/cArray.mli b/clib/cArray.mli index 9c2f521f4a..56cc3656d4 100644 --- a/clib/cArray.mli +++ b/clib/cArray.mli @@ -86,9 +86,16 @@ sig (** [smartmap f a] behaves as [map f a] but returns [a] instead of a copy when [f x == x] for all [x] in [a]. *) + val smartmap2 : ('a -> 'b -> 'b) -> 'a array -> 'b array -> 'b array + (** [smartmap2 f a b] behaves as [map2 f a b] but returns [a] instead of a copy when + [f x y == y] for all [x] in [a] and [y] in [b] pointwise. *) + val smartfoldmap : ('r -> 'a -> 'r * 'a) -> 'r -> 'a array -> 'r * 'a array (** Same as [smartmap] but threads an additional state left-to-right. *) + val smartfoldmap2 : ('r -> 'a -> 'b -> 'r * 'b) -> 'r -> 'a array -> 'b array -> 'r * 'b array + (** Same as [smartmap2] but threads an additional state left-to-right. *) + val map2 : ('a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array val map2_i : (int -> 'a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array val map3 : @@ -152,6 +159,9 @@ sig val iter : ('r -> 'a -> unit) -> 'r -> 'a array -> unit (** [Fun1.iter f x v = iter (f x) v] *) + val iter2 : ('r -> 'a -> 'b -> unit) -> 'r -> 'a array -> 'b array -> unit + (** [Fun1.iter2 f x v1 v2 = iter (f x) v1 v2] *) + end (** The functions defined in this module are the same as the main ones, except that they are all higher-order, and their function arguments have an -- cgit v1.2.3 From 4e207da568b31fb3fd097fb584f4722bd7166fcf Mon Sep 17 00:00:00 2001 From: Hugo Herbelin Date: Sun, 29 Apr 2018 21:51:05 +0200 Subject: Emphasizing the "smart"ness of Array.smartfoldmap{,2} in their documentation. This follows the model of smartmap and smartmap2. --- clib/cArray.mli | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'clib/cArray.mli') diff --git a/clib/cArray.mli b/clib/cArray.mli index 56cc3656d4..c461d856be 100644 --- a/clib/cArray.mli +++ b/clib/cArray.mli @@ -91,10 +91,14 @@ sig [f x y == y] for all [x] in [a] and [y] in [b] pointwise. *) val smartfoldmap : ('r -> 'a -> 'r * 'a) -> 'r -> 'a array -> 'r * 'a array - (** Same as [smartmap] but threads an additional state left-to-right. *) + (** [smartfoldmap f a b] behaves as [fold_left_map] but + returns [b] as second component instead of a copy of [b] when + the output array is pointwise the same as the input array [b] *) val smartfoldmap2 : ('r -> 'a -> 'b -> 'r * 'b) -> 'r -> 'a array -> 'b array -> 'r * 'b array - (** Same as [smartmap2] but threads an additional state left-to-right. *) + (** [smartfoldmap2 f a b c] behaves as [fold_left2_map] but + returns [c] as second component instead of a copy of [c] when + the output array is pointwise the same as the input array [c] *) val map2 : ('a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array val map2_i : (int -> 'a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array -- cgit v1.2.3 From f2ab2825077bf8344d2e55be433efb1891212589 Mon Sep 17 00:00:00 2001 From: Hugo Herbelin Date: Wed, 23 May 2018 13:34:22 +0200 Subject: Collecting Array.smart_* functions into a module Array.Smart. --- clib/cArray.mli | 55 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 17 deletions(-) (limited to 'clib/cArray.mli') diff --git a/clib/cArray.mli b/clib/cArray.mli index c461d856be..d800e79ed9 100644 --- a/clib/cArray.mli +++ b/clib/cArray.mli @@ -83,24 +83,14 @@ sig Raise [Failure "Array.chop"] if [i] is not a valid index. *) val smartmap : ('a -> 'a) -> 'a array -> 'a array - (** [smartmap f a] behaves as [map f a] but returns [a] instead of a copy when - [f x == x] for all [x] in [a]. *) - - val smartmap2 : ('a -> 'b -> 'b) -> 'a array -> 'b array -> 'b array - (** [smartmap2 f a b] behaves as [map2 f a b] but returns [a] instead of a copy when - [f x y == y] for all [x] in [a] and [y] in [b] pointwise. *) + [@@ocaml.deprecated "Same as [Smart.map]"] val smartfoldmap : ('r -> 'a -> 'r * 'a) -> 'r -> 'a array -> 'r * 'a array - (** [smartfoldmap f a b] behaves as [fold_left_map] but - returns [b] as second component instead of a copy of [b] when - the output array is pointwise the same as the input array [b] *) - - val smartfoldmap2 : ('r -> 'a -> 'b -> 'r * 'b) -> 'r -> 'a array -> 'b array -> 'r * 'b array - (** [smartfoldmap2 f a b c] behaves as [fold_left2_map] but - returns [c] as second component instead of a copy of [c] when - the output array is pointwise the same as the input array [c] *) + [@@ocaml.deprecated "Same as [Smart.fold_left_map]"] val map2 : ('a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array + (** See also [Smart.map2] *) + val map2_i : (int -> 'a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array val map3 : ('a -> 'b -> 'c -> 'd) -> 'a array -> 'b array -> 'c array -> 'd array @@ -113,13 +103,13 @@ sig val fold_left_map : ('a -> 'b -> 'a * 'c) -> 'a -> 'b array -> 'a * 'c array (** [fold_left_map f e_0 [|l_1...l_n|] = e_n,[|k_1...k_n|]] - where [(e_i,k_i)=f e_{i-1} l_i] *) + where [(e_i,k_i)=f e_{i-1} l_i]; see also [Smart.fold_left_map] *) val fold_right_map : ('a -> 'c -> 'b * 'c) -> 'a array -> 'c -> 'b array * 'c (** Same, folding on the right *) val fold_left2_map : ('a -> 'b -> 'c -> 'a * 'd) -> 'a -> 'b array -> 'c array -> 'a * 'd array - (** Same with two arrays, folding on the left *) + (** Same with two arrays, folding on the left; see also [Smart.fold_left2_map] *) val fold_right2_map : ('a -> 'b -> 'c -> 'd * 'c) -> 'a array -> 'b array -> 'c -> 'd array * 'c (** Same with two arrays, folding on the left *) @@ -148,6 +138,31 @@ sig (** [filter_with b a] selects elements of [a] whose corresponding element in [b] is [true]. Raise [Invalid_argument _] when sizes differ. *) + module Smart : + sig + val map : ('a -> 'a) -> 'a array -> 'a array + (** [Smart.map f a] behaves as [map f a] but returns [a] instead of a copy when + [f x == x] for all [x] in [a]. *) + + val map2 : ('a -> 'b -> 'b) -> 'a array -> 'b array -> 'b array + (** [Smart.map2 f a b] behaves as [map2 f a b] but returns [a] instead of a copy when + [f x y == y] for all [x] in [a] and [y] in [b] pointwise. *) + + val fold_left_map : ('a -> 'b -> 'a * 'b) -> 'a -> 'b array -> 'a * 'b array + (** [Smart.fold_left_mapf a b] behaves as [fold_left_map] but + returns [b] as second component instead of a copy of [b] when + the output array is pointwise the same as the input array [b] *) + + val fold_left2_map : ('a -> 'b -> 'c -> 'a * 'c) -> 'a -> 'b array -> 'c array -> 'a * 'c array + (** [Smart.fold_left2_map f a b c] behaves as [fold_left2_map] but + returns [c] as second component instead of a copy of [c] when + the output array is pointwise the same as the input array [c] *) + + end + (** The functions defined in this module are optimized specializations + of the main ones, when the returned array is of same type as one of + the original array. *) + end include ExtS @@ -158,7 +173,7 @@ sig (** [Fun1.map f x v = map (f x) v] *) val smartmap : ('r -> 'a -> 'a) -> 'r -> 'a array -> 'a array - (** [Fun1.smartmap f x v = smartmap (f x) v] *) + [@@ocaml.deprecated "Same as [Fun1.Smart.map]"] val iter : ('r -> 'a -> unit) -> 'r -> 'a array -> unit (** [Fun1.iter f x v = iter (f x) v] *) @@ -166,6 +181,12 @@ sig val iter2 : ('r -> 'a -> 'b -> unit) -> 'r -> 'a array -> 'b array -> unit (** [Fun1.iter2 f x v1 v2 = iter (f x) v1 v2] *) + module Smart : + sig + val map : ('r -> 'a -> 'a) -> 'r -> 'a array -> 'a array + (** [Fun1.Smart.map f x v = Smart.map (f x) v] *) + end + end (** The functions defined in this module are the same as the main ones, except that they are all higher-order, and their function arguments have an -- cgit v1.2.3 From 944f62d08b7d78bcb20dd12ba138891d432b5987 Mon Sep 17 00:00:00 2001 From: Hugo Herbelin Date: Sun, 13 May 2018 15:21:10 +0200 Subject: Exporting Fun1 within Array so that Array.Fun1 and not only CArray.Fun1 works. --- clib/cArray.mli | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'clib/cArray.mli') diff --git a/clib/cArray.mli b/clib/cArray.mli index d800e79ed9..8bf33f82f9 100644 --- a/clib/cArray.mli +++ b/clib/cArray.mli @@ -163,32 +163,32 @@ sig of the main ones, when the returned array is of same type as one of the original array. *) -end - -include ExtS + module Fun1 : + sig + val map : ('r -> 'a -> 'b) -> 'r -> 'a array -> 'b array + (** [Fun1.map f x v = map (f x) v] *) -module Fun1 : -sig - val map : ('r -> 'a -> 'b) -> 'r -> 'a array -> 'b array - (** [Fun1.map f x v = map (f x) v] *) + val smartmap : ('r -> 'a -> 'a) -> 'r -> 'a array -> 'a array + [@@ocaml.deprecated "Same as [Fun1.Smart.map]"] - val smartmap : ('r -> 'a -> 'a) -> 'r -> 'a array -> 'a array - [@@ocaml.deprecated "Same as [Fun1.Smart.map]"] + val iter : ('r -> 'a -> unit) -> 'r -> 'a array -> unit + (** [Fun1.iter f x v = iter (f x) v] *) - val iter : ('r -> 'a -> unit) -> 'r -> 'a array -> unit - (** [Fun1.iter f x v = iter (f x) v] *) + val iter2 : ('r -> 'a -> 'b -> unit) -> 'r -> 'a array -> 'b array -> unit + (** [Fun1.iter2 f x v1 v2 = iter (f x) v1 v2] *) - val iter2 : ('r -> 'a -> 'b -> unit) -> 'r -> 'a array -> 'b array -> unit - (** [Fun1.iter2 f x v1 v2 = iter (f x) v1 v2] *) + module Smart : + sig + val map : ('r -> 'a -> 'a) -> 'r -> 'a array -> 'a array + (** [Fun1.Smart.map f x v = Smart.map (f x) v] *) + end - module Smart : - sig - val map : ('r -> 'a -> 'a) -> 'r -> 'a array -> 'a array - (** [Fun1.Smart.map f x v = Smart.map (f x) v] *) end + (** The functions defined in this module are the same as the main ones, except + that they are all higher-order, and their function arguments have an + additional parameter. This allows us to prevent closure creation in critical + cases. *) end -(** The functions defined in this module are the same as the main ones, except - that they are all higher-order, and their function arguments have an - additional parameter. This allows us to prevent closure creation in critical - cases. *) + +include ExtS -- cgit v1.2.3