Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the Clojurescript cljs.core.PersistentQueue queue?

I can't seem to find any documentation on the Clojurescript cljs.core.PersistentQueue. Should I be using it at all? Or should I be using another method of making a Clojurescript queue?

Update

In the meantime I am using channels, (<!, (>! and go blocks and this seems to do the trick

like image 249
yazz.com Avatar asked Dec 15 '14 15:12

yazz.com


2 Answers

ClojureScript actually has a tagged literal #queue [] for creating queues, which I found after putting Mike's answer into a repl

cljs.user=> cljs.core/PersistentQueue.EMPTY    
#queue []

cljs.user=> #queue []
#queue []

cljs.user=> (def q #queue [1 2 3])
#queue [1 2 3]

cljs.user=> (conj q 4)
#queue [1 2 3 4]

cljs.user=> (pop q)
#queue [2 3]

cljs.user=> (peek q)
1
like image 77
Shaun Lebron Avatar answered Sep 28 '22 20:09

Shaun Lebron


A PersistentQueue is another persistent data structure with different behavior and performance characteristics when compared to list, vector, map, and set. If you look at the docstrings for pop and peek, for example, you will see this datatype being referred to as "queue".

Since it has no literal syntax, you have to start off by creating an empty one using cljs.core.PersistentQueue/EMPTY.

This post provides a good high-level summary of the Clojure equivalent https://stackoverflow.com/a/2495105

like image 23
Mike Fikes Avatar answered Sep 28 '22 19:09

Mike Fikes