File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -233,6 +233,15 @@ module Stdlib = struct
233
233
else loop (succ i) in
234
234
loop 0
235
235
236
+ let fold_left2 f x a1 a2 =
237
+ if Array. length a1 <> Array. length a2
238
+ then invalid_arg " Misc.Stdlib.Array.fold_left2" ;
239
+ let r = ref x in
240
+ for i = 0 to Array. length a1 - 1 do
241
+ r := f ! r (Array. unsafe_get a1 i) (Array. unsafe_get a2 i)
242
+ done ;
243
+ ! r
244
+
236
245
let for_alli p a =
237
246
let n = Array. length a in
238
247
let rec loop i =
@@ -260,6 +269,17 @@ module Stdlib = struct
260
269
false
261
270
in
262
271
loop 0
272
+
273
+ let map_sharing f a =
274
+ let same = ref true in
275
+ let f' x =
276
+ let x' = f x in
277
+ if x != x' then
278
+ same := false ;
279
+ x'
280
+ in
281
+ let a' = (Array. map [@ inlined hint]) f' a in
282
+ if ! same then a else a'
263
283
end
264
284
265
285
module String = struct
Original file line number Diff line number Diff line change @@ -192,6 +192,14 @@ module Stdlib : sig
192
192
val exists2 : ('a -> 'b -> bool ) -> 'a array -> 'b array -> bool
193
193
(* * Same as [Array.exists2] from the standard library. *)
194
194
195
+ val fold_left2 :
196
+ ('acc -> 'a -> 'b -> 'acc ) -> 'acc -> 'a array -> 'b array -> 'acc
197
+ (* * [fold_left2 f init [|a1; ...; an|] [|b1; ...; bn|]] is
198
+ [f (... (f (f init a1 b1) a2 b2) ...) an bn].
199
+ @raise Invalid_argument if the two arrays are determined
200
+ to have different lengths.
201
+ *)
202
+
195
203
val for_alli : (int -> 'a -> bool ) -> 'a array -> bool
196
204
(* * Same as [Array.for_all] from the standard library, but the
197
205
function is applied with the index of the element as first argument,
@@ -202,6 +210,10 @@ module Stdlib : sig
202
210
val equal : ('a -> 'a -> bool ) -> 'a array -> 'a array -> bool
203
211
(* * Compare two arrays for equality, using the supplied predicate for
204
212
element equality *)
213
+
214
+ val map_sharing : ('a -> 'a ) -> 'a array -> 'a array
215
+ (* * [map_sharing f a] is [map f a]. If for all elements of the array
216
+ [f e == e] then [map_sharing f a == a] *)
205
217
end
206
218
207
219
(* * {2 Extensions to the String module} *)
You can’t perform that action at this time.
0 commit comments