Skip to content

Instantly share code, notes, and snippets.

@Engelberg
Created May 18, 2016 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Engelberg/d93ea307fa5c8f1337a4ac7566df7f9d to your computer and use it in GitHub Desktop.
Save Engelberg/d93ea307fa5c8f1337a4ac7566df7f9d to your computer and use it in GitHub Desktop.
Bowling kata in 11 lines of clojure
(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))))))
@Engelberg
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment