Created
November 17, 2014 04:43
-
-
Save rmunn/70ee11d94b977ec1f1fc to your computer and use it in GitHub Desktop.
FizzBuzz with transformation functions
This file contains 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
;; 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