Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is foreach by-definition guaranteed to iterate the subject collection sequentially in Scala?

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?

like image 549
Ivan Avatar asked Feb 24 '12 23:02

Ivan


People also ask

How does foreach work in Scala?

The foreach function is applicable to both Scala's Mutable and Immutable collection data structures. The foreach method takes a function as parameter and applies it to every element in the collection. As an example, you can use foreach method to loop through all elements in a collection.

Does foreach happen in order?

The foreach statement in some languages has some defined order, processing each item in the collection from the first to the last. The foreach statement in many other languages, especially array programming languages, does not have any particular order.

Which expression is one way to iterate over a collection in Scala?

A simple for loop that iterates over a collection is translated to a foreach method call on the collection. A for loop with a guard (see Recipe 3.3) is translated to a sequence of a withFilter method call on the collection followed by a foreach call.

Which expression is one way to iterate over a collection and generate a collection of each iteration's result in Scala?

In scala, for loop is known as for-comprehensions. It can be used to iterate, filter and return an iterated collection.


2 Answers

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.

like image 69
Rex Kerr Avatar answered Oct 17 '22 00:10

Rex Kerr


To complement the answer by Rex, foreach at GenTraversableOnce only guarantees all elements will be iterated through, unless you interrupt it. Traits lower on the hierarchy chain may provide additional guarantees, for example:

  • TraversableOnce and descendants guarantee that only one element will be iterated through at a time (not guaranteed by GenTraversableOnce!).
  • Seq and descendants guarantee that elements will be traversed on the order they are kept in the collection.

And since you asked about parallel foreach...

scala> (1 to 10).par foreach println
4
6
1
3
2
7
5
8
9
10
like image 34
Daniel C. Sobral Avatar answered Oct 17 '22 00:10

Daniel C. Sobral