Skip to content

Instantly share code, notes, and snippets.

@sogaiu
Last active January 31, 2026 15:29
Show Gist options
  • Select an option

  • Save sogaiu/2f36e4842ca059f09e2217edc6161beb to your computer and use it in GitHub Desktop.

Select an option

Save sogaiu/2f36e4842ca059f09e2217edc6161beb to your computer and use it in GitHub Desktop.
(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