repl.it
@shahinism/

FizzBuzz Clojure

Clojure

No description

fork
loading
Files
  • main.clj
main.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(ns compress-clj.core
    (:use clojure.test))

(defn is-divisible?
  [number by]
  {:pre [(not= by 0)]}
  (= (mod number by) 0))

(defn to-fizz-buzz
  [number]
  (cond
    (and (is-divisible? number 5) (is-divisible? number 3)) "FizzBuzz"
    (is-divisible? number 3) "Fizz"
    (is-divisible? number 5) "Buzz"
    :else (str number)))

(defn fizz-buzz
  [number]
  {:pre [(number? number) (> number 0)]}
  (map to-fizz-buzz (range 1 (+ number 1))))

(deftest fizz-buzz-test
  (testing "calculate the FizzBuzz of 15"
    (is (= (fizz-buzz 15) ["1" "2" "Fizz" "4" "Buzz" "Fizz" "7" "8",
                           "Fizz" "Buzz" "11" "Fizz" "13" "14" "FizzBuzz"])))
  (testing "throws error on invalid inputs"
    (is (thrown? AssertionError (fizz-buzz 0)))
    (is (thrown? AssertionError (fizz-buzz nil)))))

(run-tests)
Clojure 1.8.0 Java HotSpot(TM) 64-Bit Server VM 1.8.0_91-b14
?