Skip to content

Instantly share code, notes, and snippets.

@shegeley
Created July 28, 2020 07:19
Show Gist options
  • Save shegeley/f5a2057258989bf943cd559d4e03f411 to your computer and use it in GitHub Desktop.
Save shegeley/f5a2057258989bf943cd559d4e03f411 to your computer and use it in GitHub Desktop.
Clojure filter that works with deep nested structures
(defn filter-nested
[pred xs]
(loop [r []
ys xs]
(let [k (first ys)
_ (print k)]
(cond
(not (sequential? k))
(cond
(= [] ys) r
(pred k) (recur
(conj r k)
(rest ys))
:else (recur r
(rest ys)))
(empty? ys) r
(sequential? k)
(recur
(conj r (filter-nested pred k))
(rest ys))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment