add indexed-vals

This commit is contained in:
nathanmarz 2017-08-16 07:28:05 -04:00
parent 8d5f39a861
commit c233fb7e9d
3 changed files with 16 additions and 7 deletions

View file

@ -1,5 +1,6 @@
## 1.0.4-SNAPSHOT ## 1.0.4-SNAPSHOT
* Add `indexed-vals` navigator, a variant of `INDEXED-VALS` that allows for a customized start index.
* Bug fix: Fix `INDEXED-VALS` invalidly overwriting elements in some transforms involving multiple index changes * Bug fix: Fix `INDEXED-VALS` invalidly overwriting elements in some transforms involving multiple index changes
## 1.0.3 ## 1.0.3

View file

@ -995,14 +995,14 @@
))) )))
(defnav (defnav
^{:doc "Navigate to [index elem] pairs for each element in a sequence. Changing index in transform ^{:doc "Navigate to [index elem] pairs for each element in a sequence. The sequence will be indexed
has same effect as `index-nav`. Indices seen during transform take into account any shifting starting from `start`. Changing index in transform has same effect as `index-nav`. Indices seen
from prior sequence elements changing indices."} during transform take into account any shifting from prior sequence elements changing indices."}
INDEXED-VALS indexed-vals
[] [start]
(select* [this structure next-fn] (select* [this structure next-fn]
;; could be more efficient with a primitive mutable field ;; could be more efficient with a primitive mutable field
(let [i (i/mutable-cell -1)] (let [i (i/mutable-cell (dec start))]
(doseqres NONE [e structure] (doseqres NONE [e structure]
(i/update-cell! i inc) (i/update-cell! i inc)
(next-fn [(i/get-cell i) e]) (next-fn [(i/get-cell i) e])
@ -1012,7 +1012,8 @@
(reduce (reduce
(fn [s e] (fn [s e]
(let [curri (first (i/get-cell indices)) (let [curri (first (i/get-cell indices))
[newi newe] (next-fn [curri e])] [newi* newe] (next-fn [(+ start curri) e])
newi (- newi* start)]
(i/update-cell! (i/update-cell!
indices indices
(fn [ii] (fn [ii]
@ -1029,6 +1030,11 @@
structure structure
)))) ))))
(def
^{:doc "`indexed-vals` with a starting index of 0."}
INDEXED-VALS
(indexed-vals 0))
(defrichnav (defrichnav
^{:doc "Navigates to result of running `afn` on the currently navigated value."} ^{:doc "Navigates to result of running `afn` on the currently navigated value."}
view view

View file

@ -1626,6 +1626,8 @@
(is (= [:a :b :c :d :e] (transform [s/INDEXED-VALS s/FIRST odd?] inc data))) (is (= [:a :b :c :d :e] (transform [s/INDEXED-VALS s/FIRST odd?] inc data)))
(is (= [0 2 2 4] (transform [s/INDEXED-VALS s/LAST odd?] inc [0 1 2 3]))) (is (= [0 2 2 4] (transform [s/INDEXED-VALS s/LAST odd?] inc [0 1 2 3])))
(is (= [0 1 2 3] (transform [s/INDEXED-VALS (s/collect-one s/LAST) s/FIRST] (fn [i _] i) [2 1 3 0]))) (is (= [0 1 2 3] (transform [s/INDEXED-VALS (s/collect-one s/LAST) s/FIRST] (fn [i _] i) [2 1 3 0])))
(is (= [-1 0 1 2 3] (transform [(s/indexed-vals -1) (s/collect-one s/LAST) s/FIRST] (fn [i _] i) [3 -1 0 2 1])))
(is (= [[1 :a] [2 :b] [3 :c]] (select (s/indexed-vals 1) [:a :b :c])))
)) ))
#?(:clj #?(:clj