Skip to content

Instantly share code, notes, and snippets.

@rfunduk
Last active December 17, 2015 00:39
Show Gist options
  • Select an option

  • Save rfunduk/5522295 to your computer and use it in GitHub Desktop.

Select an option

Save rfunduk/5522295 to your computer and use it in GitHub Desktop.
(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)
)
)
)
)
@rfunduk
Copy link
Author

rfunduk commented May 5, 2013

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 ->> and defmacro in 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?

brew install leiningen
lein new app fizzbuzz && cd fizzbuzz
export FB_URL="https://gist.github.com/rfunduk/5522295/raw/3c2e1e6a6e10944cea183d71293d7e1f152070b3/fizzbuzz.clj"
curl $FB_URL -o src/fizzbuzz/core.clj
lein run 100

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment