Skip to content

Instantly share code, notes, and snippets.

@chrishowejones
Last active August 29, 2015 14:20
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 chrishowejones/7872db444d8d63f85eec to your computer and use it in GitHub Desktop.
Save chrishowejones/7872db444d8d63f85eec to your computer and use it in GitHub Desktop.
FizzBuzz without conditionals
;; Generates a range of numbers and returns "Fizz" if divisible only by 3, "Buzz"
;; if divisible only by 5 and "FizzBuzz" if divisible by 15 otherwise returns
;; the number as a string.
;; It does this by getting the modulus of 15 then using this as the index for a
;; map to look up the index of a second map where the index returned is the one
;; for "Fizz" for modulus result 3, 6 & 9 "Buzz" for 5 & 10 and "FizzBuzz" for
;; 0 (i.e. divisable by 15). Anything else will return nil from the first map
;; where nil is mapped to the value of the number in the second map.
;; Finally str is called to ensure that when the number is returned unchanged
;; it is transformed into a string to match "Fizz", "Buzz" & "FizzBuzz".
(for [n (range 1 101)]
(-> n
(mod 15)
({3 0, 5 1, 6 0, 9 0, 10 1, 12 0, 0 2})
({nil n, 0 "Fizz", 1 "Buzz", 2 "FizzBuzz"})
str))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment