val x = if (a > b) a else b
def abs(x: Int) = if (x >= 0) x else -x
object Main extends App {
val s = "Hello"
for (i <- 0 to s.length - 1)
print(s(i)) //Hello
for (i <- 0 until s.length)
print(s(i)) //Hello
for (c <- s)
print(c) //Hello
for (i <- s.length - 1 to (0, -1))
print(s(i)) // olleH
}
在for循环中嵌入if;在for中使用临时变量:
object Main extends App {
for (i <- 1 to 3; j <- 1 to 3 if i != j)
printf("%d-%d\n", i, j)
for (i <- 1 to 3; from = 4 - i; j <- from to 3)
printf("%d-%d\n", i, j)
}
在for循环中嵌入多个if:
import scala.io.Source
import scala.collection.mutable.ArrayBuffer
object Main extends App {
/**
* Get the contents of a text file while skipping over comment lines and
* blank lines. This is useful when reading a data file that can have lines
* beginning with '#', or blank lines, such as a file that looks like this:
* -------------------
* foo
* # this is a comment
*
* bar
* -------------------
*/
def getFileContentsWithoutCommentLines(filename: String): List[String] = {
var lines = new ArrayBuffer[String]()
for ( line <- Source.fromFile(filename).getLines
if !line.trim.matches("")
if !line.trim.matches("#.*")) {
lines += line
}
lines.toList
}
val lines = getFileContentsWithoutCommentLines("test.dat")
lines.foreach(println)
}
如果for循环的循环体以yeild开始,则该循环会构造出一个集合,且生成的集合与它的第一个生成器是类型兼容的:
object Main extends App {
val s = for (c <- "Hello"; i <- 0 to 1) yield (c + i).toChar
println(s) // HIeflmlmop
val s2 = for (i <- 0 to 1; c <- "Hello") yield (c + i).toChar
println(s2) // Vector(H, e, l, l, o, I, f, m, m, p)
}
如果sum函数被调用时传入的是单个参数,那么该参数必须是单个整数,而不是一个整数区间。解决的办法是追加:_*,这样告诉编译器这个参数被当作参数序列处理。
object Main extends App {
def sum(args: Int*) = {
var res = 0
for (arg <- args) res += arg
res
}
sum(1, 2, 3, 4, 5)
sum(1 to 5) // error
sum(1 to 5: _*)
}
在递归时需要使用这样的技巧,注意递归函数必须标明返回值类型:
def recursiveSum(args: Int*): Int = {
if (args.length == 0) 0
else args.head + recursiveSum(args.tail: _*)
}