Commits

Anonymous committed 637e14b

+ List.uniq

  • Participants
  • Parent commits 7686f5b

Comments (0)

Files changed (1)

File src/cd_List.ml

     ;
 
 
+    value rec uniq_eq ~eq acc lst =
+      match lst with
+      [ [] -> acc
+      | [h :: t] ->
+          let acc =
+            if List.exists (eq h) acc
+            then acc
+            else [h :: acc]
+          in
+            uniq_eq ~eq acc t
+      ]
+    ;
 
+    value uniq_is_short lst =
+      match lst with
+      [ [] | [_ :: []] -> True
+      | _ -> False
+      ]
+    ;
+
+    (* todo: ~cmp / ~hash for faster uniq.
+       [?stable] = True -> list items won't be reordered
+                 = False -> may be reordered for better performance
+     *)
+    value uniq ?(stable=True) ?eq lst =
+      match eq with
+      [ None -> invalid_arg "List.uniq: no ~eq specified"
+      | Some eq ->
+          if uniq_is_short lst
+          then lst
+          else
+            let res_rev = uniq_eq ~eq [] lst in
+            if stable
+            then List.rev res_rev
+            else res_rev
+      ]
+    ;
 
     (* ** в Cd_List реализовал похожее, потом надо будет слить.