Created
November 25, 2012 01:46
-
-
Save justjake/4142060 to your computer and use it in GitHub Desktop.
Extensable and functional fizz-buzz in Clojure
This file contains hidden or 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
(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