Last active
December 17, 2015 00:39
-
-
Save rfunduk/5522295 to your computer and use it in GitHub Desktop.
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
| (ns fizzbuzz.core (:gen-class)) | |
| (use '[clojure.string :only [blank? capitalize]]) | |
| ; a macro which expands (fbfn name M) to a function of one | |
| ; argument N which outputs "Name" if N % M == 0, otherwise "" | |
| (defmacro fbfn [name test] | |
| `(fn [arg#] (if (zero? (mod arg# ~test)) | |
| ~(capitalize (str name)) "")) | |
| ) | |
| ; specify all the rules here, for instance you could add (fbfn quux 7) | |
| ; to this vector and get 'FizzQuux' instead of 'Fizz' for N = 21 | |
| (def fbfns [ (fbfn fizz 3) (fbfn buzz 5) ]) | |
| ; a function which takes a number (1-100, or whatever) and | |
| ; generates a string by reducing with all of the fns passed in | |
| (defn- text-for [n fns] (reduce #(->> (%2 n) (str %1)) "" fns)) | |
| (defn -main [upto] | |
| (dotimes [index (Integer/parseInt upto)] | |
| (println | |
| (let [n (inc index) | |
| text (text-for n fbfns)] | |
| ; if the text is blank, none of fbfns matched | |
| ; so just output the number | |
| (if (blank? text) n text) | |
| ) | |
| ) | |
| ) | |
| ) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ridiculous FizzBuzz in Clojure
I tried to use several completely unnecessary features where possible... just in case you were wondering why in seven hells you are seeing
->>anddefmacroin here :)It does at least, as a result, support extension! (see here)
This is my 2nd Clojure program ever, just playing around with it.
Want to run it?