mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 11:45:31 -05:00
feat(CCString): add CCString.foldi
This commit is contained in:
parent
5ad8914e4c
commit
4ad331fbe3
3 changed files with 16 additions and 0 deletions
|
|
@ -760,6 +760,12 @@ let fold f acc s =
|
|||
else fold_rec f (f acc s.[i]) s (i+1)
|
||||
in fold_rec f acc s 0
|
||||
|
||||
let foldi f acc s =
|
||||
let rec fold_rec f acc s i =
|
||||
if i = String.length s then acc
|
||||
else fold_rec f (f acc i s.[i]) s (i+1)
|
||||
in fold_rec f acc s 0
|
||||
|
||||
let pad ?(side=`Left) ?(c=' ') n s =
|
||||
let len_s = String.length s in
|
||||
if len_s >= n then s
|
||||
|
|
|
|||
|
|
@ -34,6 +34,11 @@ val fold : ('a -> char -> 'a) -> 'a -> t -> 'a
|
|||
(** [fold f init s] folds on chars by increasing index. Computes [f(… (f (f init s.[0]) s.[1]) …) s.[n-1]].
|
||||
@since 0.7 *)
|
||||
|
||||
val foldi : ('a -> int -> char -> 'a) -> 'a -> t -> 'a
|
||||
(** [foldi f init s] is just like {!fold}, but it also passes in the index of each chars
|
||||
as second argument to the folded function [f].
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
(** {2 Conversions} *)
|
||||
|
||||
val to_gen : t -> char gen
|
||||
|
|
|
|||
|
|
@ -34,6 +34,11 @@ val fold : f:('a -> char -> 'a) -> init:'a -> t -> 'a
|
|||
(** [fold ~f ~init s] folds on chars by increasing index. Computes [f(… (f (f init s.[0]) s.[1]) …) s.[n-1]].
|
||||
@since 0.7 *)
|
||||
|
||||
val foldi : f:('a -> int -> char -> 'a) -> 'a -> t -> 'a
|
||||
(** [foldi ~f init s] is just like {!fold}, but it also passes in the index of each chars
|
||||
as second argument to the folded function [f].
|
||||
@since NEXT_RELEASE *)
|
||||
|
||||
(** {2 Conversions} *)
|
||||
|
||||
val to_gen : t -> char gen
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue