我“应该”如何使用 scala.collection.immutable.Queue?
我认为处理队列的最常见情况是这样的。我将读取队列的前面,对元素进行操作(这可能会导致更多元素添加到队列中),然后循环直到队列为空。
- 我的第一直觉是 foreach,但不是,显然队列(即使是可变队列)是严格的,并且 foreach 在迭代开始时循环遍历队列中的所有元素。
- 我无法弄清楚
while
循环的语法。
您可能会认为这会起作用
while (!q.isEmpty) {
var (e, q) = q.dequeue
... }
,只是我重新声明了 q
。这确实有效:
while (!q.isEmpty) {
var (e, q1) = q.dequeue
q = q1
... }
但是伙计,它看起来错误吗?
I have what I would think is the most common case for processing a queue. I will read off the front of the queue, act on the element (which may cause more elements to be added to the queue), and then loop until the queue is empty.
- My first instinct was
foreach
, but no, apparently a queue (even a mutable one) is strict and the foreach loops over all the elements that are in the queue when the iteration starts. - I cannot figure out the syntax for a
while
loop.
You'd think that it would be something like
while (!q.isEmpty) {
var (e, q) = q.dequeue
... }
would work, except that I'm redeclaring q
. This does work:
while (!q.isEmpty) {
var (e, q1) = q.dequeue
q = q1
... }
but man, does it look wrong ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是完全避免任何变量的一种方法:
从初始队列
q0
开始,然后在第qi
步中,将某些内容出列并生成一个新队列,如果需要,将其返回以进行下一步。您剩下的就是停止条件(不为空),然后由于这只是定义了一个进程,而不是实际操作,因此您必须运行它(例如,使用无操作 foreach)。
Here's one way to avoid any vars at all:
You start with the initial queue,
q0
, and then on theqi
th step, you dequeue something and produce a new queue if need be, returning that for the next step.All you have left is the stopping condition (not empty), and then since this just defines a process, not the actual action, you have to run it (using a no-op foreach, for example).
而Rex Kerr的答案很好,迭代器是可变的。这是一个真正不可变的解决方案,非常接近 Rex Kerr 自己的答案中的代码。
While Rex Kerr's answer is good, iterators are mutable. Here's a truly immutable solution, modeled very closely on the code in Rex Kerr's own answer.
在
while
循环中处理Queue
可以在没有重复的var
/val
的情况下完成,如下所示:(我是意识到这个答案晚了 7 年,但我认为它仍然是一个有价值的选择。)
Processing a
Queue
in awhile
loop can be done without a duplicatevar
/val
as follows:(I am aware that this answer is 7 years late, but I thought it a valuable alternative nonetheless.)