-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.scala
123 lines (80 loc) · 2.46 KB
/
functions.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import scala.io.Source
object LongLines {
def getFromFile(fname: String,
width: Int) {
val source = Source.fromFile(fname)
for (line <- source.getLines)
processLine(line, width)
}
private def processLine(line: String,
width: Int) {
if (line.length > width)
println(line.length.toString +
":" + line.trim)
}
}
LongLines.getFromFile("Rational.scala", 40)
def processFile(filename: String, width: Int) {
def processLine(line: String) {
if (line.length > width)
println(line.length.toString +
":" + line.trim)
}
val source = Source.fromFile(filename)
for (line <- source.getLines) {
processLine(line)
}
}
processFile("Rational.scala", 40)
def prefixWithLengthAndTrim(x: String): String =
x.length.toString + "*:*" + x.trim
def onlyLongLines(stringSeq: Iterator[String],
minWidth: Int): Iterator[String] =
stringSeq.filter(_.length >= minWidth)
def processFile2(filename: String, width: Int) {
val source = Source.fromFile(filename)
val lines = source.getLines
val onlyLong = onlyLongLines(lines, width)
val lengthPrefixed = onlyLong.map(prefixWithLengthAndTrim _)
lengthPrefixed.foreach(println)
}
processFile2("Rational.scala", 40)
def processFile3(filename: String, width: Int) {
val source = Source.fromFile(filename)
val lines = source.getLines
val onlyLong = lines.filter(_.length >= width)
val lengthPrefixed = onlyLong.map(
(x:String) => x.length + " => " + x.trim)
lengthPrefixed.foreach(println)
}
processFile3("Rational.scala", 40)
// Partially applied functions:
def sum(a:Int, b:Int, c:Int): Int = a + b + c
assert(sum(1,2,3) == 6)
val addPlus4 = sum(4, _:Int, _:Int)
assert(addPlus4(2,3) == 9)
// scala captures variables as variables in closures, not values
// this is like javascript
var more:Int = 10
def addMore(x:Int): Int = x + more
assert(addMore(3) == 13)
more = 20
assert(addMore(3) == 23)
// but if passed in as an argument to a functional:
def makeIncreaser(amount: Int) = (x: Int) => x + amount
val inc10 = makeIncreaser(10)
assert(inc10(3) == 13)
val incrByMore = makeIncreaser(more)
assert(incrByMore(3) == 23)
more = 30
assert(incrByMore(3) == 23)
// Let's see what happens if we have lazy
// evaluation of the amount
def lazyIncreaser(amount: => Int) = (x: Int) => x + amount
val lzyIncr = lazyIncreaser(20)
assert(lzyIncr(3) == 23)
val lzyIncrByMore = lazyIncreaser(more)
assert(lzyIncrByMore(3) == 33)
more = 40
// seems to reproduce the closure behavior
assert(lzyIncrByMore(3) == 43)