Add Clojure translation

This commit is contained in:
Paulus Esterhazy 2022-09-25 20:41:30 +02:00
parent bdcea60169
commit c81d02acf5
7 changed files with 138 additions and 0 deletions

2
clojure/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.cpcache
actual.txt

34
clojure/README.md Normal file
View File

@ -0,0 +1,34 @@
# Gilded Rose
This is the Gilded Rose kata in Clojure.
## Getting started
You'll need the [clojure command-line tool](https://clojure.org/guides/install_clojure).
## Running app
Produces output suitable for texttests:
```sh
clojure -M:main
```
You can specify the number of days as args:
```sh
clojure -M:main 31
```
## Running tests
To run all tests
```sh
clojure -M:test
```
To run all tests in watch mode
```sh
clojure -M:test --watch
```

10
clojure/deps.edn Normal file
View File

@ -0,0 +1,10 @@
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}}
:aliases
{:main
{:main-opts ["-m" "gilded.main"]}
:test
{:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.70.1086"}
org.slf4j/slf4j-nop {:mvn/version "2.0.1"}}
:main-opts ["-m" "kaocha.runner"]}}}

View File

@ -0,0 +1,46 @@
(ns gilded.core)
(defn make-store [items]
(assert (vector? items))
(->> items
(map (fn [item] (atom item)))
vec))
(defn item-seq [store]
(->> store
(map deref)))
;; ---
(defn update-quality! [store]
(doseq [item store]
(if (and (not (= (:name @item)
"Aged Brie"))
(not (= (:name @item)
"Backstage passes to a TAFKAL80ETC concert")))
(when (> (:quality @item) 0)
(when (not (= (:name @item) "Sulfuras, Hand of Ragnaros"))
(swap! item update :quality #(- % 1))))
(when (< (:quality @item) 50)
(swap! item update :quality #(+ % 1))
(when (= (:name @item) "Backstage passes to a TAFKAL80ETC concert")
(when (< (:sell-in @item) 11)
(when (< (:quality @item) 50)
(swap! item update :quality #(+ % 1))))
(when (< (:sell-in @item) 6)
(when (< (:quality @item) 50)
(swap! item update :quality #(+ % 1)))))))
(when (not (= (:name @item) "Sulfuras, Hand of Ragnaros"))
(swap! item update :sell-in #(- % 1)))
(when (< (:sell-in @item) 0)
(if (not (= (:name @item) "Aged Brie"))
(if (not (= (:name @item) "Backstage passes to a TAFKAL80ETC concert"))
(when (> (:quality @item) 0)
(when (not (= (:name @item) "Sulfuras, Hand of Ragnaros"))
(swap! item update :quality #(- % 1))))
(swap! item update :quality #(- % %)))
(when (< (:quality @item) 50)
(swap! item update :quality #(+ % 1)))))))

View File

@ -0,0 +1,32 @@
(ns gilded.main
(:require [gilded.core :as x]))
(defn item->str [item]
(str (:name item) ", " (:sell-in item) ", " (:quality item)))
(defn print-store [store]
(println "name, sellIn, quality")
(doseq [item (x/item-seq store)]
(println (item->str item)))
(println))
(def fixture
[{: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 "Sulfuras, Hand of Ragnaros", :quality 80, :sell-in -1}
{:name "Backstage passes to a TAFKAL80ETC concert", :quality 20, :sell-in 15}
{:name "Backstage passes to a TAFKAL80ETC concert", :quality 49, :sell-in 10}
{:name "Backstage passes to a TAFKAL80ETC concert", :quality 49, :sell-in 5}
{:name "Conjured Mana Cake", :quality 6, :sell-in 3}])
(defn -main [& args]
(let [n-days (if (nil? (first args))
2
(Long/parseLong (first args)))
store (x/make-store fixture)]
(dotimes [day n-days]
(println "-------- day" day "--------")
(print-store store)
(x/update-quality! store))))

View File

@ -0,0 +1,12 @@
(ns gilded.core-test
(:require [clojure.test :refer [deftest is]]
[gilded.core :as x]))
(def fixture
[{:name "foo", :quality 20, :sell-in 10}])
(deftest simple-test
(let [store (x/make-store fixture)
_ (x/update-quality! store)
item (first (x/item-seq store))]
(is (= "fixme" (:name item)))))

2
clojure/tests.edn Normal file
View File

@ -0,0 +1,2 @@
#kaocha/v1
{}