Merge pull request #176 from orbifx/master

Add `CCList.iteri2` and `CCList.foldi2`
This commit is contained in:
Simon Cruanes 2018-01-18 12:21:46 -06:00 committed by GitHub
commit 829ceeb147
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View file

@ -1059,6 +1059,16 @@ let iteri f l =
| x::l' -> f i x; aux f (i+1) l' | x::l' -> f i x; aux f (i+1) l'
in aux f 0 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 foldi f acc l =
let rec foldi f acc i l = match l with let rec foldi f acc i l = match l with
| [] -> acc | [] -> acc
@ -1068,6 +1078,17 @@ let foldi f acc l =
in in
foldi f acc 0 l 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 let rec get_at_idx_rec i l = match l with
| [] -> raise Not_found | [] -> raise Not_found
| x::_ when i=0 -> x | x::_ when i=0 -> x

View file

@ -334,9 +334,18 @@ val mapi : (int -> 'a -> 'b) -> 'a t -> 'b t
val iteri : (int -> 'a -> unit) -> 'a t -> unit 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 val foldi : ('b -> int -> 'a -> 'b) -> 'b -> 'a t -> 'b
(** Fold on list, with index *) (** 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 val get_at_idx : int -> 'a t -> 'a option
(** Get by index in the list. (** Get by index in the list.
If the index is negative, it will get element starting from the end If the index is negative, it will get element starting from the end