For the cycle
You can break it down on map .
for( line <- Source.fromFile(args(0)).getLines() ) { val currentLine = line println(currentLine) }
So this code is converted to
Source.fromFile(args(0)).getLines().map( line => block )
block can be any expression. Where in your case block is:
{ val currentLine = line println(currentLine) }
Here, currentLine is local to block and is created for each of the line values ββspecified for the map operation.
If-else
Again, the following is not true:
if( some condition is satisfied) val x = 2 else val x = 3
Essentially, if-else in Scala returns a value. Therefore, it should be:
if( condition ) expression1 else expression1
In your case, it could be:
if( condition ) { val x = 2 } else { val x = 3 }
However, assignment returns Unit (or void if you need an analogy with Java / C ++). Therefore, you can simply take the if-else value as follows:
val x = if( condition ) { 2 } else { 3 } // OR val x = if( condition ) 2 else 3
tuxdna
source share