Skip to content

Commit bdf7014

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

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

compiler/lib/stdlib.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,21 @@ 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
1129+
then init, [||]
1130+
else
1131+
let acc, elt = f init (unsafe_get input_array 0) in
1132+
let output_array = make len elt in
1133+
let acc = ref acc in
1134+
for i = 1 to len - 1 do
1135+
let acc', elt = f !acc (unsafe_get input_array i) in
1136+
acc := acc';
1137+
unsafe_set output_array i elt
1138+
done;
1139+
!acc, output_array
11251140
end
11261141

11271142
module Filename = struct

0 commit comments

Comments
 (0)