Skip to content

Instantly share code, notes, and snippets.

@bmabey
Created December 31, 2009 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmabey/266552 to your computer and use it in GitHub Desktop.
Save bmabey/266552 to your computer and use it in GitHub Desktop.
(ns keys-in-test
(:use [clojure.test]))
(defn keys-in
"returns all possible keys in a nested map"
[m & ks]
(if (map? m)
(reduce into ks
(for [[k next-m] m]
(let [next-k (vec (conj (last ks) k))]
(keys-in next-m next-k))))
ks))
(deftest keys-in-test
(let [nested {"a" {"b" {"c1" 1 "c2" 2}}}]
(is (= (set [["a"] ["a" "b"] ["a" "b" "c1"] ["a" "b" "c2"]]) (set (keys-in nested))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment