是否可以确保按foreach定义顺序在Scala中迭代主题集合? [英] Is foreach by-definition guaranteed to iterate the subject collection sequentially in Scala?

查看:79
本文介绍了是否可以确保按foreach定义顺序在Scala中迭代主题集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

foreach 的定义是否保证从头到尾(除非意外中断)元素依次迭代主题集合(如果它定义了顺序)?是否没有任何编译器优化开关可以阻止它(改组序列)或计划在将来的版本中使普通的 foreach 并行?

Is foreach by-definition guaranteed to iterate the subject collection (if it defines order) sequentially from the very first to the very last (unless accidentally interrupted) element? Aren't there any compiler optimization switches which can brake it (shuffle the sequence) or plans to make the ordinary foreach parallel in future versions?

推荐答案

对于顺序收集,保证Foreach是顺序的 (即,正常的层次结构,或由<$ c $转换的任何内容) c> .seq )。集合库的并行部分(可通过 .par 或通过显式使用 collection.parallel )几乎可以保证 not 进行评估。如果您想成为不可知论者,可以使用 GenX 一组特征(例如 GenSeq );这些都不能保证执行顺序,也不能保证同时进行工作。

Foreach is guaranteed to be sequential for sequential collections (that is, the normal hierarchy, or for anything transformed by .seq). The parallel part of the collections library (which you get from a standard collection via .par or by explicitly using classes from collection.parallel) are pretty much guaranteed not to evaluate in order. If you want to be agnostic, you can use the GenX set of traits (e.g. GenSeq); these provide no guarantees either on execution order or that work will be done in parallel.

这篇关于是否可以确保按foreach定义顺序在Scala中迭代主题集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆