Skip to content

Instantly share code, notes, and snippets.

@slipset
Last active October 21, 2015 21:10
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 slipset/2c96d5b2bea6a87f57cd to your computer and use it in GitHub Desktop.
Save slipset/2c96d5b2bea6a87f57cd to your computer and use it in GitHub Desktop.
;;
;; When refactoring this kind of stuff, I tend to create functions to transform one of the items
;; This way, the threading stuff doesn't get to complicated.
;;
(defn ->sku-name-type [product]
(let [type (:type product)]
(->> product
:variations
(map val)
(map (partial merge {:type type})))))
(defn available-stock [availability tuple]
(->> tuple
:sku
str
keyword
(get availability)
(merge tuple)))
(defn low-on-stock [hardwareCatalog availability treshold]
(->> hardwareCatalog
(vals)
(mapcat ->sku-name-type)
(map (partial available-stock availability))
(filter #(> treshold (:availableQuantity %)))
(sort-by :name)))
@holyjak
Copy link

holyjak commented Oct 21, 2015

Thanks a lot, Erik! This is really much nicer. I have modified the blog post to point to your version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment