Skip to content

Instantly share code, notes, and snippets.

@rmunn
Created November 17, 2014 04:43
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 rmunn/70ee11d94b977ec1f1fc to your computer and use it in GitHub Desktop.
Save rmunn/70ee11d94b977ec1f1fc to your computer and use it in GitHub Desktop.
FizzBuzz with transformation functions
;; FizzBuzz with transformation functions
(defn reduce'
"Alternate reduce with reordered params, so -> can be used easily."
[val f coll]
(reduce f val coll))
(def fizz (constantly "fizz"))
(def buzz (constantly "buzz"))
(def fb (constantly "fizzbuzz"))
; We could just build this list by hand, but that's boring. Let's be functional-ish.
(def fb-transforms
(-> (into [] (repeat 15 identity)) ; Replace "identity" with "str" if you want output to *always* be a string
(reduce' #(assoc %1 %2 fizz) (take 5 (iterate #(+ % 3) 0))) ; Every third function is now fizz
(reduce' #(assoc %1 %2 buzz) (take 3 (iterate #(+ % 5) 0))) ; Every fifth function is now buzz
(assoc 0 fb)))
; fb-transforms is now something like: [fb id id f id b f id id f b id f id id]
(defn fizzbuzz
"Given an integer, return its fizzbuzz value"
[n]
((get fb-transforms (mod n 15)) n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment