开发者

Handling the return of the queue:out_r

开发者 https://www.devze.com 2023-01-28 00:05 出处:网络
I have a statement where I want to remove the last item in a queue using out_r. The Docs say that the return is

I have a statement where I want to remove the last item in a queue using out_r.

The Docs say that the return is

Result = {{value, Item}, Q2} | {empty, Q1}

Q1 = Q2 = queue()

How do I handle thi开发者_运维问答s if I just want to get the queue with the item removed?

How do I get the queue and disregard the {value, item}?

e.g. NewQueue = queue:out_r(OldQueue)

Thanks


Use pattern matching!

{_, NewQueue} = queue:out_r(OldQueue)

Given both {value, Item} and empty are returned as the first element of the tuple, ignoring the first element will do what you want.

Note that the queue module supports 3 APIs. Other APIs might do what you want better. In this case, you can have the same function, except it crashes if the queue is empty:

drop_r(Q1) -> Q2
Returns a queue Q2 that is the result of removing the rear item from Q1.
Fails with reason empty if Q1 is empty.

Picking one or the other depends on your applications and what you expect to be in the queue, if you can handle an empty one, etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消