I am playing around with lazy lists and cannot seem to figure this out. I think I can solve my problem by writing this as one big recursive function that does all that is necessary, but I would like to compose it of simpler functions.
I will try to write a simple example that should translate to my problem easily:
(defn numbers
([] (numbers 1))
([n] (cons n (lazy-seq (numbers (inc n))))))
(defn repeat-n [n]
(take n (repeat n)))
So, we have two functions. One returns a lazy sequence of numbers. The other returns its number parameter n times (hopefully it is also lazy; if not it seems easy to write one that would be).
I would like to map the repeat-n to numbers in such a way as to return lazy sequence of results. I have played around a bit with lazy-seq
, lazy-cat
, concat
, and recursive functions, but I still have problems with it.
The function should be something like this
(lazy-concat-map [f items] ...)
And (hopefully) the result of calling
(take 11 (lazy-concat-map repeat-n numbers))
would be
12233344445
Any ideas?
(take 11 (mapcat #(repeat % %) (range)))
;=> (1 2 2 3 3 3 4 4 4 4 5)
The functions map
, concat
(and combination mapcat
), as well as repeat
and range
are all lazy.
List comprehension, for
, is also lazy
(take 11 (apply concat (for [x (range)] (repeat x x))))
;=> (1 2 2 3 3 3 4 4 4 4 5)