Skip to content

Instantly share code, notes, and snippets.

@quephird
Created January 4, 2013 17:34
Show Gist options
  • Save quephird/4454421 to your computer and use it in GitHub Desktop.
Save quephird/4454421 to your computer and use it in GitHub Desktop.
FizzBuzz comes up from time to time on technical fora and so I wanted to see if I could come up with an optimal solution. I also wanted to see if I could make it somewhat extensible, being partially inspired by this blog post: http://dave.fayr.am/posts/2012-10-4-finding-fizzbuzz.html
(ns fizzbuzz)
(defn divides? [n d]
(zero? (rem n d)))
(defn fizz [n]
(if (divides? n 3) "fizz"))
(defn buzz [n]
(if (divides? n 5) "buzz"))
(defn bazz [n]
(if (divides? n 7) "bazz"))
(defn concatenator [n & fns]
(apply str (map #(% n) fns)))
(defn fizzbuzz [n]
(let [fb (concatenator n fizz buzz)]
(if (empty? fb)
n
fb)))
(defn buzzfizz [n]
(let [fb (concatenator n buzz fizz)]
(if (empty? fb)
n
fb)))
(defn fizzbuzzbazz [n]
(let [fb (concatenator n fizz buzz bazz)]
(if (empty? fb)
n
fb)))
(println (map fizzbuzz (range 1 31)))
(println (map buzzfizz (range 1 31)))
(println (map fizzbuzzbazz (range 1 31)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment