Skip to content

Commit 0625907

Browse files
committed
Reimplement Array.fold_left_map
as it was introduced in OCaml 4.13.
1 parent c8d9c72 commit 0625907

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

compiler/lib/stdlib.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,20 @@ module Array = struct
11221122
incr i
11231123
done;
11241124
!i = len_a
1125+
1126+
let fold_left_map ~f ~init input_array =
1127+
let len = length input_array in
1128+
if len = 0 then (init, [||]) else begin
1129+
let acc, elt = f init (unsafe_get input_array 0) in
1130+
let output_array = make len elt in
1131+
let acc = ref acc in
1132+
for i = 1 to len - 1 do
1133+
let acc', elt = f !acc (unsafe_get input_array i) in
1134+
acc := acc';
1135+
unsafe_set output_array i elt;
1136+
done;
1137+
!acc, output_array
1138+
end
11251139
end
11261140

11271141
module Filename = struct

0 commit comments

Comments
 (0)