BEFORE-ELEM, AFTER-ELEM, FIRST, LAST, BEGINNING, and END on subvecs now produce vector type in cljs

This commit is contained in:
Nathan Marz 2020-09-17 15:47:26 -04:00
parent efaf35558a
commit a379893598
2 changed files with 63 additions and 31 deletions

View file

@ -2,6 +2,7 @@
* Add arglist metadata to navs (thanks @phronmophobic) * Add arglist metadata to navs (thanks @phronmophobic)
* Improve before-index performance by 150x on lists and 5x on vectors (thanks @jeff303) * Improve before-index performance by 150x on lists and 5x on vectors (thanks @jeff303)
* Bug fix: BEFORE-ELEM, AFTER-ELEM, FIRST, LAST, BEGINNING, and END on subvecs now produce vector type in cljs
## 1.1.3 - 2019-10-13 ## 1.1.3 - 2019-10-13

View file

@ -449,6 +449,24 @@
(prepend-one [structure elem] (prepend-one [structure elem]
(into [elem] structure)) (into [elem] structure))
#?(:cljs cljs.core/Subvec)
#?(:cljs
(append-all [structure elements]
(reduce conj structure elements)))
#?(:cljs
(prepend-all [structure elements]
(let [ret (transient [])]
(as-> ret <>
(reduce conj! <> elements)
(reduce conj! <> structure)
(persistent! <>)))))
#?(:cljs
(append-one [structure elem]
(conj structure elem)))
#?(:cljs
(prepend-one [structure elem]
(into [elem] structure)))
#?(:clj Object :cljs default) #?(:clj Object :cljs default)
(append-all [structure elements] (append-all [structure elements]
@ -487,6 +505,14 @@
structure structure
(updater structure next-fn)))) (updater structure next-fn))))
#?(
:clj
(defn vec-count [^clojure.lang.IPersistentVector v]
(.length v))
:cljs
(defn vec-count [v]
(count v)))
(defn- update-first-list [l afn] (defn- update-first-list [l afn]
(let [newf (afn (first l)) (let [newf (afn (first l))
@ -502,14 +528,33 @@
(if (nil? bl) '() bl) (if (nil? bl) '() bl)
(concat bl [lastl])))) (concat bl [lastl]))))
#?( (defn- update-first-vector [v afn]
:clj (let [val (nth v 0)
(defn vec-count [^clojure.lang.IPersistentVector v] newv (afn val)]
(.length v)) (if (identical? i/NONE newv)
(subvec v 1)
(assoc v 0 newv)
)))
:cljs (defn- update-last-vector [v afn]
(defn vec-count [v] ;; type-hinting vec-count to ^int caused weird errors with case
(count v))) (let [c (int (vec-count v))]
(case c
1 (let [[e] v
newe (afn e)]
(if (identical? i/NONE newe)
[]
[newe]))
2 (let [[e1 e2] v
newe (afn e2)]
(if (identical? i/NONE newe)
[e1]
[e1 newe]))
(let [i (dec c)
newe (afn (nth v i))]
(if (identical? i/NONE newe)
(pop v)
(assoc v i newe))))))
#?( #?(
@ -525,32 +570,18 @@
(extend-protocol UpdateExtremes (extend-protocol UpdateExtremes
#?(:clj clojure.lang.IPersistentVector :cljs cljs.core/PersistentVector) #?(:clj clojure.lang.IPersistentVector :cljs cljs.core/PersistentVector)
(update-first [v afn] (update-first [v afn]
(let [val (nth v 0) (update-first-vector v afn))
newv (afn val)]
(if (identical? i/NONE newv)
(subvec v 1)
(assoc v 0 newv)
)))
(update-last [v afn] (update-last [v afn]
;; type-hinting vec-count to ^int caused weird errors with case (update-last-vector v afn))
(let [c (int (vec-count v))]
(case c #?(:cljs cljs.core/Subvec)
1 (let [[e] v #?(:cljs
newe (afn e)] (update-first [v afn]
(if (identical? i/NONE newe) (update-first-vector v afn)))
[] #?(:cljs
[newe])) (update-last [v afn]
2 (let [[e1 e2] v (update-last-vector v afn)))
newe (afn e2)]
(if (identical? i/NONE newe)
[e1]
[e1 newe]))
(let [i (dec c)
newe (afn (nth v i))]
(if (identical? i/NONE newe)
(pop v)
(assoc v i newe))))))
#?(:clj String :cljs string) #?(:clj String :cljs string)
(update-first [s afn] (update-first [s afn]