Skip to content

Instantly share code, notes, and snippets.

@tomjack
Created October 2, 2009 01:56
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 tomjack/b99ca5c6e87318540cf5 to your computer and use it in GitHub Desktop.
Save tomjack/b99ca5c6e87318540cf5 to your computer and use it in GitHub Desktop.
(ns com.tomojack.fizzbuzz
(:use [clojure.contrib.str-utils :only (str-join)]))
(defmacro deffizzer [name fizzmap]
`(defn ~name [~'n]
(let [~'s (str ~@(for [[modulo string] (sort fizzmap)]
`(when (zero? (mod ~'n ~modulo)) ~string)))]
(if (empty? ~'s) ~'n ~'s))))
(defn do-fizzer [fizzer]
(println (str-join \newline
(map fizzer (range 1 101)))))
(deffizzer fizzbuzz
{3 "Fizz"
5 "Buzz"})
(defn do-fizzbuzz []
(do-fizzer fizzbuzz))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment