diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 4735b6a7..8951562c 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -1059,6 +1059,16 @@ let iteri f l = | x::l' -> f i x; aux f (i+1) l' in aux f 0 l +let iteri2 f l1 l2 = + let rec aux f i l1 l2 = match l1, l2 with + | [], [] -> () + | [], _ + | _, [] -> invalid_arg "iteri2" + | x1::l1', x2::l2' -> + f i x1 x2; + aux f (i+1) l1' l2' + in aux f 0 l1 l2 + let foldi f acc l = let rec foldi f acc i l = match l with | [] -> acc @@ -1068,6 +1078,17 @@ let foldi f acc l = in foldi f acc 0 l +let foldi2 f acc l1 l2 = + let rec foldi f acc i l1 l2 = match l1, l2 with + | [], [] -> acc + | [], _ + | _, [] -> invalid_arg "foldi2" + | x1::l1', x2::l2' -> + let acc = f acc i x1 x2 in + foldi f acc (i+1) l1' l2' + in + foldi f acc 0 l1 l2 + let rec get_at_idx_rec i l = match l with | [] -> raise Not_found | x::_ when i=0 -> x diff --git a/src/core/CCList.mli b/src/core/CCList.mli index 08e6b1a5..8513ca4a 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -334,9 +334,18 @@ val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t val iteri : (int -> 'a -> unit) -> 'a t -> unit +val iteri2 : (int -> 'a -> 'b -> unit) -> 'a t -> 'b t -> unit +(** @raise Invalid_argument when lists do not have the same length + @since NEXT_RELEASE *) + val foldi : ('b -> int -> 'a -> 'b) -> 'b -> 'a t -> 'b (** Fold on list, with index *) +val foldi2 : ('c -> int -> 'a -> 'b -> 'c) -> 'c -> 'a t -> 'b t -> 'c +(** Fold on two lists, with index + @raise Invalid_argument when lists do not have the same length + @since NEXT_RELEASE *) + val get_at_idx : int -> 'a t -> 'a option (** Get by index in the list. If the index is negative, it will get element starting from the end