Created
May 18, 2016 12:33
-
-
Save Engelberg/d93ea307fa5c8f1337a4ac7566df7f9d to your computer and use it in GitHub Desktop.
Bowling kata in 11 lines of clojure
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
(defn- sum [s] (reduce + s)) | |
(defn score | |
([gs] (let [s (clojure.string/replace gs #"\d/" #(str (nth % 0) (char (- 106 (int (nth % 0)))))), | |
g (map #(case % \X 10 \- 0 (- (int %) 48)) s)] | |
(score g 1))) | |
([game frame] | |
(cond | |
(= frame 10) (sum game) | |
(= (first game) 10) (+ (sum (take 3 game)) (score (drop 1 game) (inc frame))), | |
(= (sum (take 2 game)) 10) (+ (sum (take 3 game)) (score (drop 2 game) (inc frame))), | |
:else (+ (sum (take 2 game)) (score (drop 2 game) (inc frame)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 3 deals with the spares
Line 4 deals with strikes and misses
Together, those lines produce a list of all the numbers of pins knocked down during the game, which is then processed by the 2-arity version of the function.