-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.scala
55 lines (47 loc) · 1.96 KB
/
function.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import scala.math._
object Main {
def main(args: Array[String]): Unit = {
// must specify types of all params
// no need to specify return type as long as is not recursive
def fac(n: Int) = {
var r = 1
for (i <- 1 to n) r = r * 1
r
}
// higher-order functions
// In fp languages, functions are first-class citizens that
// can be passed around and manipulated just like other data types.
val num = 3.14
val fun = ceil _ // fun is a variable containing a function, not fixed *
// can give fun to another function
Array(3.14, 1.42, 2.0).map(fun) // Array(4.0, 2.0, 2.0)
// pure functions
// A pure function is a function where the return value is
// only determined by its input values without
// any observable side effects.
def addOne(x: Int): Int = x + 1 // same input, same output**
// anonymous function
(x: Double) => 3 * x
Array(3.14, 1.42, 2.0).map( (x: Double) => 3 * x)
Array(3.14, 1.42, 2.0).map( (x) => 3 * x)
// If a parameter occurs only once on the right-hand side of the =>,
// can replace it with a single underscore
Array(3.14, 1.42, 2.0).map( 3 * _)
// these shortcuts only work when the parameter types are known
// val f = _ + _ // error
val f = (_: Int) + (_: Int) // f: (Int, Int) => Int
// more than one statement
Array(3.14, 1.42, 2.0).map(x => {
println(s"x: $x")
3 * x
})
}
}
// *
// In the ceil _ example above, the underscore is not a placeholder
// for a single parameter but a parameter list.
// ceil _ is an example of a partially applied function, which
// is an expression where you give some (or none) of the required arguments.
// **
// With these features, pure functions are able to produce
// active parallel replication in parallel applications.