What is the best way to obtain a simple, efficient immutable queue data type in Clojure?
It only needs two operations, enqueue and dequeue with the usual semantics.
I considered lists and vectors of course, but I understand that they have comparatively poor performance (i.e. O(n) or worse) for modifications at the end and beginning respectively - so not ideal for queues!
Ideally I'd like a proper persistent data structure with O(log n) for both enqueue and dequeue operations.
clojure.lang.PersistentQueue/EMPTYto get an empty instance. Thenconj,pop&peekwork as they should with a queue. See e.g. my answer to this question: stackoverflow.com/questions/2760017 for some code written with bothc.l.PQand Java'sLinkedBlockingQueue. - Michał MarczykPersistentQueueis indeed one of Clojure's more closely guarded secrets. ;-) About possible queue-related API enhancements, see this thread on Clojure Dev: groups.google.com/group/clojure-dev/browse_thread/thread/… Note that's probably a very low-priority matter right now, what with the new numerics and all... - Michał Marczyk