Scala入門筆記二

2022-05-22 15:21:16 字數 4777 閱讀 6322

目錄使用反引號定義識別符號

無引數方法

優先順序規則

if語句

迴圈生成器表示式

保護式: 篩選元素

新增多個保護式

yielding

擴充套件作用域與值定義

while迴圈

do...while迴圈

條件操作符

使用try...catch...final子句

惰性賦值

列舉可插入字串

不能使用保留字.

val xyz_++= = 1
可以通過反引號定義識別符號,兩個反引號內的任意長度的字串便是定義的識別符號.

def `test that addition work`= assert(1 + 1 = 2)
list(1, 2, 3, 4).filter((i: int) => iseven(i)).foreach((i: int) => println(i))

list(1, 2, 3, 4).filter(i => iseven(i)).foreach(i => println(i))

list(1, 2, 3, 4).filter(iseven).foreach(println)

list(1, 2, 3, 4) filter iseven foreach println

1. 所有字母

2. |

3. ^

4. &

5. < >

6. = !

7. :

8. + -

9. * / %

10. 其他特殊字元

scala> val list = list('c', 'd')

list: list[char] = list(c, d)

scala> 'a' :: list

res1: list[char] = list(a, c, d)

scala> val list = list('c', 'd')

list: list[char] = list(c, d)

scala> 'b' :: list

res2: list[char] = list(b, c, d)

scala> list.::('a')

res3: list[char] = list(a, c, d)

scala> 'a' :: list

res4: list[char] = list(a, c, d)

scala> 'b' :: list

res5: list[char] = list(b, c, d)

注意:

scala不支援三元表示式

示例**

val list = list("a", "b", "c")

for (item <- list)

println(item)

輸出
a

bc

示例

**

val list = list("abc", "cde", "efg")

for (item <- list

if item.contains("c")

)println(item)

輸出
abc

cde

新增多個保護式

示例

val list = list("abc", "bcd", "cde")

for (item <- list

if item.contains("b")

if !item.startswith("a")

)println(item)

for (item <- list

if item.contains("b") && !item.startswith("a")

)println(item)

輸出
bcd

bcd

示例

**

val list = list("abc", "bcd", "cde")

val filteritems = for yield item

filteritems

執行
scala> val list = list("abc", "bcd", "cde")

list: list[string] = list(abc, bcd, cde)

scala> val filteritems = for yield item

filteritems: list[string] = list(bcd)

示例

**

val list = list("abc", "bcd", "cde")

val upcaseitems = for yield upcaseitem

執行
scala> val list = list("abc", "bcd", "cde")

list: list[string] = list(abc, bcd, cde)

scala>

| val upcaseitems = for yield upcaseitem

upcaseitems: list[string] = list(abc, bcd, cde)

while (表示式)
var count = 0

do while(count < 10)

操作符

操作&&

和操作||

或操作》

大於》=

大於或等於

<

小於<=

小於或等於

==等於

!=不等於

try  catch  finally

object expensiveresource 

}

// file: enum.scala

object breed extends enumeration

import breed._

println("id\tbreed")

for (breed <- breed.values) println(s"$\t$breed")

println("\njust terriers:")

breed.values filter (_.tostring.endswith("terrier")) foreach println

def isterrier(b: breed) = b.tostring.endswith("terrier")

println("\njust terriers:")

breed.values filter isterrier foreach println

scala enum.scala

id      breed

0 doberman pinscher

1 yorkshire terrier

2 scottish terrier

3 great dane

4 portuguese water dog

just terriers:

yorkshire terrier

scottish terrier

just terriers:

yorkshire terrier

scottish terrier

下面是乙個簡單示例:

val name = "buck trends"

println(s"hello, $name")

下面是幾個對可插入字串進行格式化的示例.

scala> val gross = 100000f

gross: float = 100000.0

scala> val net = 64000f

net: float = 64000.0

scala> val percent = (net / gross) * 100

percent: float = 64.0

scala> println(f"$$$%.2f vs. $$$%.2f or $%.1f%%")

$100000.00 vs. $64000.00 or 64.0%

scala> val s = "%02d: name = %s".format(5, "dean wampler")

s: string = 05: name = dean wampler

// 原生插入器

scala> val name = "dean wampler"

name: string = dean wampler

scala> s"123\n$name\n456"

res1: string =

123dean wampler

456scala> raw"123\n$name\n456"

res2: string = 123\ndean wampler\n456

Scala入門系列(二) Scala函式基礎介紹

2 匿名函式 3 高階函式 4 函式巢狀 5 柯里化 6 隱式引數 7 命名引數 8 引數預設值 函式是scala的核心 def 函式名 引數列表 返回值 列1 def square x int int 列2 def sayhello x string unit 函式呼叫 square 10 say...

Scala 語法入門

for i 1 to 3 j 1 to 3 println i j for i 1 to 3 j 1 to 3 if i j print f 3d 引數後面跟型別 def sum a int,b int int var sumfun a int,b int a b 預設值 def sum a int...

scala入門練習

1 輸入兩個數,按從小到大排序後輸出 2 輸入三個數 互不相同 並將三個公升序輸出 公升序 從小到大 3 輸入乙個年份,判斷是否是閏年 能被400整除 能被4整除,不能被100整除 4.輸入乙個時間,輸出它的下一秒時間。20 39 40 20 39 41 20 39 59 20 40 00 20 5...