[sledge] Add Array.contains_adjacent_duplicate

Reviewed By: ngorogiannis

Differential Revision: D28907803

fbshipit-source-id: 0bcabaf34
master
Josh Berdine 3 years ago committed by Facebook GitHub Bot
parent 69c4f089bd
commit 2378068442

@ -89,6 +89,25 @@ let iter xs ~f = iter ~f xs
let iteri xs ~f = iteri ~f xs
let exists xs ~f = exists ~f xs
let for_all xs ~f = for_all ~f xs
let contains_adjacent_duplicate ~eq xs =
let len = length xs in
if len < 2 then false
else
let contains_dup = ref false in
let idx = ref 1 in
let prev = ref xs.(0) in
while !idx < len do
let curr = xs.(!idx) in
if eq !prev curr then (
contains_dup := true ;
idx := len )
else (
prev := curr ;
incr idx )
done ;
!contains_dup
let fold xs init ~f = fold ~f:(fun s x -> f x s) ~init xs
let fold_right xs init ~f = fold_right ~f ~init xs

@ -27,6 +27,7 @@ val combine : 'a t -> 'b t -> ('a * 'b) t option
val combine_exn : 'a t -> 'b t -> ('a * 'b) t
val is_empty : 'a t -> bool
val mem : 'a -> 'a t -> eq:('a -> 'a -> bool) -> bool
val contains_adjacent_duplicate : eq:('a -> 'a -> bool) -> 'a t -> bool
val iter : 'a t -> f:('a -> unit) -> unit
val iteri : 'a t -> f:(int -> 'a -> unit) -> unit
val exists : 'a t -> f:('a -> bool) -> bool

Loading…
Cancel
Save