Skip to content

Instantly share code, notes, and snippets.

@orend
Last active April 29, 2019 19:50
Show Gist options
  • Save orend/4ec5d898a74379ee6b0ad1d7bd2fcc04 to your computer and use it in GitHub Desktop.
Save orend/4ec5d898a74379ee6b0ad1d7bd2fcc04 to your computer and use it in GitHub Desktop.
; This function is a great solution to the "fizzbuzz" interview question.
(defn fizz-buzz [n]
(condp (fn [a b] (zero? (mod b a))) n
15 "fizzbuzz"
3 "fizz"
5 "buzz"
n))
; Output:
(map fizz-buzz (range 25))
("fizzbuzz" 1 2 "fizz" 4 "buzz" "fizz" 7 8 "fizz" "buzz" 11 "fizz" 13 14 "fizzbuzz" 16 17 "fizz" 19 "buzz" "fizz" 22 23 "fizz")
; And here's another solution
(clojure.pprint/pprint
(map vector
(range 25)
(cycle [:fizz :_ :_])
(cycle [:buzz :_ :_ :_ :_])))
; Credit to http://clojure-and-me.blogspot.co.uk/2012/08/functional-fizzbuzz.html
; Output:
([0 :fizz :buzz]
[1 :_ :_]
[2 :_ :_]
[3 :fizz :_]
[4 :_ :_]
[5 :_ :buzz]
[6 :fizz :_]
[7 :_ :_]
[8 :_ :_]
[9 :fizz :_]
[10 :_ :buzz]
[11 :_ :_]
[12 :fizz :_]
[13 :_ :_]
[14 :_ :_]
[15 :fizz :buzz]
[16 :_ :_]
[17 :_ :_]
[18 :fizz :_]
[19 :_ :_]
[20 :_ :buzz]
[21 :fizz :_]
[22 :_ :_]
[23 :_ :_]
[24 :fizz :_])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment