2

Say I'have a lazy sequence called numbers that's giving me an infinite sequence of numbers: 0, 1, 2, 3, 4, 5, 6...

(def numbers (iterate inc 0))

I limit the infinity by passing it to the function take. e.g.:

(take 3 numbers)
; > (0 1 2)

I'm asking myself how to add some post-processing to the members of the lazy sequence. More concretely: How would I declare a function "numbers-doubled" that would produce the following output, when I use take:

(take 3 numbers-doubled)
; > ("00" "11" "22")
Anton Harald
  • 5,772
  • 4
  • 27
  • 61

1 Answers1

6

You can use a map function in between your iterate function and the take. Since map is lazy, it will only consume as many as requested by the later take function.

(take 3 (map #(str % %) numbers))

You could easily def that map to make it its own infinite sequence:

(def numbers-doubled
  (map #(str % %) numbers))

(take 3 numbers-doubled)
rabidpraxis
  • 556
  • 3
  • 10