-
Notifications
You must be signed in to change notification settings - Fork 0
/
controlStructs.scala
47 lines (29 loc) · 1005 Bytes
/
controlStructs.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
val filesHere = (new java.io.File(".")).listFiles
filesHere.filter(_.getName().endsWith(".scala"))
.foreach(println)
for (i <- 1 to 5) println("Iteration" + i)
for (i <- 1 until 5) println("Until5" + i)
for (i <- (1 to 5).filter(_ % 2 == 0))
println("Even" + i)
for (
file <- filesHere
if !file.getName().startsWith("h0");
if file.getName().endsWith(".scala")
) println(file)
def fileLines(file: java.io.File) =
scala.io.Source.fromFile(file).getLines().toList
for (line <- fileLines(new java.io.File("./Rational.scala")))
println(line)
def grep(pattern: String) =
for (
file <- filesHere
if file.getName.endsWith(".scala");
line <- fileLines(file)
if line.trim.matches(pattern)
) println(file + ":" + line.trim)
def scalaFiles = filesHere.filter(_.getName.endsWith(".scala"))
grep(".*gcd.*")
val totalLines = scalaFiles.map(fileLines _).map(_.length).sum
println(totalLines)
val totalFlatMapLines = scalaFiles.flatMap(fileLines _).length
println(totalFlatMapLines)