Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Example in Clojure

  • Loading branch information...
commit 26e9a7c6614e8c9065dce26f469603c57547be62 1 parent 836a42a
Johan van Zijl johanvanzijl authored
10 clojure/johanvanzijl/holling/.gitignore
... ... @@ -0,0 +1,10 @@
  1 +/target
  2 +/lib
  3 +/classes
  4 +/checkouts
  5 +pom.xml
  6 +*.jar
  7 +*.class
  8 +.lein-deps-sum
  9 +.lein-failures
  10 +.lein-plugins
28 clojure/johanvanzijl/holling/README.md
Source Rendered
... ... @@ -0,0 +1,28 @@
  1 +# HollingBerries Solution in Clojure
  2 +
  3 +A couple of comments:
  4 +* I am very new to clojure so any comments on doing things better would be welcomed.
  5 +* I re-used(read copied...) quite a few things from mschristiansen's implementation.
  6 +* The approach I used here is similar to the my example in ABAP where I placed the Supplier and Product rules in records. I believe this will give a bit more flexibility should the business rules(e.g. vary markups, expiry dates, etc by either product or supplier).
  7 +
  8 +## Usage
  9 +
  10 +You'll need [Leiningen](https://github.com/technomancy/leiningen), a
  11 +build tool for Clojure.
  12 +
  13 +Leiningen will fetch project dependencies described in the project.clj
  14 +file.
  15 +> lein deps
  16 +
  17 +Allows you to run the program, which will generate the pricefile.txt.
  18 +> lein run
  19 +
  20 +Or start a repl (is it called a prompt in Python?)
  21 +> lein repl
  22 +
  23 +To run the test:
  24 +> lein test
  25 +
  26 +## License
  27 +
  28 +Distributed under the Eclipse Public License, the same as Clojure.
11 clojure/johanvanzijl/holling/project.clj
... ... @@ -0,0 +1,11 @@
  1 +(defproject holling "0.1.0-SNAPSHOT"
  2 + :description "An Example Holling Berries Implementation"
  3 + :url "http://example.com/FIXME"
  4 + :license {:name "Eclipse Public License"
  5 + :url "http://www.eclipse.org/legal/epl-v10.html"}
  6 + :dependencies [[org.clojure/clojure "1.4.0"]
  7 + [org.clojure/math.numeric-tower "0.0.1"]
  8 + [org.clojure/data.csv "0.1.2"]
  9 + [clj-time "0.4.2"]]
  10 + :main holling.core
  11 + )
79 clojure/johanvanzijl/holling/src/holling/core.clj
... ... @@ -0,0 +1,79 @@
  1 +(ns holling.core
  2 + (:use [clojure.java.io :only [reader]]
  3 + [clojure.data.csv :only [read-csv]]
  4 + [clj-time.core :only [plus days]]
  5 + [clojure.math.numeric-tower :only [ceil]]
  6 + [clj-time.format :only [formatter parse unparse]]
  7 + ))
  8 +
  9 +(defrecord Suppinfo [ suppcode sellby markup-perc markup-curr roundup ])
  10 +(def suppliers [ (Suppinfo. "32" -3 0 -2 false) ;Susan Windler (Supplier ID 32)
  11 + (Suppinfo. "101" -3 0 -2 false) ;Togetherness Tshabalala (Supplier ID 101)
  12 + (Suppinfo. "219" 0 0.1 0 true ) ;Promise Mashangu (Supplier ID 219)
  13 + (Suppinfo. "204" 0 0.1 0 true ) ;Karel Visser (Supplier ID 204)
  14 + ])
  15 +
  16 +(defrecord Prodtype [codefrom codeto sellby markup])
  17 +(def prodtypes [ (Prodtype. 1000 1099 7 0.5) ;Other Fruit 1
  18 + (Prodtype. 1100 1199 14 0.4) ;Apples
  19 + (Prodtype. 1200 1299 5 0.35) ;Bananas
  20 + (Prodtype. 1300 1399 7 0.55) ;Berries
  21 + (Prodtype. 1400 1999 7 0.5) ;Other Fruit 2
  22 + ])
  23 +
  24 +(defrecord ProductRec [suppcode prodcode desc deldate cost qty] )
  25 +
  26 +;;; Define file locations and parse the file
  27 +(def out-file "pricefile.txt")
  28 +
  29 +(defn parse-product [item]
  30 + "Parses each line of the input file to a ProductRec"
  31 + (ProductRec. (get item 0) ;supplier code
  32 + (Integer/parseInt (get item 1)) ;parse the prodcode to int
  33 + (get item 2) ;description
  34 + (parse (formatter "yyyy/MM/dd") (get item 3)) ;parse delivery date
  35 + (/ (Integer/parseInt (get item 4)) 100) ;parse cost
  36 + (Integer/parseInt (get item 5)))) ;parse qty
  37 +
  38 +(def products
  39 + "List of ProductRec. Reads input file"
  40 + (for [ item (drop 1 ;Leave headers out.
  41 + (with-open [file (reader "produce.csv")]
  42 + (doall (read-csv file)))) ]
  43 + (parse-product item)))
  44 +
  45 +(defn between? [n min max]
  46 + (and (>= n min) (<= n max)))
  47 +
  48 +(defn get-prodtype [prodcode prodtypes]
  49 + (first (for [prodtype prodtypes :when (let [{codefrom :codefrom codeto :codeto} prodtype]
  50 + (between? prodcode codefrom codeto))] prodtype)))
  51 +
  52 +(defn get-suppinfo [suppcode suppliers]
  53 + (first (filter (comp(partial = suppcode) :suppcode) suppliers)))
  54 +
  55 +(defn calc-product [product]
  56 + (let [suppinfo (get-suppinfo (:suppcode product) suppliers)
  57 + prodtype (get-prodtype (:prodcode product) prodtypes) ]
  58 + ( -> product
  59 + (assoc :price (if (nil? suppinfo)
  60 + (* (:cost product) (+ (:markup prodtype) 1))
  61 + (let [rawprice (+ (* (:cost product) (+ (:markup prodtype) (:markup-perc suppinfo) 1))
  62 + (:markup-curr suppinfo))]
  63 + (if (> rawprice 0)
  64 + (if (:roundup suppinfo) (ceil rawprice) rawprice)
  65 + 0 ))))
  66 + (assoc :expdate (plus (:deldate product) (days (:sellby prodtype))
  67 + (when suppinfo (days (:sellby suppinfo))))))))
  68 + ;(calc-product (nth products 2))
  69 +(defn label-product [product]
  70 + "Generate the label for a single product"
  71 + (apply str ( repeat (:qty product) (str
  72 + "R" (format "%8.2f" (double (:price product)))
  73 + (unparse (formatter "yyyy/MM/dd") (:expdate product))
  74 + (subs (:desc product) 0 31)
  75 + "\n"))))
  76 +
  77 +(defn -main []
  78 + (spit "pricefile2.txt" (doall
  79 + (reduce str (for [product products] (label-product (calc-product product)))))))
10 clojure/johanvanzijl/holling/test/holling/core_test.clj
... ... @@ -0,0 +1,10 @@
  1 +(ns holling.core-test
  2 + (:use clojure.test
  3 + holling.core))
  4 +
  5 +(def test-file "pricefile.txt")
  6 +(def new-file "pricefile2.txt")
  7 +
  8 +(deftest test-diff-file
  9 + (testing "Is the new-file equal to the test-file"
  10 + (is (= (slurp new-file) (slurp test-file)))))

0 comments on commit 26e9a7c

Please sign in to comment.
Something went wrong with that request. Please try again.