specter 2020-10-21

Hi everyone,

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 ?

can you give an example input output?

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

need to select all lists that starts with :y

output

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

@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

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

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