Implementing a Queue


#1

I’m slowly working on moving some of my old SmartApps to pistons. One of them used groovy’s Queue class.

Is there a way to use this class (or something equivalent) within a piston?


(creating a queue) Append to a list of strings while pulling the first item in the list
#2

i have kind of like a queue implemented in one of my pistons, but only if you squint just the right way. :slight_smile:

theres no way to use the queue class in wC that i know of. but you could use a list[…] variable with pointers to implement a queue within wC.


#3

heres the piston that does it. remember i said you have to squint. :slight_smile: but the basic principle of a queue implementation is the same as what is done here with colorsToShow and colorIndex.


#4

Another way to manage a queue with a single variable is to leverage a comma-separated string variable which will act like an array when provided to some of the array functions. This may be a useful solution if you have to manage several queues in a single piston and don’t want to track indexes for all of them.

Here is an example piston that shows expressions that could be used to manage a comma-separated string queue. Note that arrayItem() and count() interpret a comma-separated string as a list.

The following test runs through a few basic operations:

for which the Queue piston logs:

Don’t get too excited about this “Queue” piston though, you probably can’t use it how you would want (execute from other pistons and get the result). The real-life scenario would involve using the expressions shown to interact with the queue in a single piston.


#5

why not use a list[…] variable like already shown in the example posted above yours? :slight_smile:


#6

Because it’s quite a bit more work and you have to keep track of a few indexes to know which part of the queue is still alive, I suppose.


#7

that’s easily done with 3 indexes and a cleaner solution than trying to use string variable for it.