diff --git a/core/CCList.ml b/core/CCList.ml index 7375f5d8..72d5ff87 100644 --- a/core/CCList.ml +++ b/core/CCList.ml @@ -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 diff --git a/core/CCList.mli b/core/CCList.mli index f835ef4c..281a6616 100644 --- a/core/CCList.mli +++ b/core/CCList.mli @@ -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