Questions about scala

I am currently learning scala. I am reading Scala for the impatient.

1.)

Guards

Is there a difference?

for (i <- 0 to 10)if (i % 2 == 0) println(i)

for (i <- 0 to 10 if i % 2 == 0) println(i)

2.)

I always see the following symbol => but they never explain what it does.

Sometimes I think it is a cast but then it is something completely different, I hope you can clear things up.


1.) Yes, there is a difference, the first if a normal if statement inside of the closure you pass to the for-comprehension. The second is an actual guard. It will actually call withFilter on the range, before calling foreach . So the translation of the two things will look like this:

0.to(10).foreach(i => if(i % 2 == 0) println(i) )
0.to(10).withFilter(x => x % 2 == 0).foreach(i => println(i))

To add a little more context, calling withFilter or even just filter instead of using a normal if statement has some benefits. In a for comprehension, you can have nested calls to map, flatmap, filter, collect etc. so if you add guards, you can prevent a lot af calls from actually happening. For example:

for {
  x <- 0 until 10
  y <- 10 until 20
} {
  if(x % 2 == 0) println(x*y)
}

would call the actual closure 100 times

for {
  x <- 0 until 10
  if x % 2 == 0
  y <- 10 until 20
} println(x*y)

this will only call it 50 times, while the result stays the same.

2.)

=> separates the argument list of a function/closure from the body.

This case e: NumberFormatException => None is a part of a partial function. Here the => separates the "argument" e from the body None .

In a type signature like in someFunction(i: (A) => Int) it implies that i is of the type Function1[A,Int] , read "function from A to Int ".

链接地址: http://www.djcxy.com/p/72848.html

上一篇: Akka源代码研究

下一篇: 关于Scala的问题