Scala define a standard function

The following equivalents:

scala> val f1 = {i: Int => i == 1}
f1: Int => Boolean = <function1>

scala> val f2 = (i: Int) => i == 1
f2: Int => Boolean = <function1>

I am more familiar with the former (based on Groovy), but the latter form is much more common, AFAIK, the standard way to define a function in Scala.

Should I forget the past (Groovy) and take a second form? 1st form is more natural for me, because it looks like Groovy / Ruby / Javascript way of defining closures (functions)

EDIT
See Zeiger's answer in this thread , for example, where Groovy / ruby ​​/ javascript close syntax {=>}seems more natural than () =>I assume that both can be used interchangeably with the same performance, passing ability, etc. and that the only difference is the syntax

+3
2

, (scala styleleguide ). ( > 2 ):

val f1 = { i: Int =>
  val j = i/2
  j == 1
}

+2

"" .

: , .

scala> val f0 = { (x: Int, y: Int) => val rest = x % y; x / y + (if (rest > 0) 1 else 0) }
f0: (Int, Int) => Int = <function2>

"groovy": , .

scala> val f1 = { x: Int => val square = x * x; square + x }
f1: Int => Int = <function1>

scala: , .

scala> val f2 = (x: Int, y: Int) => x * y
f2: (Int, Int) => Int = <function2>

, (.. ).

+2

All Articles