Skip to content

Instantly share code, notes, and snippets.

@mattdenner
Created July 6, 2013 10:01
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 mattdenner/5939454 to your computer and use it in GitHub Desktop.
Save mattdenner/5939454 to your computer and use it in GitHub Desktop.
Messing about with lazy sequences in clojure using fizzbuzz as an environment
; 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