How to convert lazy sequence to non-lazy in Clojure

前端 未结 5 1152
陌清茗
陌清茗 2020-11-29 18:19

I tried the following in Clojure, expecting to have the class of a non-lazy sequence returned:

(.getClass (doall (take 3 (repeatedly rand))))
5条回答
  •  青春惊慌失措
    2020-11-29 18:32

    This is to some degree a question of taxonomy. a lazy sequence is just one type of sequence as is a list, vector or map. So the answer is of course "it depends on what type of non lazy sequence you want to get:
    Take your pick from:

    • an ex-lazy (fully evaluated) lazy sequence (doall ... )
    • a list for sequential access (apply list (my-lazy-seq)) OR (into () ...)
    • a vector for later random access (vec (my-lazy-seq))
    • a map or a set if you have some special purpose.

    You can have whatever type of sequence most suites your needs.

提交回复
热议问题