-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclosure_demo1.scala
More file actions
50 lines (40 loc) · 1.11 KB
/
closure_demo1.scala
File metadata and controls
50 lines (40 loc) · 1.11 KB
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
package ScalaBasic
// https://www.youtube.com/watch?v=6A8rV5bIVjo&list=PLmOn9nNkQxJEqCNXBu5ozT_26xwvUbHyE&index=171
object closure_demo1 extends App {
/**
* Closure
*
* 1) Closure is a combination of a func and its relative references
*/
// example 1
// the x and anonymous func now as a "closure"
def minusxy(x: Int) = {
(y: Int) => x - y // anonymous func
}
// example 2
// Make a func that will return <filename>.jpg if there is no jpg file type
// it there is (jpg file type), just return it. (e.g. : <filename2>.jpg )
/**
* Closure
* 1) returned an anonymous func
* 2) the anonymous func uses external var : suffix
* 3) the anonymous func + external var (suffix) as a Closure
*/
def makeSuffix(suffix: String) = {
// anonymous func
(name: String) => {
if (name.endsWith(suffix)) {
name
} else {
name + suffix
}
}
}
val f1 = makeSuffix(".jpg")
println( f1("hello"))
println("================")
println( f1("abc.jpg"))
println("================")
val r2 = makeSuffix(".jpg")("xyz")
println("r2 = " + r2)
}