Log In  

Pico lacks native stack and queue support. I'm unhappy about it, so i cried a bit and my friend GreyKnight came up with a small implementation of a deque (queue/stack combo). From preliminary tests it works fine.

-- 3 tokens

-- 19 tokens
function pop(stack)
    local v = stack[#stack]
    return v

-- 11 tokens
function pop_discard(stack)

-- 3 tokens

-- 16 tokens
function dequeue(queue)
    local v = queue[1]
    del(queue, v)
    return v

----- Usage -----
stack = {}
push(stack, "goat")
push(stack, "billy")
assert("billy" == pop(stack))
assert("goat" == pop(stack))

queue = {}
enqueue(queue, "nanny")
enqueue(queue, "kid")
assert("nanny" == dequeue(queue))
assert("kid" == dequeue(queue))

If you want to save tokens you can do away with defining push and enqueue and just use add() instead.

P#20837 2016-05-19 03:40 ( Edited 2016-05-21 10:00)

:: keiya

You can also just drop pop_discard, and not use the return value. It's only worth spending those 11 tokens if you don't need pop in the first place, and that would be a bit weird.

P#20937 2016-05-20 13:57 ( Edited 2016-05-20 17:57)

Right, I just wrote that function as a potential replacement for pop() in case you were doing something that never cared about the popped value and really desperately needed to save 8 tokens. Hey, I don't judge. :o)

P#20995 2016-05-21 06:00 ( Edited 2016-05-21 10:00)

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2022-05-23 23:20:36 | 0.005s | Q:10