add nil->val, NIL->SET, NIL->LIST, NIL->VECTOR, and subset selectors

This commit is contained in:
Nathan Marz 2015-10-10 11:52:50 -04:00
parent 25de0eca7f
commit 3a13052145

View file

@ -21,7 +21,8 @@
paramscollector paramscollector
paramspath]] paramspath]]
) )
(:require [com.rpl.specter.impl :as i]) (:require [com.rpl.specter.impl :as i]
[clojure.set :as set])
) )
(defn comp-paths [& paths] (defn comp-paths [& paths]
@ -164,11 +165,26 @@
(i/srange-transform structure start end next-fn) (i/srange-transform structure start end next-fn)
)) ))
(def BEGINNING (srange 0 0)) (def BEGINNING (srange 0 0))
(def END (srange-dynamic count count)) (def END (srange-dynamic count count))
(defparamspath
^{:doc "Navigates to the specified subset (by taking an intersection).
In a transform, that subset in the original set is changed to the
new value of the subset."}
subset
[aset]
(select* [this structure next-fn]
(next-fn (set/intersection structure aset)))
(transform* [this structure next-fn]
(let [subset (set/intersection structure aset)
newset (next-fn subset)]
(-> structure
(set/difference subset)
(set/union newset))
)))
(defparamspath (defparamspath
walker walker
[afn] [afn]
@ -303,6 +319,20 @@
(transform* [this structure next-fn] (transform* [this structure next-fn]
(i/filter-transform afn structure next-fn))) (i/filter-transform afn structure next-fn)))
(defparamspath
^{:doc "Navigates to the provided val if the structure is nil. Otherwise it stays
navigated at the structure."}
nil->val
[v]
(select* [this structure next-fn]
(next-fn (if structure structure v)))
(transform* [this structure next-fn]
(next-fn (if structure structure v))))
(def NIL->SET (nil->val #{}))
(def NIL->LIST (nil->val '()))
(def NIL->VECTOR (nil->val []))
(defn collect [& path] (defn collect [& path]
(pathed-collector [late path] (pathed-collector [late path]
(collect-val [this structure] (collect-val [this structure]