Sample Solutions

Sample Solutions: FizzBuzz in Clojure

For the kata associated with this solution, see FizzBuzz by CyberDojo:

(ns clojure-katas.fizzbuzz)

(defn fizzBuzz [n]

(let [divisible-by? #(= 0 (rem %2 %1))

divisible-by-three? (divisible-by? 3 n)

divisible-by-five? (divisible-by? 5 n)

divisible-by-three-and-five? (divisible-by? 15 n)]

(cond

divisible-by-three-and-five? “fizzbuzz”

divisible-by-three? “fizz”

divisible-by-five? “buzz”

:else (str n))))

(ns clojure-katas.fizzbuzz-test

(:require [clojure.test :refer :all]

[clojure-katas.fizzbuzz :refer :all]))

(deftest should-fizzbuzz-number

(are [number expected] (= expected (fizzBuzz number))

1 “1”

2 “2”

3 “fizz”

6 “fizz”

9 “fizz”

5 “buzz”

10 “buzz”

20 “buzz”

15 ...

Get Agile Technical Practices Distilled now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.