Horace
Horace

Reputation: 1208

clojure: create a lazy-seq containing another lazy-seq

I would like to create a lazy-seq containing another lazy-seq using clojure.

The data structure that I aready have is a lazy-seq of map and it looks like this:

({:a 1 :b 1})

Now I would like to put that lazy-seq into another one so that the result would be a lazy-seq of a lazy-seq of map:

(({:a 1 :b 1}))

Does anyone know how to do this? Any help would be appreciated

Regards,

Upvotes: 1

Views: 178

Answers (2)

Shlomi
Shlomi

Reputation: 4748

generally, there's nothing special about having a lazy-seq containing many lazy-seq's, so i dont understand exactly what it is you are really after.

you could always do

(map list '({:a 1 :b 1}))   ;; gives (({:a 1, :b 1}))

we can even verify that it maintains laziness:

(def a
  (concat
   (take 5 (repeat {:a 1 :b 2}))
   (lazy-seq
    (throw (Exception. "too eager")))))

(println (take 5 (map list a))) ;; works fine
(println (take 6 (map list a))) ;; throws an exception

Upvotes: 0

Leonid Beschastny
Leonid Beschastny

Reputation: 51450

Here is an example of creating a list containing a list of maps:

=> (list (list {:a 1 :b 1}))
(({:a 1, :b 1}))

It's not lazy, but you can make both lists lazy with lazy-seq macro:

=> (lazy-seq (list (lazy-seq (list {:a 1 :b 1}))))

or the same code with -> macro:

=> (-> {:a 1 :b 1} list lazy-seq list lazy-seq)

Actually, if you'll replace lists here with vectors you'll get the same result:

=> (lazy-seq [(lazy-seq [{:a 1 :b 1}])])
(({:a 1, :b 1}))

I'm not sure what you're trying to do and why do you want both lists to be lazy. So, provide better explanation if you want further help.

Upvotes: 1

Related Questions