Skip to content

Commit

Permalink
Reimplement Array.fold_left_map
Browse files Browse the repository at this point in the history
as it was introduced in OCaml 4.13.
  • Loading branch information
OlivierNicole committed May 22, 2023
1 parent c8d9c72 commit 0625907
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions compiler/lib/stdlib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,20 @@ module Array = struct
incr i
done;
!i = len_a

let fold_left_map ~f ~init input_array =
let len = length input_array in
if len = 0 then (init, [||]) else begin
let acc, elt = f init (unsafe_get input_array 0) in
let output_array = make len elt in
let acc = ref acc in
for i = 1 to len - 1 do
let acc', elt = f !acc (unsafe_get input_array i) in
acc := acc';
unsafe_set output_array i elt;
done;
!acc, output_array
end
end

module Filename = struct
Expand Down

0 comments on commit 0625907

Please sign in to comment.