SCALA中的函数式编程

演示了值函数,匿名函数,闭包。。。

其它具体的应用,还得在生产当中吧。。

这个告一段落。。其它SAM,CURRY,高阶函数,集合,泛型,隐式类。。这些,还是找专门的书去深入了解啦。。。

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
C:\Users\hengheng>scala
Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_4
3).
Type in expressions to have them evaluated.
Type :help for more information.
 
scala> def add(x : Int, y : Int) : Int = (x + y)
add: (x: Int, y: Int)Int
 
scala> var result = add _
result: (Int, Int) => Int = <function2>
 
scala> result(1, 2)
res0: Int = 3
 
scala> (x : Int) => x + 3
res1: Int => Int = <function1>
 
scala> var fun = (x : Int) => x + 3
fun: Int => Int = <function1>
 
scala> fun(7)
res2: Int = 10
 
scala> var y = 1
y: Int = 1
 
scala> val sum = (x : Int) => x + y
sum: Int => Int = <function1>
 
scala> sum(5)
res3: Int = 6
 
scala>

  SCALA中的函数式编程