Created
January 19, 2012 14:30
-
-
Save martinsson/1640288 to your computer and use it in GitHub Desktop.
clojure solution to checkout-kata
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 lean-challenge.core | |
(:use [midje.sweet])) | |
; ==== application core | |
(defn cost [fruit] | |
({"b" 150 "a" 100 "p" 100 "m" 100 "c" 75} fruit)) | |
(defn csv-to-col [s] | |
(seq (.split s ","))) | |
(defn discount [fruits] | |
(+ | |
(* 20 (quot (count (filter #{"c"} fruits)) 2)) | |
(* 50 (quot (count (filter #{"m"} fruits)) 2)) | |
(* 100 (quot (count (filter #{"p"} fruits)) 3)) | |
(* 100 (quot (count (filter #{"p" "a" "m"} fruits)) 4)) | |
(* 200 (quot (count fruits) 5)) | |
(* (cost "b") (quot (count (filter #{"b"} fruits)) 2)))) | |
(defn basket-price [fruits] | |
(- (reduce + (map cost fruits)) | |
(discount fruits))) | |
; === facts about the application | |
(fact | |
(cost "b" ) => 150 | |
(cost "a" ) => 100 | |
(cost "c" ) => 75) | |
(fact "adds up the cost of all fruits in the basket" | |
(basket-price '("a" "c" "b")) => 325) | |
(fact "discount for every two lots of cherries" | |
(basket-price '("c" "c")) => (- 150 20) | |
(basket-price '("c" "c" "c" "c")) => (- 300 40)) | |
(fact "buy two bananas get one for free" | |
(basket-price '("b" "b")) => (cost "b")) | |
(fact "3 pommes are 200" | |
(basket-price '("p" "p" "p")) => 200 | |
(basket-price '("m" "m")) => 150) | |
(fact "global discounts" | |
(basket-price '("a", "a" "b", "c" "b" "b")) => 375 | |
(basket-price '("a", "a" "m", "p" )) => 300) | |
(fact | |
(csv-to-col "a,b,c") => '("a" "b" "c")) | |
; ===== To run in repl just (use 'lean-challange/core) | |
; when you're ready for the next client : enter an empty line | |
(loop [item (read-line) items []] | |
(println (basket-price (concat (csv-to-col item) items))) | |
(if (empty? item) | |
item | |
(recur (read-line) (concat (csv-to-col item) items)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment