Browse Source

Merge pull request #7779 from logseq/enhance/replace-distinct-by

enhance: replace util/distinct-by
rcmerci 2 years ago
parent
commit
1515b2982c

+ 7 - 7
deps/graph-parser/deps.edn

@@ -4,20 +4,20 @@
  {com.andrewmcveigh/cljs-time {:git/url "https://github.com/logseq/cljs-time" ;; fork
                                :sha     "5704fbf48d3478eedcf24d458c8964b3c2fd59a9"}
   ;; local dep
-  logseq/db {:local/root "../db"}
+  logseq/db                   {:local/root "../db"}
   ;; stubbed in nbb
-  com.lambdaisland/glogi {:mvn/version "1.1.144"}
+  com.lambdaisland/glogi      {:mvn/version "1.1.144"}
   ;; built in to nbb
-  cljs-bean/cljs-bean {:mvn/version "1.5.0"}}
+  cljs-bean/cljs-bean         {:mvn/version "1.5.0"}}
 
  :aliases
  ;; This runs tests with nodejs. Would be nice to run this with in a browser env
  ;; since this is how its normally run in the app but this requires more setup
  ;; with karma, shadow-cljs.edn and headless mode on CI
  {:test {:extra-paths ["test"]
-         :extra-deps {olical/cljs-test-runner {:mvn/version "3.8.0"}
-                      org.clojure/clojurescript {:mvn/version "1.11.54"}}
-         :main-opts ["-m" "cljs-test-runner.main"]}
+         :extra-deps  {olical/cljs-test-runner   {:mvn/version "3.8.0"}
+                       org.clojure/clojurescript {:mvn/version "1.11.54"}}
+         :main-opts   ["-m" "cljs-test-runner.main"]}
 
   :clj-kondo {:replace-deps {clj-kondo/clj-kondo {:mvn/version "2022.12.08"}}
-              :main-opts  ["-m" "clj-kondo.main"]}}}
+              :main-opts    ["-m" "clj-kondo.main"]}}}

+ 13 - 9
deps/graph-parser/src/logseq/graph_parser/util.cljs

@@ -157,16 +157,20 @@
            (map string/capitalize)
            (string/join " ")))
 
+
 (defn distinct-by
-  "Copy of frontend.util/distinct-by. Too basic to couple to main app"
-  [f col]
-  (reduce
-   (fn [acc x]
-     (if (some #(= (f x) (f %)) acc)
-       acc
-       (vec (conj acc x))))
-   []
-   col))
+  "Copy from medley"
+  [f coll]
+  (let [step (fn step [xs seen]
+               (lazy-seq
+                ((fn [[x :as xs] seen]
+                   (when-let [s (seq xs)]
+                     (let [fx (f x)]
+                       (if (contains? seen fx)
+                         (recur (rest s) seen)
+                         (cons x (step (rest s) (conj seen fx)))))))
+                 xs seen)))]
+    (step (seq coll) #{})))
 
 (defn normalize-format
   [format]

+ 3 - 8
src/electron/electron/search.cljs

@@ -5,7 +5,8 @@
             ["better-sqlite3" :as sqlite3]
             [clojure.string :as string]
             ["electron" :refer [app]]
-            [electron.logger :as logger]))
+            [electron.logger :as logger]
+            [medley.core :as medley]))
 
 (defonce databases (atom nil))
 
@@ -240,13 +241,7 @@
 
 (defn distinct-by
   [f col]
-  (reduce
-   (fn [acc x]
-     (if (some #(= (f x) (f %)) acc)
-       acc
-       (vec (conj acc x))))
-   []
-   col))
+  (medley/distinct-by f (seq col)))
 
 (defn search-blocks
   ":page - the page to specificly search on"

+ 14 - 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,17 @@
   [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
+   (defn distinct-by
+     [f col]
+     (medley/distinct-by f (seq col))))
+
+#?(:cljs
+   (defn distinct-by-last-wins
+     [f col]
+     {:pre [(sequential? col)]}
+     (reverse (distinct-by f (reverse col)))))
 
 (defn get-git-owner-and-repo
   [repo-url]
@@ -1040,7 +1028,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)