-
-
Save zjhmale/9054713cbefaad73cf9a to your computer and use it in GitHub Desktop.
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
(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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
partition-all
这个实现有点取巧 因为在range中下标的值就是容器中元素的值 所以可以直接 partition 然后每一个partition 头部的元素就是3的倍数或者5的倍数或者15的倍数