Вопрос

How can I lazily "clone" a seq in Clojure. Something along the lines of

(let [[s1 s2] (clone-seq s)]
 ...)

such that s1 and s2 are independent seqs backed by s?

Это было полезно?

Решение

"cloning" a seq feels wrong to me: in normal Clojure usage you would expect seqs to be immutable, so it should be perfectly fine to just do something like:

(let [s1 s
      s2 s]
   .....)

If your seqs are mutable or have some kind of side effects when they are traversed, then you are likely to run into problems for different reasons: mutable seqs tend not to be a good fit for a functional language like Clojure. You'll run into all sorts of odd issues: do you want the side effects to happen twice when you "clone" a seq for example? Do you need a deep clone of all the contents as well?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top