add INDEXED-VALS navigator
This commit is contained in:
parent
1b7f987eaa
commit
1b4a7d3d59
1 changed files with 40 additions and 0 deletions
|
|
@ -994,6 +994,46 @@
|
||||||
structure
|
structure
|
||||||
)))
|
)))
|
||||||
|
|
||||||
|
(defnav
|
||||||
|
^{:doc "Navigate to [index elem] pairs for each element in a sequence. Changing in index in transforms
|
||||||
|
has same effect as `index-nav`. Indices seen during transform take into account any shifting
|
||||||
|
resulting from prior sequence elements changing indices."}
|
||||||
|
INDEXED-VALS
|
||||||
|
[]
|
||||||
|
(select* [this structure next-fn]
|
||||||
|
;; could be more efficient with a primitve mutable field
|
||||||
|
(let [i (i/mutable-cell -1)]
|
||||||
|
(doseqres NONE [e structure]
|
||||||
|
(i/update-cell! i inc)
|
||||||
|
(next-fn [(i/get-cell i) e])
|
||||||
|
)))
|
||||||
|
(transform* [this structure next-fn]
|
||||||
|
(let [i (i/mutable-cell 0)
|
||||||
|
indices (i/mutable-cell (-> structure count range vec))]
|
||||||
|
(reduce
|
||||||
|
(fn [s e]
|
||||||
|
(let [curri (nth (i/get-cell indices) (i/get-cell i))
|
||||||
|
[newi newe] (next-fn [curri e])]
|
||||||
|
(if (> newi curri)
|
||||||
|
(i/update-cell! indices
|
||||||
|
(fn [ii]
|
||||||
|
(loop [j (inc curri)
|
||||||
|
s ii]
|
||||||
|
(let [news (update s j dec)]
|
||||||
|
(if (< j newi)
|
||||||
|
(recur (inc j) news)
|
||||||
|
news
|
||||||
|
))))))
|
||||||
|
(i/update-cell! i inc)
|
||||||
|
(->> s
|
||||||
|
(setval (nthpath curri) newe)
|
||||||
|
(setval (index-nav curri) newi)
|
||||||
|
i/spy
|
||||||
|
)))
|
||||||
|
structure
|
||||||
|
structure
|
||||||
|
))))
|
||||||
|
|
||||||
(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
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue