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.

4


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.


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)



[Please log in to post a comment]