specter

Latest version: 1.1.3
abdullahibra 2020-10-21T14:01:13.017600Z

Hi everyone,

abdullahibra 2020-10-21T14:01:57.018600Z

if i have a tree of nested lists with all keyword data type, what is the efficient pattern to select all sublists that start with specific keyword ?

idiomancy 2020-10-21T14:02:24.018900Z

can you give an example input output?

abdullahibra 2020-10-21T14:06:20.019700Z

(:a
  (:x
   (:x :xoo) 
   (:y (:ya :yay))
   (:z (:t :till)
        (:y (:v :hello) (:h :world)))))

abdullahibra 2020-10-21T14:06:36.020100Z

need to select all lists that starts with :y

abdullahibra 2020-10-21T14:07:28.020800Z

output

(:y (:ya :yay))
(:y (:v :hello) (:h :world))

schmee 2020-10-21T16:02:31.021100Z

@abdullahibra

(def LISTS
    (recursive-path [] p
      (if-path list?
        (stay-then-continue ALL p)
        STOP)))

  user=> (select [LISTS (selected? FIRST (pred= :y))] your-list)
  [(:y (:ya :yay)) (:y (:v :hello) (:h :world))])

👍 1
Lucy Wang 2020-10-21T16:17:33.021500Z

or use walker

(select [(walker (fn [x]
                   (and (list? x)
                        (= (first x) :y))))]
        '(:a
         (:x
          (:x :xoo)
          (:y (:ya :yay))
          (:z (:t :till)
           (:y (:v :hello) (:h :world))))))
;; => [(:y (:ya :yay)) (:y (:v :hello) (:h :world))]

👍 1
abdullahibra 2020-10-21T16:18:57.022100Z

i have used tree-seq and filter based on some conditions and it's working, but it's worth to try those approaches too