This post originated from an RSS feed registered with Ruby Buzz
by Jay Fields.
Original Post: Clojure: Flatten Keys
Feed Title: Jay Fields Thoughts
Feed URL: http://blog.jayfields.com/rss.xml
Feed Description: Thoughts on Software Development
I recently needed to take a nested map and flatten the keys. After a bit of trial and error I came up with the following code. (note: the example expects Expectations)
(ns example (:use expectations))
(defn flatten-keys* [a ks m] (if (map? m) (reduce into (map (fn [[k v]] (flatten-keys* a (conj ks k) v)) (seq m))) (assoc a ks m)))