add NODE-SEQ, find-first, and NEXT-WALK
This commit is contained in:
parent
96d5e94a5b
commit
9a3a19bb9b
2 changed files with 61 additions and 11 deletions
|
|
@ -1,10 +1,10 @@
|
||||||
(ns com.rpl.specter.zipper
|
(ns com.rpl.specter.zipper
|
||||||
#+cljs (:require-macros
|
#+cljs (:require-macros
|
||||||
[com.rpl.specter.macros
|
[com.rpl.specter.macros
|
||||||
:refer [defpath path]])
|
:refer [defpath path declarepath providepath]])
|
||||||
#+clj
|
#+clj
|
||||||
(:use
|
(:use
|
||||||
[com.rpl.specter.macros :only [defpath path]])
|
[com.rpl.specter.macros :only [defpath path declarepath providepath]])
|
||||||
(:require [com.rpl.specter :as s]
|
(:require [com.rpl.specter :as s]
|
||||||
[clojure.zip :as zip]))
|
[clojure.zip :as zip]))
|
||||||
|
|
||||||
|
|
@ -84,3 +84,29 @@
|
||||||
(transform* [this structure next-fn]
|
(transform* [this structure next-fn]
|
||||||
(zip/edit structure next-fn)
|
(zip/edit structure next-fn)
|
||||||
))
|
))
|
||||||
|
|
||||||
|
(defpath NODE-SEQ []
|
||||||
|
(select* [this structure next-fn]
|
||||||
|
(next-fn [(zip/node structure)])
|
||||||
|
)
|
||||||
|
(transform* [this structure next-fn]
|
||||||
|
(let [to-insert (next-fn [(zip/node structure)])
|
||||||
|
inserted (reduce zip/insert-left structure to-insert)]
|
||||||
|
(zip/remove inserted)
|
||||||
|
)))
|
||||||
|
|
||||||
|
(declarepath find-first [predfn])
|
||||||
|
|
||||||
|
(providepath find-first
|
||||||
|
(s/if-path [NODE s/pred]
|
||||||
|
s/STAY
|
||||||
|
[NEXT (s/params-reset find-first)]
|
||||||
|
))
|
||||||
|
|
||||||
|
(declarepath NEXT-WALK)
|
||||||
|
|
||||||
|
(providepath NEXT-WALK
|
||||||
|
(s/stay-then-continue
|
||||||
|
NEXT
|
||||||
|
NEXT-WALK
|
||||||
|
))
|
||||||
|
|
|
||||||
|
|
@ -69,20 +69,21 @@
|
||||||
)))
|
)))
|
||||||
)
|
)
|
||||||
|
|
||||||
(declarepath NEXT-WALKER)
|
|
||||||
|
|
||||||
(providepath NEXT-WALKER
|
|
||||||
(s/stay-then-continue
|
|
||||||
z/NEXT
|
|
||||||
NEXT-WALKER
|
|
||||||
))
|
|
||||||
|
|
||||||
|
|
||||||
(deftest next-terminate-test
|
(deftest next-terminate-test
|
||||||
(is (= [2 [3 4 [5]] 6]
|
(is (= [2 [3 4 [5]] 6]
|
||||||
(s/transform [z/VECTOR-ZIP NEXT-WALKER z/NODE number?]
|
(s/transform [z/VECTOR-ZIP z/NEXT-WALK z/NODE number?]
|
||||||
inc
|
inc
|
||||||
[1 [2 3 [4]] 5])))
|
[1 [2 3 [4]] 5])))
|
||||||
|
(is (= [1 [3 [[]] 5]]
|
||||||
|
(s/setval [z/VECTOR-ZIP
|
||||||
|
z/NEXT-WALK
|
||||||
|
(s/selected? z/NODE number? even?)
|
||||||
|
z/NODE-SEQ]
|
||||||
|
[]
|
||||||
|
[1 2 [3 [[4]] 5] 6]
|
||||||
|
)
|
||||||
|
))
|
||||||
)
|
)
|
||||||
|
|
||||||
(deftest zipper-nav-stop-test
|
(deftest zipper-nav-stop-test
|
||||||
|
|
@ -96,3 +97,26 @@
|
||||||
(s/transform [z/VECTOR-ZIP z/DOWN z/NODE] inc [])))
|
(s/transform [z/VECTOR-ZIP z/DOWN z/NODE] inc [])))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
(deftest find-first-test
|
||||||
|
(is (= [1 [3 [[4]] 5] 6]
|
||||||
|
(s/setval [z/VECTOR-ZIP
|
||||||
|
(z/find-first #(and (number? %) (even? %)))
|
||||||
|
z/NODE-SEQ
|
||||||
|
]
|
||||||
|
[]
|
||||||
|
[1 2 [3 [[4]] 5] 6])
|
||||||
|
))
|
||||||
|
)
|
||||||
|
|
||||||
|
(deftest nodeseq-expand-test
|
||||||
|
(is (= [2 [2] [[4 4 4]] 4 4 4 6]
|
||||||
|
(s/transform [z/VECTOR-ZIP
|
||||||
|
z/NEXT-WALK
|
||||||
|
(s/selected? z/NODE number? odd?)
|
||||||
|
(s/collect-one z/NODE)
|
||||||
|
z/NODE-SEQ]
|
||||||
|
(fn [v _]
|
||||||
|
(repeat v (inc v)))
|
||||||
|
[1 [2] [[3]] 3 6]
|
||||||
|
)))
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue