Last active
December 17, 2015 04:20
-
-
Save zaz/701554ac7545ca6f4ca8 to your computer and use it in GitHub Desktop.
A simple solution to FizzBuzz — Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn fizzbuzz? | |
"Determines what to print for a given number" | |
[x] | |
(condp #(zero? (mod %2 %1)) x | |
15 "fizzbuzz" | |
3 "fizz" | |
5 "buzz" | |
1 x)) | |
(defn fizzbuzz | |
"Prints fizzes and buzzes for numbers between 1 and x" | |
[x] | |
(doseq [i (range 1 (inc x))] | |
(println (fizzbuzz? i)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Notes:
(rem)
can be used in place of(mod)
with no change to the code — the difference in efficiency seems negligible.(and (mod 3 x) (mod 5 x)) == (mod 15 x)
because a number is divisible by 3 and 5 ⟷ the number is divisible by 15.I prefer
doseq
, butdotimes
can be used instead in the body offizzbuzz
: