From 3a130521452fb96ccb44e9f432bbe8f53b4ab606 Mon Sep 17 00:00:00 2001 From: Nathan Marz Date: Sat, 10 Oct 2015 11:52:50 -0400 Subject: [PATCH] add nil->val, NIL->SET, NIL->LIST, NIL->VECTOR, and subset selectors --- src/com/rpl/specter.cljx | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/com/rpl/specter.cljx b/src/com/rpl/specter.cljx index bcd76ae..79f794f 100644 --- a/src/com/rpl/specter.cljx +++ b/src/com/rpl/specter.cljx @@ -21,7 +21,8 @@ paramscollector paramspath]] ) - (:require [com.rpl.specter.impl :as i]) + (:require [com.rpl.specter.impl :as i] + [clojure.set :as set]) ) (defn comp-paths [& paths] @@ -164,11 +165,26 @@ (i/srange-transform structure start end next-fn) )) - (def BEGINNING (srange 0 0)) (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 walker [afn] @@ -303,6 +319,20 @@ (transform* [this 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] (pathed-collector [late path] (collect-val [this structure]