CCList.sort_uniq

This commit is contained in:
Simon Cruanes 2014-07-02 16:47:10 +02:00
parent b521f3af8e
commit 9595ac688c
2 changed files with 16 additions and 0 deletions

View file

@ -179,6 +179,19 @@ let sorted_merge ?(cmp=Pervasives.compare) l1 l2 =
= [11; 20; 101; 200]
*)
let sort_uniq (type elt) ?(cmp=Pervasives.compare) l =
let module S = Set.Make(struct
type t = elt
let compare = cmp
end) in
let set = fold_right S.add l S.empty in
S.elements set
(*$T
sort_uniq [1;2;5;3;6;1;4;2;3] = [1;2;3;4;5;6]
sort_uniq [] = []
sort_uniq [10;10;10;10;1;10] = [1;10]
*)
let take n l =
let rec direct i n l = match l with

View file

@ -101,6 +101,9 @@ val filter_map : ('a -> 'b option) -> 'a t -> 'b t
val sorted_merge : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list -> 'a list
(** merges elements from both sorted list, removing duplicates *)
val sort_uniq : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list
(** Sort the list and remove duplicate elements *)
(** {2 Indices} *)
module Idx : sig