Does Clojure get your head around?

I have a very large lazy sequence and I would like to convert it to a set. I know that the number of individual items in a sequence is small, so I can easily set the set to memory. However, I may not be able to fit the entire lazy segment into memory. I just want to do it (into #{} my-lazy-seq)

, but it occurred to me that depending on how it is implemented into

, this might dump the entire seq into memory at once.

Will it be into

held at the beginning of the sequence when it is running?

+3


source to share


2 answers


I don't see an increase in the use of this app (takes a minute or so)

user=> (into #{} (take 1e8 (cycle [:foo :bar])))
#{:bar :foo}

      

A more accurate proof would be to check the source for into

, and we see this as just a fancy call reduce

:



(defn into
  ([to from]
     ;...
       (reduce conj to from)))

      

If reduce

rests on the head, it into

does. But I don't think it reduce

does.

+6


source


In addition to @progo's answer, you can always use

(source into)

      



to check the source code into

in repl. This can save you some time searching for lines in the longer core.clj.

+3


source







All Articles