Skip to content

Instantly share code, notes, and snippets.

@justjake
Created November 25, 2012 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justjake/4142060 to your computer and use it in GitHub Desktop.
Save justjake/4142060 to your computer and use it in GitHub Desktop.
Extensable and functional fizz-buzz in Clojure
(comment
Here is FizzBuzz written by a
first-day clojure bro)
(defn divisible-by [by-num]
(fn [num]
(= 0 (rem num by-num))))
(def tests-and-outputs
(list
[(divisible-by 3) "Fizz"]
[(divisible-by 5) "Buzz"]
;; [(divisible-by 7) "Burp"]
))
(defn noise-string [num]
(->> tests-and-outputs
(filter #((first %) num))
(map last)
(reduce str "")))
(defn str-for-num [num]
(if (= (noise-string num) "")
(str num)
(noise-string num)))
(defn fizz-buzz [limit]
(let [nums (range 1 (+ limit 1))]
(->> nums
(map #(str (str-for-num %) "\n"))
(reduce str))))
(defn -main [& args]
(print (fizz-buzz 100)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment