Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clone seq in Clojure

Tags:

clojure

seq

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?

like image 688
Robert Onslow Avatar asked Jan 15 '23 20:01

Robert Onslow


1 Answers

"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?

like image 67
mikera Avatar answered Jan 21 '23 08:01

mikera