Skip to content

Instantly share code, notes, and snippets.

@djui
Created June 15, 2013 22:15
Show Gist options
  • Select an option

  • Save djui/5789778 to your computer and use it in GitHub Desktop.

Select an option

Save djui/5789778 to your computer and use it in GitHub Desktop.
(ns io.djui.misc)
;; Use case example for tree-seq
(defn- map-traverse
"Traverse a map with vectors in values as branches and return a list of key
x's values. Example:
{:a nil :x {:foo 1} :b [{:c nil :x 42} \"test\"]} => [{:foo 1} 42]"
[map-tree x]
(let [map-vals #(when (map? %) (vals %))
branch? (comp (partial some sequential?) map-vals)
children (comp flatten (partial filter sequential?) vals)
nodes (tree-seq branch? children map-tree)]
(keep x nodes)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment