Last active
January 31, 2026 15:29
-
-
Save sogaiu/2f36e4842ca059f09e2217edc6161beb to your computer and use it in GitHub Desktop.
This file contains hidden or 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 visit | |
| `` | |
| Traverse directory tree starting at `path`, applying | |
| argument `a-fn` to each encountered file or directory path. | |
| `` | |
| [path a-fn] | |
| (assertf (is-dir? path) | |
| "expected dir but got: %n" (os/stat path :mode)) | |
| (def seen? @{}) | |
| # | |
| (a-fn path) | |
| (def paths (map |(path-join path $) | |
| (os/dir path))) | |
| (while (def p (array/pop paths)) | |
| (def [ok? value] (protect (os/realpath p))) | |
| (when (and ok? (not (get seen? value))) | |
| (put seen? value true) | |
| (cond | |
| (is-file? p) | |
| (a-fn p) | |
| # | |
| (is-dir? p) | |
| (do | |
| (a-fn p) | |
| (array/push paths ;(map |(path-join p $) | |
| (os/dir p)))))))) | |
| (comment | |
| (visit (path-join (os/getenv "HOME") ".config") | |
| |(eprint $)) | |
| ) | |
| # XXX: make more general version that accepts array of paths? | |
| (defn make-visitor | |
| [path a-fn] | |
| (assertf (is-dir? path) | |
| "expected dir but got: %n" (os/stat path :mode)) | |
| (coro | |
| (def seen? @{}) | |
| # | |
| (yield (a-fn path)) | |
| (def paths (map |(path-join path $) | |
| (os/dir path))) | |
| (while (def p (array/pop paths)) | |
| (def [ok? value] (protect (os/realpath p))) | |
| (when (and ok? (not (get seen? value))) | |
| (put seen? value true) | |
| (cond | |
| (is-file? p) | |
| (yield (a-fn p)) | |
| # | |
| (is-dir? p) | |
| (do | |
| (yield (a-fn p)) | |
| (array/push paths ;(map |(path-join p $) | |
| (os/dir p))))))))) | |
| (comment | |
| (def v | |
| (make-visitor (path-join (os/getenv "HOME") ".config") | |
| identity)) | |
| (each p v (pp p)) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment