Skip to content

Commit

Permalink
Add lazylist equal predicate (#811)
Browse files Browse the repository at this point in the history
added LazyList.equal
  • Loading branch information
mars0i authored and UnixJunkie committed Jul 31, 2019
1 parent 3180492 commit 60a0bcf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/batLazyList.ml
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,21 @@ let for_all2 p l1 l2 =
| (Cons _, Nil) | (Nil, Cons _) -> raise (Different_list_size "LazyList.for_all2")
in aux l1 l2

let equal eq l1 l2 =
let rec aux l1 l2 =
match (next l1, next l2) with
| (Cons (h1, t1), Cons (h2, t2)) -> eq h1 h2 && (aux t1 t2)
| (Nil, Nil) -> true
| (Cons _, Nil) | (Nil, Cons _) -> false
in aux l1 l2

(*$T equal
equal (equal (=)) (init 3 (range 0)) (init 3 (range 0))
not (equal (equal (=)) (of_list [(of_list [0; 1; 2])]) (of_list [(of_list [0; 42; 2])]))
not (equal (=) (range 0 2) (range 0 3))
not (equal (=) (range 0 3) (range 0 2))
*)

let exists2 p l1 l2 =
let rec aux l1 l2 =
match (next l1, next l2) with
Expand Down
22 changes: 22 additions & 0 deletions src/batLazyList.mli
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,28 @@ val for_all2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
@raise Different_list_size if the two lists have
different lengths. *)

val equal : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
(** [equal eq s1 s2] compares elements of [s1] and [s2] pairwise using [eq]
and returns true if all elements pass the test and the lists have the same
length; otherwise it returns false. Examples:
{[
equal (=) (range 0 4) (range 0 4) (* true *)
(* Make lazy lists of lazy lists: *)
let s1 = init 5 (range 0)
let s2 = init 5 (range 0)
equal (equal (=)) s1 s2 (* true *)
]}
(Calling [=] directly on a pair of lazy lists may succeed but is not
guaranteed to behave consistently.)
Note that on lists of equal length, [equal] and [for_all2] can perform
the same function; their intended uses differ, however, as signaled by
behavior on lists of different lengths.
*)

val exists2 : ('a -> 'b -> bool) -> 'a t -> 'b t -> bool
(** Same as {!exists}, but for a two-argument predicate.
@raise Different_list_size if the two lists have
Expand Down

0 comments on commit 60a0bcf

Please sign in to comment.