Skip to content

Commit

Permalink
did a second version
Browse files Browse the repository at this point in the history
  • Loading branch information
mschristiansen committed May 16, 2012
1 parent c414310 commit 1c5aa3a
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 8 deletions.
1 change: 1 addition & 0 deletions clojure/mschristiansen/.gitignore
Expand Up @@ -9,3 +9,4 @@ pom.xml
.lein-failures
.lein-plugins
pricefile.txt
pricefile2.txt
6 changes: 6 additions & 0 deletions clojure/mschristiansen/README.md
Expand Up @@ -20,6 +20,12 @@ Or start a repl (is it called a prompt in Python?)
To run the test:
> lein test
UPDATE:
I wasn't quite happy with first version and have done a second, which
is now available in the namespace 'berry.second - it's slightly shorter
and data is nicely separated from the program.


## License

Distributed under the Eclipse Public License, the same as Clojure.
14 changes: 6 additions & 8 deletions clojure/mschristiansen/src/berry/core.clj
Expand Up @@ -97,13 +97,11 @@
"Concatenate the strings together and add end-of-line."
[item]
(let [tags (lookup-number item 5)]
(reduce str
(repeat tags
(str (calculate-price item)
(calculate-sell-by-date item)
(include-description item)
"\n")))))
(repeat tags
(str (calculate-price item)
(calculate-sell-by-date item)
(include-description item)
"\n"))))

(defn -main []
(spit out-file (doall (reduce str (map create-tags items)))))

(spit out-file (doall (reduce str (mapcat create-tags items)))))
81 changes: 81 additions & 0 deletions clojure/mschristiansen/src/berry/second.clj
@@ -0,0 +1,81 @@
(ns berry.second
(:use [clojure.java.io :only [reader]]
[clojure.data.csv :only [read-csv]]
[clj-time.core :only [plus days]]
[clj-time.format :only [formatter parse unparse]]))

(def markup
{:apples 0.4
:bananas 0.35
:berries 0.55
:default 0.5})

(def durations
{:apples (days 14)
:bananas (days 5)
:default (days 7)})

(def categories
{:fruit [1000 1099]
:apples [1100 1199]
:bananas [1200 1299]
:berries [1300 1399]})

(def poor-suppliers
#{"32"})

(def date-format (formatter "yyyy/MM/dd"))

(defrecord Product [supplier code desc delivery cost amount])

(def products
(map #(apply ->Product %)
(drop 1 (with-open [file (reader "../../produce.csv")]
(doall (read-csv file))))))

(defn parse-int [key product]
(Integer/parseInt (key product)))

(defn between? [n min max]
(and (> n min) (< n max)))

(defn get-category [product]
(let [code (parse-int :code product)]
(some #(when (apply between? (cons code (val %))) (key %)) categories)))

(defn format-price [cents]
(str "R" (format "%8.2f" (/ cents 100))))

(defmulti price
(fn [product] (get-category product)))

(defn price-methods [category markup]
(defmethod price category
[product] (format-price (* (inc markup) (parse-int :cost product)))))

(doall (map price-methods (keys markup) (vals markup)))

(defmulti sell-by
(fn [product] (get-category product)))

(defn sell-by-methods [category duration]
(defmethod sell-by category
[{delivery :delivery supplier :supplier}]
(unparse date-format
(plus (parse date-format delivery)
duration
(when (poor-suppliers supplier) (days -3))))))

(doall (map sell-by-methods (keys durations) (vals durations)))

(defn description [{desc :desc}] (subs desc 0 31))

(defn tag [product]
(apply str
(repeat (parse-int :amount product)
(str (price product)
(sell-by product)
(description product) "\n"))))

(defn -main []
(spit "pricefile2.txt" (doall (apply str (map tag products)))))
10 changes: 10 additions & 0 deletions clojure/mschristiansen/test/berry/second_test.clj
@@ -0,0 +1,10 @@
(ns berry.core-test
(:use clojure.test
berry.second))

(def test-file "../../pricefile.txt")
(def out-file "pricefile2.txt")

(deftest test-diff-file
(testing "Is the out-file equal to the test-file"
(is (= (slurp out-file) (slurp test-file)))))

0 comments on commit 1c5aa3a

Please sign in to comment.