페이지

2015년 10월 27일 화요일

Scala2e Chapter2 First Steps in Scala - Step 6. Iterate with foreach and for

Step 6. Iterate with foreach and for

The functional style as you are with imperative style.

args.foreach(arg => println(arg))

$ scala pa.scala  Concise is nice

Concise
in
nice

args.foreach ((arg: String) => println(arg))



Now, by this point you may be wondering what happened to those trusty for loops you have been accustomed to using in imperative languages such as Java or C. In an effort to guide you in a functional direction, only a functional relative of the imperative for is available in Scala.

In a new file named forargs.scala, type the following:

for(arg <- args="" p="">println(arg)

for each element of the args array, a new arg val will be created and initialized to the element value, and the body of the for will be executed

$scala forargs.scala for arg in args

for
arg
in
args




댓글 없음: