Explorar el Código

enhance: replace util/distinct-by

rcmerci hace 3 años
padre
commit
c4f3e44be1
Se han modificado 1 ficheros con 13 adiciones y 26 borrados
  1. 13 26
      src/main/frontend/util.cljc

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

@@ -27,7 +27,8 @@
             [promesa.core :as p]
             [promesa.core :as p]
             [rum.core :as rum]
             [rum.core :as rum]
             [clojure.core.async :as async]
             [clojure.core.async :as async]
-            [cljs.core.async.impl.channels :refer [ManyToManyChannel]]))
+            [cljs.core.async.impl.channels :refer [ManyToManyChannel]]
+            [medley.core :as medley]))
   (:require
   (:require
    [clojure.pprint]
    [clojure.pprint]
    [clojure.string :as string]
    [clojure.string :as string]
@@ -471,30 +472,16 @@
   [s substr]
   [s substr]
   (string/starts-with? 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
 (defn get-git-owner-and-repo
   [repo-url]
   [repo-url]
@@ -1040,7 +1027,7 @@
 
 
 #?(:clj
 #?(:clj
    (defmacro with-time
    (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."
       Returns the value of expr and the spent time of float number in msecs."
      [expr]
      [expr]
      `(let [start# (cljs.core/system-time)
      `(let [start# (cljs.core/system-time)