From 2d349b606f3fecf2e13b692e9f72377edef3b132 Mon Sep 17 00:00:00 2001 From: Vincent Bernardoff Date: Thu, 4 Jan 2018 17:10:24 +0100 Subject: [PATCH] TzString: add `fold_left` --- lib_stdlib/tzString.ml | 5 +++++ lib_stdlib/tzString.mli | 3 +++ 2 files changed, 8 insertions(+) diff --git a/lib_stdlib/tzString.ml b/lib_stdlib/tzString.ml index 5941d05fd..323ac2ec3 100644 --- a/lib_stdlib/tzString.ml +++ b/lib_stdlib/tzString.ml @@ -73,3 +73,8 @@ let mem_char s c = match String.index s c with | exception Not_found -> false | _ -> true + +let fold_left f init s = + let acc = ref init in + String.iter (fun c -> acc := f !acc c) s ; + !acc diff --git a/lib_stdlib/tzString.mli b/lib_stdlib/tzString.mli index 1d3f834ce..e2548eac5 100644 --- a/lib_stdlib/tzString.mli +++ b/lib_stdlib/tzString.mli @@ -29,3 +29,6 @@ val common_prefix: string -> string -> int (** Test whether a string contains a given character *) val mem_char: string -> char -> bool + +(** Functional iteration over the characters of a string from first to last *) +val fold_left : ('a -> char -> 'a) -> 'a -> string -> 'a