1

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,

Monika
  • 2,172
  • 15
  • 24
Horace
  • 1,198
  • 2
  • 17
  • 31
  • Can you add a few steps to your final expected output, i cant fully understand the sense of your requirements – tangrammer Nov 15 '13 at 13:26
  • Hi tangrammer. Thx for your replying. I actually want no output at this point. The point is that there is another function with takes as argument a seq of seq of maps, that` s to say (({:a 1 :b 1})). – Horace Nov 15 '13 at 13:31

2 Answers2

1

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.

Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
  • Thx Leonid. And you were right that there is no need to make both lists lazy. It was an error in my reasoning ;-) – Horace Nov 18 '13 at 09:04
0

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
Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
Shlomi
  • 4,708
  • 1
  • 23
  • 32