Created
July 28, 2020 07:19
-
-
Save shegeley/f5a2057258989bf943cd559d4e03f411 to your computer and use it in GitHub Desktop.
Clojure filter that works with deep nested structures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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