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)
* 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

View file

@ -449,6 +449,24 @@
(prepend-one [structure elem]
(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)
(append-all [structure elements]
@ -487,6 +505,14 @@
structure
(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]
(let [newf (afn (first l))
@ -502,29 +528,7 @@
(if (nil? bl) '() bl)
(concat bl [lastl]))))
#?(
:clj
(defn vec-count [^clojure.lang.IPersistentVector v]
(.length v))
:cljs
(defn vec-count [v]
(count v)))
#?(
:clj
(defn transient-vec-count [^clojure.lang.ITransientVector v]
(.count v))
:cljs
(defn transient-vec-count [v]
(count v)))
(extend-protocol UpdateExtremes
#?(:clj clojure.lang.IPersistentVector :cljs cljs.core/PersistentVector)
(update-first [v afn]
(defn- update-first-vector [v afn]
(let [val (nth v 0)
newv (afn val)]
(if (identical? i/NONE newv)
@ -532,7 +536,7 @@
(assoc v 0 newv)
)))
(update-last [v afn]
(defn- update-last-vector [v afn]
;; type-hinting vec-count to ^int caused weird errors with case
(let [c (int (vec-count v))]
(case c
@ -552,6 +556,33 @@
(pop v)
(assoc v i newe))))))
#?(
:clj
(defn transient-vec-count [^clojure.lang.ITransientVector v]
(.count v))
:cljs
(defn transient-vec-count [v]
(count v)))
(extend-protocol UpdateExtremes
#?(:clj clojure.lang.IPersistentVector :cljs cljs.core/PersistentVector)
(update-first [v afn]
(update-first-vector v afn))
(update-last [v afn]
(update-last-vector v afn))
#?(:cljs cljs.core/Subvec)
#?(:cljs
(update-first [v afn]
(update-first-vector v afn)))
#?(:cljs
(update-last [v afn]
(update-last-vector v afn)))
#?(:clj String :cljs string)
(update-first [s afn]
(let [rests (subs s 1 (count s))