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
push=add

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

-- 11 tokens
function pop_discard(stack)
    stack[#stack]=nil
end

-- 3 tokens
enqueue=add

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

----- 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)

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 2024-04-18 23:14:51 | 0.006s | Q:14