Skip to content

Instantly share code, notes, and snippets.

@jmhdez jmhdez/core.clj Secret
Last active Dec 14, 2017

Embed
What would you like to do?
Gilded Rose - From C# to Clojure
(ns gildedrose.core)
(def DEXTERY_VEST "+5 Dexterity Vest")
(def AGED_BRIE "Aged Brie")
(def ELIXIR "Elixir of the Mongoose")
(def SULFURAS "Sulfuras, Hand of Ragnaros")
(def BACKSTAGE_PASSES "Backstage passes to a TAFKAL80ETC concert")
(def CONJURED "Conjured Mana Cake")
(def items [{:name DEXTERY_VEST :quality 20 :sell-in 10}
{:name AGED_BRIE :quality 0 :sell-in 2}
{:name ELIXIR :quality 7 :sell-in 5}
{:name SULFURAS :quality 80 :sell-in 0}
{:name BACKSTAGE_PASSES :quality 20 :sell-in 15}
{:name CONJURED :quality 6 :sell-in 3}])
(defn default-quality-delta [sell-in]
(if (pos? sell-in) 1 2))
(defmulti update-item :name)
(defmethod update-item :default [{:keys [name quality sell-in]}]
{:name name
:quality (max 0 (- quality (default-quality-delta sell-in)))
:sell-in (dec sell-in)})
(defmethod update-item AGED_BRIE [{:keys [name quality sell-in]}]
{:name name
:quality (min 50 (+ quality (default-quality-delta sell-in)))
:sell-in (dec sell-in)})
(defmethod update-item SULFURAS [item]
item)
(defmethod update-item BACKSTAGE_PASSES [{:keys [name quality sell-in]}]
(let [qdelta (cond
(<= sell-in 5) 3
(<= sell-in 10) 2
:else 1)]
{:name name
:quality (min 50 (if (pos? sell-in) (+ quality qdelta) 0))
:sell-in (dec sell-in)}))
(defmethod update-item CONJURED [{:keys [name quality sell-in]}]
{:name name
:quality (max 0 (- quality (* 2 (default-quality-delta sell-in))))
:sell-in (dec sell-in)})
(defn update-items [items]
(map update-item items))
(ns gildedrose.core-test
(:require [clojure.test :refer :all]
[gildedrose.core :refer :all]
[gildedrose.core-test-approved :as approved]))
(deftest approval
(testing "Original data - excluding conjured items"
(let [updates (iterate update-items items)]
(doall (map #(is (= (butlast %1) (butlast %2))) updates approved/data)))))
(deftest conjured-items
(testing "Conjured items degrade twice as fast"
(let [conjured {:name CONJURED :quality 5 :sell-in 10}]
(is (= 3 (:quality (update-item conjured)))))
(let [conjured {:name CONJURED :quality 5 :sell-in 0}]
(is (= 1 (:quality (update-item conjured)))))
(let [conjured {:name CONJURED :quality 5 :sell-in -1}]
(is (= 1 (:quality (update-item conjured)))))))
(ns gildedrose.core-test-approved
(:require [clojure.test :as t]))
(def data [[{:name "+5 Dexterity Vest" :quality 20 :sell-in 10}
{:name "Aged Brie" :quality 0 :sell-in 2}
{:name "Elixir of the Mongoose" :quality 7 :sell-in 5}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 20 :sell-in 15}
{:name "Conjured Mana Cake" :quality 6 :sell-in 3}]
[{:name "+5 Dexterity Vest" :quality 19 :sell-in 9}
{:name "Aged Brie" :quality 1 :sell-in 1}
{:name "Elixir of the Mongoose" :quality 6 :sell-in 4}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 21 :sell-in 14}
{:name "Conjured Mana Cake" :quality 5 :sell-in 2}]
[{:name "+5 Dexterity Vest" :quality 18 :sell-in 8}
{:name "Aged Brie" :quality 2 :sell-in 0}
{:name "Elixir of the Mongoose" :quality 5 :sell-in 3}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 22 :sell-in 13}
{:name "Conjured Mana Cake" :quality 4 :sell-in 1}]
[{:name "+5 Dexterity Vest" :quality 17 :sell-in 7}
{:name "Aged Brie" :quality 4 :sell-in -1}
{:name "Elixir of the Mongoose" :quality 4 :sell-in 2}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 23 :sell-in 12}
{:name "Conjured Mana Cake" :quality 3 :sell-in 0}]
[{:name "+5 Dexterity Vest" :quality 16 :sell-in 6}
{:name "Aged Brie" :quality 6 :sell-in -2}
{:name "Elixir of the Mongoose" :quality 3 :sell-in 1}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 24 :sell-in 11}
{:name "Conjured Mana Cake" :quality 1 :sell-in -1}]
[{:name "+5 Dexterity Vest" :quality 15 :sell-in 5}
{:name "Aged Brie" :quality 8 :sell-in -3}
{:name "Elixir of the Mongoose" :quality 2 :sell-in 0}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 25 :sell-in 10}
{:name "Conjured Mana Cake" :quality 0 :sell-in -2}]
[{:name "+5 Dexterity Vest" :quality 14 :sell-in 4}
{:name "Aged Brie" :quality 10 :sell-in -4}
{:name "Elixir of the Mongoose" :quality 0 :sell-in -1}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 27 :sell-in 9}
{:name "Conjured Mana Cake" :quality 0 :sell-in -3}]
[{:name "+5 Dexterity Vest" :quality 13 :sell-in 3}
{:name "Aged Brie" :quality 12 :sell-in -5}
{:name "Elixir of the Mongoose" :quality 0 :sell-in -2}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 29 :sell-in 8}
{:name "Conjured Mana Cake" :quality 0 :sell-in -4}]
[{:name "+5 Dexterity Vest" :quality 12 :sell-in 2}
{:name "Aged Brie" :quality 14 :sell-in -6}
{:name "Elixir of the Mongoose" :quality 0 :sell-in -3}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 31 :sell-in 7}
{:name "Conjured Mana Cake" :quality 0 :sell-in -5}]
[{:name "+5 Dexterity Vest" :quality 11 :sell-in 1}
{:name "Aged Brie" :quality 16 :sell-in -7}
{:name "Elixir of the Mongoose" :quality 0 :sell-in -4}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 33 :sell-in 6}
{:name "Conjured Mana Cake" :quality 0 :sell-in -6}]
[{:name "+5 Dexterity Vest" :quality 10 :sell-in 0}
{:name "Aged Brie" :quality 18 :sell-in -8}
{:name "Elixir of the Mongoose" :quality 0 :sell-in -5}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 35 :sell-in 5}
{:name "Conjured Mana Cake" :quality 0 :sell-in -7}]
[{:name "+5 Dexterity Vest" :quality 8 :sell-in -1}
{:name "Aged Brie" :quality 20 :sell-in -9}
{:name "Elixir of the Mongoose" :quality 0 :sell-in -6}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 38 :sell-in 4}
{:name "Conjured Mana Cake" :quality 0 :sell-in -8}]
[{:name "+5 Dexterity Vest" :quality 6 :sell-in -2}
{:name "Aged Brie" :quality 22 :sell-in -10}
{:name "Elixir of the Mongoose" :quality 0 :sell-in -7}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 41 :sell-in 3}
{:name "Conjured Mana Cake" :quality 0 :sell-in -9}]
[{:name "+5 Dexterity Vest" :quality 4 :sell-in -3}
{:name "Aged Brie" :quality 24 :sell-in -11}
{:name "Elixir of the Mongoose" :quality 0 :sell-in -8}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 44 :sell-in 2}
{:name "Conjured Mana Cake" :quality 0 :sell-in -10}]
[{:name "+5 Dexterity Vest" :quality 2 :sell-in -4}
{:name "Aged Brie" :quality 26 :sell-in -12}
{:name "Elixir of the Mongoose" :quality 0 :sell-in -9}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 47 :sell-in 1}
{:name "Conjured Mana Cake" :quality 0 :sell-in -11}]
[{:name "+5 Dexterity Vest" :quality 0 :sell-in -5}
{:name "Aged Brie" :quality 28 :sell-in -13}
{:name "Elixir of the Mongoose" :quality 0 :sell-in -10}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 50 :sell-in 0}
{:name "Conjured Mana Cake" :quality 0 :sell-in -12}]
[{:name "+5 Dexterity Vest" :quality 0 :sell-in -6}
{:name "Aged Brie" :quality 30 :sell-in -14}
{:name "Elixir of the Mongoose" :quality 0 :sell-in -11}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 0 :sell-in -1}
{:name "Conjured Mana Cake" :quality 0 :sell-in -13}]
[{:name "+5 Dexterity Vest" :quality 0 :sell-in -7}
{:name "Aged Brie" :quality 32 :sell-in -15}
{:name "Elixir of the Mongoose" :quality 0 :sell-in -12}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 0 :sell-in -2}
{:name "Conjured Mana Cake" :quality 0 :sell-in -14}]
[{:name "+5 Dexterity Vest" :quality 0 :sell-in -8}
{:name "Aged Brie" :quality 34 :sell-in -16}
{:name "Elixir of the Mongoose" :quality 0 :sell-in -13}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 0 :sell-in -3}
{:name "Conjured Mana Cake" :quality 0 :sell-in -15}]
[{:name "+5 Dexterity Vest" :quality 0 :sell-in -9}
{:name "Aged Brie" :quality 36 :sell-in -17}
{:name "Elixir of the Mongoose" :quality 0 :sell-in -14}
{:name "Sulfuras, Hand of Ragnaros" :quality 80 :sell-in 0}
{:name "Backstage passes to a TAFKAL80ETC concert" :quality 0 :sell-in -4}
{:name "Conjured Mana Cake" :quality 0 :sell-in -16}]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.