add CCList.group_succ

This commit is contained in:
Simon Cruanes 2015-04-28 17:34:09 +02:00
parent b66caf67bf
commit 531134d754
2 changed files with 25 additions and 1 deletions

View file

@ -266,6 +266,25 @@ let uniq_succ ?(eq=(=)) l =
uniq_succ [1;1;2;3;1;6;6;4;6;1] = [1;2;3;1;6;4;6;1] uniq_succ [1;1;2;3;1;6;6;4;6;1] = [1;2;3;1;6;4;6;1]
*) *)
let group_succ ?(eq=(=)) l =
let rec f ~eq acc cur l = match cur, l with
| [], [] -> List.rev acc
| _::_, [] -> List.rev (List.rev cur :: acc)
| [], x::tl -> f ~eq acc [x] tl
| (y :: _), x :: tl when eq x y -> f ~eq acc (x::cur) tl
| _, x :: tl -> f ~eq (List.rev cur :: acc) [x] tl
in
f ~eq [] [] l
(*$T
group_succ [1;2;3;1;1;2;4] = [[1]; [2]; [3]; [1;1]; [2]; [4]]
group_succ [] = []
group_succ [1;1;1] = [[1;1;1]]
group_succ [1;2;2;2] = [[1]; [2;2;2]]
group_succ ~eq:(fun (x,_)(y,_)-> x=y) [1, 1; 1, 2; 1, 3; 2, 0] \
= [[1, 1; 1, 2; 1, 3]; [2, 0]]
*)
let sorted_merge_uniq ?(cmp=Pervasives.compare) l1 l2 = let sorted_merge_uniq ?(cmp=Pervasives.compare) l1 l2 =
let push ~cmp acc x = match acc with let push ~cmp acc x = match acc with
| [] -> [x] | [] -> [x]

View file

@ -54,7 +54,7 @@ val fold_while : ('a -> 'b -> 'a * [`Stop | `Continue]) -> 'a -> 'b t -> 'a
@since 0.8 *) @since 0.8 *)
val init : int -> (int -> 'a) -> 'a t val init : int -> (int -> 'a) -> 'a t
(** Same as [Array.init] (** Similar to {!Array.init}
@since 0.6 *) @since 0.6 *)
val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
@ -135,6 +135,11 @@ val uniq_succ : ?eq:('a -> 'a -> bool) -> 'a list -> 'a list
[uniq_succ [1;1;2] = [1;2]] [uniq_succ [1;1;2] = [1;2]]
@since 0.10 *) @since 0.10 *)
val group_succ : ?eq:('a -> 'a -> bool) -> 'a list -> 'a list list
(** [group_succ ~eq l] groups together consecutive elements that are equal
according to [eq]
@since NEXT_RELEASE *)
(** {2 Indices} *) (** {2 Indices} *)
module Idx : sig module Idx : sig