Skip to content

Instantly share code, notes, and snippets.

@gtrak
Created August 11, 2011 19:35
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 gtrak/1140545 to your computer and use it in GitHub Desktop.
Save gtrak/1140545 to your computer and use it in GitHub Desktop.
;;These functions are equivalent
;; the :as :map isn't quite right, it returns a map with key to a vector of
;; values when I should just have key to value, so we adjust it,
;; TODO: there must be a better way
(defn- headers-to-map1 [headers]
(loop [out {} left headers]
(let [cur (first left)
key (first cur)
val (second cur)
;;for the recur
nleft (next left)]
(if (empty? left) out
(if (and (vector? val) (= 1 (count val)))
(recur (assoc out key (first val)) nleft)
;;else just put the value back in
(recur (assoc out key val) nleft))))))
(defn- headers-to-map [headers]
(into {} (map (fn [[k v]]
(if (and (vector? v) (second v))
[k v]
[k (first v)]))
headers)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment