I was reading in Practical Clojure (Chapter 5) that the rseq
function operation executes in constant time. It seems to me that it should be a linear time operation. Can anyone shed some light on this for me?
Try this:
(class [1 2 3 4])
You'll see:
clojure.lang.PersistentVector
Now try this:
(class (rseq [1 2 3 4]))
And the sequence implementation is different:
clojure.lang.APersistentVector$RSeq
As Roman said, it is a changed interface to a sequence. All the elements are where they were you are just accessing them in a reverse order.
You can see RSeq
class to see how it's implemented here: https://github.com/clojure/clojure/blob/b578c69d7480f621841ebcafdfa98e33fcb765f6/src/jvm/clojure/lang/APersistentVector.java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With