Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a lazy-seq vector

Running this works as expected:

(defn long-seq [n]
  (lazy-seq (cons 
             (list n {:somekey (* n 2)})
             (long-seq (+ n 1)))))
(take 3 (long-seq 3))
; => ((3 {:somekey 6}) (4 {:somekey 8}) (5 {:somekey 10}))

However I would like to do the same thing with a vector:

(defn long-seq-vec [n]
  (lazy-seq (into 
             (vector (list n {:somekey (* n 2)}))
             (long-seq-vec (+ n 1)))))
(take 3 (long-seq-vec 3))

This gives me a stack overflow. Why?

like image 876
snowape Avatar asked Aug 30 '12 23:08

snowape


1 Answers

The main reason is that vectors aren't lazy - so the into call greedily consumes the recursive sequences produced by long-seq-vec and results in a stack overflow. As a corollary of this, it's not possible to create an infinite vector (in general, you can only create an infinite data structure if it is lazy or cyclic).

It works in the first example because cons is quite happy to behave lazily when consing onto the front of a lazy sequence, so the sequence can be infinite.

Assuming you actually want an infinite sequence of vectors I'd suggest something like:

(defn long-seq-vec [n]
  (lazy-seq (cons 
              (vector n {:somekey (* n 2)})
              (long-seq-vec (+ n 1)))))

(take 3 (long-seq-vec 3))

=> ([3 {:somekey 6}] [4 {:somekey 8}] [5 {:somekey 10}])

Or as an alternative, you can use for which is lazy in itself:

(defn long-seq-vec [n]
  (for [x (iterate inc n)]
    (vector x {:somekey (* x 2)})))

I prefer this as it avoids the lazy-seq/cons boilerplate, avoids recursion and is slightly more clear in expressing what your function does... it's a little more "declarative" if you like. You could also use map in a similar way.

like image 160
mikera Avatar answered Nov 03 '22 00:11

mikera