Skip to content

Instantly share code, notes, and snippets.

@rcampbell
Created March 4, 2010 12:35
Show Gist options
  • Save rcampbell/321663 to your computer and use it in GitHub Desktop.
Save rcampbell/321663 to your computer and use it in GitHub Desktop.
(ns coordinates
(:import [clojure.lang IPersistentVector IPersistentMap]))
(declare flatten)
(defmulti append
"Appends a given data structure to a flat map"
#(class %3))
(defmethod append IPersistentVector [m k v]
(assoc m k (vec (distinct (concat (m k) v)))))
(defmethod append IPersistentMap [m k v]
(flatten m (str (name k) "__") v))
(defmethod append :default [m k v]
(assoc m k (if-let [vals (m k)]
(vec (distinct (conj vals v)))
(vector v))))
(defn flatten
"Reduces a nil-free coordinate c into the
taxonomy m using an optional key prefix p"
([c] (flatten {} "" c))
([m c] (flatten m "" c))
([m p c]
(reduce #(append %1
(keyword (str p (name (key %2))))
(val %2)) m
(filter #(not (nil? (val %))) c))))
(defn taxonomy [coordinates]
"Returns the available coordinate values"
(reduce flatten {} coordinates))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment