Created
October 7, 2012 12:50
-
-
Save davespanton/3848306 to your computer and use it in GitHub Desktop.
Gilded Rose Clojure core
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
(ns gilded-rose.core) | |
(def dex-vest "+5 Dexterity Vest") | |
(def aged-brie "Aged Brie") | |
(def elixir-mong "Elixir of the Mongoose") | |
(def sulfuras "Sulfuras, Hand of Ragnaros") | |
(def back-passes "Backstage passes to a TAFKAL80ETC concert") | |
(def conjured "Conjured frog") | |
(def minimum-quality 0) | |
(def maximum-quality 50) | |
(defn standard-sell-in | |
[item] | |
(assoc item :sell-in (dec (:sell-in item)))) | |
(defn inc-quality | |
[inc-by item] | |
(assoc item :quality (min maximum-quality (+ (:quality item) inc-by)))) | |
(defn dec-quality | |
[dec-by item] | |
(assoc item :quality (max minimum-quality (- (:quality item) dec-by)))) | |
(defn kill-quality | |
[item] | |
(assoc item :quality 0)) | |
(defn standard-quality | |
[item] | |
(if (< (:sell-in item) 0) | |
(dec-quality 2 item) | |
(dec-quality 1 item))) | |
(defn conjured-quality | |
[item] | |
(dec-quality 2 item)) | |
(defn maturing-quality | |
[item] | |
(inc-quality 1 item)) | |
(defn concert-quality | |
[item] | |
(let [sell-in (:sell-in item)] | |
(cond | |
(< sell-in 0) | |
(assoc item :quality 0) | |
(contains? #{0 1 2 3 4} sell-in) | |
(inc-quality 3 item) | |
(contains? #{5 6 7 8 9} sell-in) | |
(inc-quality 2 item) | |
:else | |
(inc-quality 1 item)))) | |
(defn update-quality | |
[items] | |
(map #((:quality-fn %) %) (map #((:sell-in-fn %) %) items))) | |
(defn item [item-name, sell-in, quality] | |
{:name item-name, :sell-in sell-in, :quality quality}) | |
(defn with-fns | |
[item sell-in-fn quality-fn] | |
(merge item {:sell-in-fn sell-in-fn :quality-fn quality-fn})) | |
(def inventory | |
[(with-fns (item dex-vest 10 20) standard-sell-in standard-quality) | |
(with-fns (item aged-brie 2 0) standard-sell-in maturing-quality) | |
(with-fns (item elixir-mong 5 7) standard-sell-in standard-quality) | |
(with-fns (item sulfuras 0 80) identity identity) | |
(with-fns (item back-passes 15 20) standard-sell-in concert-quality) | |
(with-fns (item conjured 4 10) standard-sell-in conjured-quality)]) | |
(defn update-current-inventory | |
[] | |
(update-quality inventory)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment