Created
July 6, 2013 10:01
-
-
Save mattdenner/5939454 to your computer and use it in GitHub Desktop.
Messing about with lazy sequences in clojure using fizzbuzz as an environment
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
; Two infinite sequences of repeating pattern. | |
(def threes (lazy-cat [nil nil "fizz"] threes)) | |
(def fives (lazy-cat [nil nil nil nil "buzz"] fives)) | |
; A lazy sequence of "fizz", "buzz", "fizzbuzz" or nil | |
(def fizz-buzz-ing (map (fn [t f] (let [s (str t f)] (if-not (empty? s) s))) threes fives)) | |
; A lazy sequence of "fizz", "buzz", "fizzbuzz" or a number (starts at 1) | |
(def fizz-buzz (map (fn [f v] (or f v)) fizz-buzz-ing (rest (range)))) | |
(take 30 fizz-buzz) | |
; => (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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment