From 0625907302f012d30d7faf9dc943299bd3a54dcc Mon Sep 17 00:00:00 2001 From: Olivier Nicole Date: Mon, 22 May 2023 14:45:15 +0200 Subject: [PATCH] Reimplement Array.fold_left_map as it was introduced in OCaml 4.13. --- compiler/lib/stdlib.ml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/compiler/lib/stdlib.ml b/compiler/lib/stdlib.ml index ef8aeddca2..3fbd4fb111 100644 --- a/compiler/lib/stdlib.ml +++ b/compiler/lib/stdlib.ml @@ -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