September 2015
Intermediate to advanced
250 pages
6h 40m
English
The foreach method provides internal iterators on collections—you don’t control the looping. You simply provide code to execute in the context of each iteration. However, if you’d like to control the looping or work with multiple collections at the same time, you can use an external iterator, the for expression. Let’s look at a simple loop:
| UsingCollections/PowerOfFor.scala | |
| | for (i <- 1 to 3) { print("ho ") } |
The code prints “ho ho ho.” It’s a short form of the general syntax of the following expression:
| | for([pattern <- generator; definition*]+; filter*) |
| | [yield] expression |
The for expression takes as a parameter one or more generators, with zero or more definitions and zero or more filters. These are separated from ...
Read now
Unlock full access