Welcome! Please see the About page for a little more info on how this works.

0 votes
in Collections by

Appears to be an oversight, given that peek is a read operation.


(peek (transient [:a :b :c]))

Expected outcome:


Actual outcome:

Unhandled java.lang.ClassCastException clojure.lang.PersistentVector$TransientVector cannot be cast to clojure.lang.IPersistentStack

3 Answers

0 votes

Comment made by: steveminer@gmail.com

I just ran into this issue today. I agree it would be convenient if {{peek}} worked on a transient vector. Other functions such as {{nth}} and {{count}} work on transient vectors, so it's natural to want {{peek}}.

As a work-around (not a suggested fix), I use this:

(defn peek! [tv] (nth tv (dec (count tv))))

Admittedly, that's a misnomer but it fits the bang pattern for transient transformation of regular collection code. I also have a convenience function update! which calls assoc!. I offer the work-around just for users who run into this problem and want to get back to work.

0 votes

Comment made by: alexmiller

I think this would mean PersistentVector.TransientVector would need to support IPersistentStack but that has two methods - peek and pop. Interestingly, ITransientVector and TransientVector both have pop() already. Seems like the pop() impl has all the smarts you'd need to implement peek() too.

0 votes
Reference: https://clojure.atlassian.net/browse/CLJ-2464 (reported by alex+import)