Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive map query using specter

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"] ?

like image 569
jwinandy Avatar asked Mar 19 '17 16:03

jwinandy


People also ask

What maps are being remodeled in Specter?

Luxury Home and Art Gallery are the only maps to be remodeled instead of reworked. Alleyway in Christmas was the first map that was given snowy effects. According to Periodic Twitter, Church was also a scrapped map. According to Periodic Twitter, the Village map will be added in Specter 2.0, which is the first sneak peek of the game.

Is there a restaurant map in Specter?

This map is only available for a LIMITED TIME." There was a scrapped map in Specter called “Restaurant.” There are planned to be 14 permanent maps in Specter in total. EternalFurious, one of the developers of Specter, made most of the maps. The only map he didn’t make was Chalet, made by LowPolys.

How many permanent maps are there in Specter?

There was a scrapped map in Specter called “Restaurant.” There are planned to be 14 permanent maps in Specter in total. EternalFurious, one of the developers of Specter, made most of the maps. The only map he didn’t make was Chalet, made by LowPolys. Private Home is the first map to have rainy weather in it. The Alleyway map was made in 4 hours.

Is it possible to implement recursive queries in SQL Server?

By: Edwin Sarmiento | Updated: 2021-10-06 | Comments (18) | Related: More > Common Table Expressions We need a better way to implement recursive queries in SQL Server and in this article we look at how this can be done using a Common Table Expression or CTE.


2 Answers

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 get the full “item / part maps” rather than just the :name strings.)

(def data
  {:items [{:name "Washing machine"
            :subparts [{:name "Ballast" :weight 1}
                       {:name "Hull" :weight 2}]}]})

(specter/select
  [(specter/recursive-path [] p
     [(specter/walker :name) (specter/stay-then-continue [:subparts p])])
   :name]
  data)
;= ["Washing machine" "Ballast" "Hull"]

Update: In answer to the comment below, here's a version of the above the descends into arbitrary branches of the tree, as opposed to only descending into the :subparts branch of any given node, excluding :name (which is the key whose values in the tree we want to extract and should not itself be viewed as a branching off point):

(specter/select
  [(specter/recursive-path [] p
     [(specter/walker :name)
      (specter/stay-then-continue
        [(specter/filterer #(not= :name (key %)))
         (specter/walker :name)
         p])])
   :name]
  ;; adding the key `:subparts` with the value [{:name "Foo"}]
  ;; to the "Washing machine" map to exercise the new descent strategy
  (assoc-in data [:items 0 :subparts2] [{:name "Foo"}]))

;= ["Washing machine" "Ballast" "Hull" "Foo"]
like image 166
Michał Marczyk Avatar answered Oct 24 '22 11:10

Michał Marczyk


The selected? selector can be used to collect structures for which another selector matches something within the structure

From the examples at https://github.com/nathanmarz/specter/wiki/List-of-Navigators#selected

=> (select [ALL (selected? [(must :a) even?])] [{:a 0} {:a 1} {:a 2} {:a 3}])
[{:a 0} {:a 2}]
like image 33
Arthur Ulfeldt Avatar answered Oct 24 '22 12:10

Arthur Ulfeldt