Search code examples
clojurelazy-sequences

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,


Solution

  • 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.