Просмотр исходного кода

enhance: replace util/distinct-by

rcmerci 3 лет назад
Родитель
Сommit
c4f3e44be1
1 измененных файлов с 13 добавлено и 26 удалено
  1. 13 26
      src/main/frontend/util.cljc

+ 13 - 26
src/main/frontend/util.cljc

@@ -27,7 +27,8 @@
             [promesa.core :as p]
             [rum.core :as rum]
             [clojure.core.async :as async]
-            [cljs.core.async.impl.channels :refer [ManyToManyChannel]]))
+            [cljs.core.async.impl.channels :refer [ManyToManyChannel]]
+            [medley.core :as medley]))
   (:require
    [clojure.pprint]
    [clojure.string :as string]
@@ -471,30 +472,16 @@
   [s substr]
   (string/starts-with? s substr))
 
-(defn distinct-by
-  [f col]
-  (reduce
-   (fn [acc x]
-     (if (some #(= (f x) (f %)) acc)
-       acc
-       (vec (conj acc x))))
-   []
-   col))
-
-(defn distinct-by-last-wins
-  [f col]
-  (reduce
-   (fn [acc x]
-     (if (some #(= (f x) (f %)) acc)
-       (mapv
-        (fn [v]
-          (if (= (f x) (f v))
-            x
-            v))
-        acc)
-       (vec (conj acc x))))
-   []
-   col))
+
+#?(:cljs
+   (def distinct-by medley/distinct-by))
+
+#?(:cljs
+   (defn distinct-by-last-wins
+     [f col]
+     (if (sequential? col)
+       (reverse (distinct-by f (reverse col)))
+       (distinct-by f col))))
 
 (defn get-git-owner-and-repo
   [repo-url]
@@ -1040,7 +1027,7 @@
 
 #?(:clj
    (defmacro with-time
-     "Evaluates expr and prints the time it took. 
+     "Evaluates expr and prints the time it took.
       Returns the value of expr and the spent time of float number in msecs."
      [expr]
      `(let [start# (cljs.core/system-time)