Skip to content

Instantly share code, notes, and snippets.

@zjhmale
Forked from gigasquid/fizzbuzz.clj
Last active August 29, 2015 14:23
Show Gist options
  • Save zjhmale/9054713cbefaad73cf9a to your computer and use it in GitHub Desktop.
Save zjhmale/9054713cbefaad73cf9a to your computer and use it in GitHub Desktop.
(defn fizzbuzz [n]
(let [all-nums (range 0 n)
folder (fn [fb-str p-num fb-coll]
(mapcat (fn [x] (cons fb-str (rest x)))
(partition-all p-num fb-coll)))
fizz (folder "fizz" 3 all-nums)
buzz (folder "buzz" 5 fizz)
fizzbuzz (folder "fizzbuzz" 15 buzz)]
fizzbuzz))
(fizzbuzz 33)
;; -> ("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" "buzz" 26 "fizz" 28 29 "fizzbuzz" 31 32)
@zjhmale
Copy link
Author

zjhmale commented Jun 28, 2015

partition-all

这个实现有点取巧 因为在range中下标的值就是容器中元素的值 所以可以直接 partition 然后每一个partition 头部的元素就是3的倍数或者5的倍数或者15的倍数

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment