Fork me on GitHub
#specter
<
2020-10-21
>
abdullahibra14:10:57

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 ?

idiomancy14:10:24

can you give an example input output?

abdullahibra14:10:20

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

abdullahibra14:10:36

need to select all lists that starts with :y

abdullahibra14:10:28

output

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

schmee16:10:31

@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))])

👍 3
Lucy Wang16:10:33

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))]

👍 3
abdullahibra16:10:57

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