Scala 入门

1 Collections classes

1.1 Populating lists

It’s highly recommended that you learn the basic Scala collections classes — List, ListBuffer, Vector, ArrayBuffer, Map, and Set — as soon as possible. A great benefit of the Scala collections classes is that they offer many powerful methods that you’ll want to start using as soon as possible to simplify your code.

val nums = List.range(0, 10)
// 包括 10
val nums = (1 to 10 ).toList
val nums = (1 to 10 by 2).toList
val letters = ('a' to 'f').toList
val letters = ('a' to 'f' by 2).toList

1.2 Sequence methods

While there are many sequential collections classes you can use — Array, ArrayBuffer, Vector, List, and more — let’s look at some examples of what you can do with the List class.

定义两个列表,随后进行相关操作

val nums = (1 to 10).toList
val names = List("joel", "ed", "chris", "maurice")

foreach method:

scala> names.foreach(println)
joel
ed
chris
maurice

filter method, followed by foreach:

scala> nums.filter(_ < 4).foreach(println)
1
2
3

map method:
It applies an algorithm you supply to every element in the collection, returning a new, transformed value for each element.

scala> val doubles = nums.map(_ * 2)
doubles: List[Int] = List(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

scala> val capNames = names.map(_.capitalize)
capNames: List[String] = List(Joel, Ed, Chris, Maurice)

scala> val lessThanFive = nums.map(_ < 5)
lessThanFive: List[Boolean] = List(true, true, true, true, false, false, false, false, false, false)

foldLeft:

scala> nums.foldLeft(0)(_ + _)
res0: Int = 55

scala> nums.foldLeft(1)(_ * _)
res1: Int = 3628800

2 Tuples

元祖中可以包含不同类型的对象

class Person(var name: String)
// 定义元祖
val t = (11, "Eleven", new Person("Eleven"))

access the tuple values by number:

t._1
t._2
t._3

assign the tuple fields to variables:

val (num, string, person) = (11, "Eleven", new Person("Eleven"))

3 Control structures

if/else

if (test1) {
    doA()
} else if (test2) {
    doB()
} else if (test3) {
    doC()
} else {
    doD()
}
val x = if (a < b) a else b

match expressions

def getClassAsString(x: Any):String = x match {
    case s: String => s + " is a String"
    case i: Int => "Int"
    case f: Float => "Float"
    case l: List[_] => "List"
    case p: Person => "Person"
    case _ => "Unknown"
}

try/catch

try {
    writeToFile(text)
} catch {
    case fnfe: FileNotFoundException => println(fnfe)
    case ioe: IOException => println(ioe)
}

for loops and expressions

for (arg <- args) println(arg)

// "x to y" syntax
for (i <- 0 to 5) println(i)

// "x to y by" syntax
for (i <- 0 to 10 by 2) println(i)

用 yield 生成可迭代对象

val x = for (i <- 1 to 5) yield i * 2

while and do/while

// while loop
while(condition) {
    statement(a)
    statement(b)
}

// do-while
do {
   statement(a)
   statement(b)
} 
while(condition)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值