Last active
February 19, 2016 15:47
-
-
Save hauntedhost/9e60a6b8a98a36c6e6ca to your computer and use it in GitHub Desktop.
mixed map in clojure hack (wip)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def items {:item_name_1 "Great Deal", | |
:item_options_2 "blah: 2", | |
:item_name_2 "Awesome Deal", | |
:item_options_1 "foo: 3", | |
:item_quantity_1 "1", | |
:item_price_2 "9.99", | |
:item_price_1 "9.99", | |
:itemCount "2"}) | |
(def item-keys | |
[:item_name :item_options :item_quantity :item_price]) | |
(defn one-to-n [n] | |
(map inc (take n (range)))) | |
(defn clean-key [key] | |
(keyword (clojure.string/replace (name key) #"_\d+" ""))) | |
(defn keys-for-num [item-num] | |
(map #(keyword (str (name %) "_" item-num)) item-keys)) | |
(defn num-items [items] | |
(Integer/parseInt (:itemCount items))) | |
(defn parse-items [items] | |
(into [] | |
(for [item-num (one-to-n (num-items items))] | |
(reduce-kv (fn [m k v] (assoc m (clean-key k) v)) {} | |
(select-keys items (keys-for-num item-num)))))) | |
(parse-items items) | |
;; [{:item_name "Great Deal", :item_options "foo: 3", :item_quantity "1", :item_price "9.99"} | |
;; {:item_name "Awesome Deal", :item_options "blah: 2", :item_price "9.99"}] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment