Add a tail-recursive implementation of List.combine

Closes #108.
This commit is contained in:
LemonBoy 2017-04-18 10:45:28 +02:00
parent af6cd08ff4
commit 404fede54a
3 changed files with 25 additions and 0 deletions

View file

@ -19,3 +19,4 @@
- Malcolm Matalka (`orbitz`)
- David Sheets (@dsheets)
- Glenn Slotte (glennsl)
- @LemonBoy

View file

@ -339,6 +339,26 @@ let partition_map f l =
assert_equal [1;3] l2
*)
let combine l1 l2 =
let rec direct i l1 l2 = match l1, l2 with
| ([], []) -> []
| _ when i=0 -> safe l1 l2 []
| (x1::l1', x2::l2') -> (x1, x2) :: direct (i-1) l1' l2'
| (_, _) -> invalid_arg "CCList.combine"
and safe l1 l2 acc = match l1, l2 with
| ([], []) -> List.rev acc
| (x1::l1', x2::l2') -> safe l1' l2' @@ (x1, x2) :: acc
| (_, _) -> invalid_arg "CCList.combine"
in
direct direct_depth_default_ l1 l2
(*$T
try ignore (combine [1] []); false with Invalid_argument _ -> true
try ignore (combine (1--1001) (1--1002)); false with Invalid_argument _ -> true
combine [1;2;3] [3;2;1] = List.combine [1;2;3] [3;2;1]
combine (1 -- 100_000) (1 -- 100_000) = List.combine (1 -- 100_000) (1 -- 100_000)
*)
let return x = [x]
let (>>=) l f = flat_map f l

View file

@ -72,6 +72,10 @@ val init : int -> (int -> 'a) -> 'a t
(** Similar to {!Array.init}
@since 0.6 *)
val combine : ('a list) -> ('b list) -> ('a * 'b) list
(** Similar to {!List.combine} but tail-recursive
@since NEXT_RELEASE *)
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool