Why is there no peek! function for clojure transient vectors?
Asked Answered
P

1

10

Clojure has transient analogs for some of its persistent data structures, vectors, maps and sets. For vectors, there are pop! and conj! functions, analogous to pop and conj for persistent vectors, but no peek!.

Is there a technical reason that makes an efficient implementation of peek! impossible? Or is it just not necessary in most use cases for transient vectors? I can always do

(defn peek! [tvec] (get tvec (dec (count tvec))))

But it seems strange that there's no built-in solution.

Proprioceptor answered 8/7, 2010 at 19:40 Comment(0)
B
5

That's really a design question best directed to the ggroup, but FWIW, I did investigate peek / peek! a while ago and providing peek! seems to be a simple matter of creating a new clojure.lang.ITransientStack interface to parallel clojure.lang.IPersistentStack and having transient vectors implement it.

My guess is that if such an interface is not already available (and used by transients), it's probably a matter of priorities. A single-threaded fast stack implementation is already available in Clojure in the form of java.util.Stack, so we're not missing out on that many features here; syntactic convenience and smooth conversion to persistent vectors will probably come as headway is made on Clojure-in-Clojure.

(Where the return on the effort invested is high, improvements to the Java side of Clojure make sense even if the ultimate goal is eventually to drop the relevant part of the Java codebase and replace it with an implementation in Clojure. Where expected returns are lower, it might make more sense to wait for protocols to be used more pervasively etc. The currently available set of functions for handling transients suffices for Clojure's own needs and I'm not sure if there's ever been call for peek! on the ggroup -- as for #clojure, I remember one relevant conversation -- so the return is probably judged low... You could start a grassroots movements to change this, though. :-))

Burford answered 8/7, 2010 at 22:14 Comment(1)
Thank you very much, that helped. I'm not in dire need of a peek! function, but if that changes, I may think about starting the grassroots campaign. For now I wouldn't want to bother our wise BDFL or his lieutenants.Proprioceptor

© 2022 - 2024 — McMap. All rights reserved.