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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access