From 9a3a19bb9b945c10763e114eb14c1e8f3ff9a4ee Mon Sep 17 00:00:00 2001 From: Nathan Marz Date: Sun, 24 Apr 2016 16:49:45 -0400 Subject: [PATCH] add NODE-SEQ, find-first, and NEXT-WALK --- src/clj/com/rpl/specter/zipper.cljx | 30 +++++++++++++++++-- test/com/rpl/specter/zipper_test.cljx | 42 +++++++++++++++++++++------ 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/clj/com/rpl/specter/zipper.cljx b/src/clj/com/rpl/specter/zipper.cljx index d551cbe..5b19f9a 100644 --- a/src/clj/com/rpl/specter/zipper.cljx +++ b/src/clj/com/rpl/specter/zipper.cljx @@ -1,10 +1,10 @@ (ns com.rpl.specter.zipper #+cljs (:require-macros [com.rpl.specter.macros - :refer [defpath path]]) + :refer [defpath path declarepath providepath]]) #+clj (:use - [com.rpl.specter.macros :only [defpath path]]) + [com.rpl.specter.macros :only [defpath path declarepath providepath]]) (:require [com.rpl.specter :as s] [clojure.zip :as zip])) @@ -84,3 +84,29 @@ (transform* [this 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 + )) diff --git a/test/com/rpl/specter/zipper_test.cljx b/test/com/rpl/specter/zipper_test.cljx index e0305e8..0cd30d0 100644 --- a/test/com/rpl/specter/zipper_test.cljx +++ b/test/com/rpl/specter/zipper_test.cljx @@ -69,20 +69,21 @@ ))) ) -(declarepath NEXT-WALKER) - -(providepath NEXT-WALKER - (s/stay-then-continue - z/NEXT - NEXT-WALKER - )) - (deftest next-terminate-test (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 [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 @@ -96,3 +97,26 @@ (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] + ))) + )