Last active
December 19, 2015 16:39
-
-
Save yuanmai/5985485 to your computer and use it in GitHub Desktop.
Bowling & Roman Katas in Clojure http://v.youku.com/v_show/id_XNTgyMjc0NjY0.html
http://v.youku.com/v_show/id_XNTgyMjY0ODky.html
This file contains hidden or 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 bowling_kata.core | |
(:use midje.sweet)) | |
(defn strike [x & xs] | |
(when (= x 10) | |
[[10 (first xs) (second xs)] xs])) | |
(defn spare [x y & xs] | |
(when (= 10 (+ x y)) | |
[[x y (first xs)] xs])) | |
(defn open-frame [x y & xs] | |
[[x y] xs]) | |
(defn frame [[_ xs]] | |
(or (apply strike xs) | |
(apply spare xs) | |
(apply open-frame xs))) | |
(defn score [xs] | |
(->> [0 xs] | |
(iterate frame) | |
(take 11) | |
(map first) | |
flatten | |
(reduce +))) | |
;.;. The sum of wisdom is that time is never lost that is devoted to | |
;.;. work. -- Emerson | |
(facts | |
(fact | |
(strike 10 1 2) => [[10 1 2] [1 2]] | |
(strike 10 3 7) => [[10 3 7] [3 7]] | |
(strike 3 7 2) => nil?) | |
(fact | |
(spare 3 7 2) => [[3 7 2] [2]] | |
(spare 4 6 10) => [[4 6 10] [10]] | |
(spare 0 0 0) => nil?) | |
(fact | |
(open-frame 1 2 3) => [[1 2] [3]] | |
(open-frame 0 0 10) => [[0 0] [10]]) | |
(fact | |
(frame [0 [10 1 2]]) => [[10 1 2] [1 2]] | |
(frame [0 [3 7 2]]) => [[3 7 2] [2]] | |
(frame [0 [1 2 3]]) => [[1 2] [3]] | |
(frame (frame [0 [10 3 7 1]])) => [[3 7 1] [1]]) | |
(fact | |
(score (repeat 12 10)) => 300 | |
(score (repeat 0)) => 0)) |
This file contains hidden or 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 roman_kata.core | |
(:use midje.sweet)) | |
(defn digits [i] | |
(map #(mod (quot i %) 10) [1000 100 10 1])) | |
(defn to-roman | |
([i] | |
(apply to-roman (digits i))) | |
([x1000 x100 x10 x1] | |
(str | |
(["" "M" "MM" "MMM"] x1000) | |
(["" "C","CC","CCC","CD","D","DC","DCC","DCCC","CM"] x100) | |
(["" "X","XX","XXX","XL","L","LX","LXX","LXXX","XC"] x10) | |
(["" "I","II","III","IV","V","VI","VII","VIII","IX"] x1)))) | |
;.;. Good code is its own best documentation. -- Steve McConnell | |
(facts | |
(fact | |
(map #(to-roman 0 0 0 %) (range 1 10)) | |
=> ["I","II","III","IV","V","VI","VII","VIII","IX"] | |
(map #(to-roman 0 0 % 0) (range 1 10)) | |
=> ["X","XX","XXX","XL","L","LX","LXX","LXXX","XC"] | |
(map #(to-roman 0 % 0 0) (range 1 10)) | |
=> ["C","CC","CCC","CD","D","DC","DCC","DCCC","CM"] | |
(map #(to-roman % 0 0 0) [1 2 3]) | |
=> ["M" "MM" "MMM"]) | |
(fact | |
(to-roman 1 1 1 1) => "MCXI") | |
(fact | |
(to-roman 1111) => "MCXI" | |
(to-roman 1000) => "M")) | |
(facts | |
(digits 1234) => [1 2 3 4] | |
(digits 2345) => [2 3 4 5]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment