specter

Recursive map query using specter

拟墨画扇 提交于 2019-12-21 13:04:21
问题 Is there a simple way in specter to collect all the structure satisfying a predicate ? (./pull '[com.rpl/specter "1.0.0"]) (use 'com.rpl.specter) (def data {:items [{:name "Washing machine" :subparts [{:name "Ballast" :weight 1} {:name "Hull" :weight 2}]}]}) (reduce + (select [(walker :weight) :weight] data)) ;=> 3 (select [(walker :name) :name] data) ;=> ["Washing machine"] How can we get all the value for :name, including ["Ballast" "Hull"] ? 回答1: Here's one way, using recursive-path and

Recursive map query using specter

送分小仙女□ 提交于 2019-12-21 13:04:08
问题 Is there a simple way in specter to collect all the structure satisfying a predicate ? (./pull '[com.rpl/specter "1.0.0"]) (use 'com.rpl.specter) (def data {:items [{:name "Washing machine" :subparts [{:name "Ballast" :weight 1} {:name "Hull" :weight 2}]}]}) (reduce + (select [(walker :weight) :weight] data)) ;=> 3 (select [(walker :name) :name] data) ;=> ["Washing machine"] How can we get all the value for :name, including ["Ballast" "Hull"] ? 回答1: Here's one way, using recursive-path and

Removing nested values with Specter in Clojure

牧云@^-^@ 提交于 2019-12-12 17:14:26
问题 Suppose that I have a Clojure map like this: (def mymap {:a [1 2 3] :b {:c [] :d [1 2 3]}}) I would like a function remove-empties that produces a new map in which entries from (:b mymap) that have an empty sequence as a value are removed. So (remove-empties mymap) would give the value: {:a [1 2 3] :b {:d [1 2 3]}} Is there a way to write a function to do this using Specter? 回答1: (update my-map :b (fn [b] (apply dissoc b (map key (filter (comp empty? val) b))))) 回答2: Here's how to do it with

Recursive map query using specter

谁都会走 提交于 2019-12-04 05:27:54
Is there a simple way in specter to collect all the structure satisfying a predicate ? (./pull '[com.rpl/specter "1.0.0"]) (use 'com.rpl.specter) (def data {:items [{:name "Washing machine" :subparts [{:name "Ballast" :weight 1} {:name "Hull" :weight 2}]}]}) (reduce + (select [(walker :weight) :weight] data)) ;=> 3 (select [(walker :name) :name] data) ;=> ["Washing machine"] How can we get all the value for :name, including ["Ballast" "Hull"] ? Here's one way, using recursive-path and stay-then-continue to do the real work. (If you omit the final :name from the path argument to select , you'll