Skip to content

Instantly share code, notes, and snippets.

@adomokos adomokos/fizzbuzz.clj
Last active Jan 1, 2016

Embed
What would you like to do?
Solving the "FizzBuzz" kata (http://codingdojo.org/cgi-bin/wiki.pl?KataFizzBuzz) in Clojure
(ns fizzbuzz.core
(:gen-class))
(defn- format-output [output current]
(cond
(= 0 (mod current 15)) (str output "FizzBuzz\n")
(= 0 (mod current 3)) (str output "Fizz\n")
(= 0 (mod current 5)) (str output "Buzz\n")
:else (str output current "\n")))
(defn fizz-buzz-printer
([output current limit]
(let [product (format-output output current)]
(if (< current limit)
(fizz-buzz-printer product (+ 1 current) limit)
product)))
([start limit]
(fizz-buzz-printer "" start limit))
([limit]
(fizz-buzz-printer 1 limit)))
(defn -main
[& args]
;; work around dangerous default behaviour in Clojure
(alter-var-root #'*read-eval* (constantly false))
(print (fizz-buzz-printer 100)))
(ns fizzbuzz.core-test
(:require [clojure.test :refer :all]
[fizzbuzz.core :refer :all]))
(deftest fizz-buzz-printer-test
(testing "with 1"
(is (= "1\n" (fizz-buzz-printer 1))))
(testing "with 2"
(is (= "1\n2\n" (fizz-buzz-printer 2))))
(testing "from 1-3"
(is (= "1\n2\nFizz\n" (fizz-buzz-printer 3))))
(testing "from 1-4"
(is (= "1\n2\nFizz\n4\n" (fizz-buzz-printer 4))))
(testing "from 3-4"
(is (= "Fizz\n4\n" (fizz-buzz-printer 3 4))))
(testing "from 4-5"
(is (= "4\nBuzz\n" (fizz-buzz-printer 4 5))))
(testing "from 4-6"
(is (= "4\nBuzz\nFizz\n" (fizz-buzz-printer 4 6))))
(testing "from 9-11"
(is (= "Fizz\nBuzz\n11\n" (fizz-buzz-printer 9 11))))
(testing "from 14-16"
(is (= "14\nFizzBuzz\n16\n" (fizz-buzz-printer 14 16)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.