Clojure

peek does not support transient vectors

Details

  • Type: Defect Defect
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: Release 1.10
  • Fix Version/s: None
  • Component/s: None
  • Labels:
  • Approval:
    Triaged

Description

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

Input:

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

Expected outcome:

:c

Actual outcome:

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

Activity

Hide
Steve Miner added a comment -

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.

Show
Steve Miner added a comment - 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.
Hide
Alex Miller added a comment -

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.

Show
Alex Miller added a comment - 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.

People

  • Assignee:
    Unassigned
    Reporter:
    yhq
Vote (0)
Watch (1)

Dates

  • Created:
    Updated: