Scala 編程, 3/e (Programming in Scala : Updated for Scala 2.12, 3/e)
內容描述
本書由直接參與Scala開發的一線人員編寫,因而對原理的解讀和應用的把握更加值得信賴。本書面向具有一定編程經驗的開發者,目標是讓讀者能夠全面瞭解和掌握Scala編程語言的核心特性,並能夠深入理解Scala這門語言在設計取捨背後的動因。即便是不直接使用Scala或從事一線編程工作的讀者,也能從本書學到大量關於函數式和麵向對象的編程概念和技巧。
目錄大綱
序................................................. .................................................. .....XXIV
致謝........................................... .................................................. ........XXVI
引言........................................ .................................................. ...........XXIX
第1章一門可伸縮的語言............................. .............................................1
1.1一門按需伸縮的語言.............................................. ....................... 2
1.2是什麼讓Scala能屈能伸?.................................................. ........... 7
1.3為什麼要用Scala?.................................................. .....................11
1.4 Scala尋根............................................... ...................................... 17
1.5結語......... .................................................. .................................. 19
第2章Scala入門.......... .................................................. ........................20
第1步使用Scala解釋器.................. .................................................. . 21
第2步定義變量........................................... ...................................... 22
第3步定義函數...... .................................................. ......................... 24
第4步編寫Scala腳本.................. .................................................. ..... 26
第5步用while做循環;用if做判斷................................ .................... 27
第6步用foreach和for遍歷.......................................... ....................... 30
結語......................... .................................................. ........................... 32
第3章Scala入門(續) .............. .................................................. ..........33
第7步用類型參數化數組............................... ................................... 33
第8步使用列表......... .................................................. ...................... 38
第9步使用元組..................... .................................................. .......... 42
第10步使用集和映射................................ ........................................... 43
第11步識別函數式編程風格................................................ ............. 48
第12步從文件讀取文本行......................................... ....................... 51
結語......................... .................................................. ........................... 55
第4章類和對象................ .................................................. ....................56
4.1類、字段和方法....................... .................................................. .. 56
4.2分號推斷........................................... ........................................... 61
4.3單例對象.. .................................................. .................................. 62
4.4 Scala應用程序........... .................................................. ................ 65
4.5 App特質............................................... ........................................ 68
4.6結語....... .................................................. .................................... 68
第5章基礎類型和操作...... .................................................. ....................69
5.1一些基礎類型......................... .................................................. ... 69
5.2字面量........................................... ............................................... 70
5.3字符串插值................................................ .................................. 77
5.4操作符即方法.......... .................................................. .................. 78
5.5算術操作............................................... ....................................... 81
5.6關係和邏輯操作..... .................................................. .................... 83
5.7位運算操作......................... .................................................. ....... 85
5.8對象相等性...................................... ............................................ 86
5.9操作符優先級和結合性............................................... ................ 88
5.10富包裝類............................. .................................................. ..... 91
5.11結語.......................................... .................................................. 91
第6章函數式對象............................................ .......................................93
6.1 Rational類的規格定義.... .................................................. ........... 93
6.2構建Rational ................................... ............................................. 94
6.3重新實現toString方法................................................. ................. 96
6.4檢查前置條件........................... .................................................. . 97
6.5添加字段............................................. ......................................... 98
6.6自引用..... .................................................. ..................................100
6.7輔助構造方法........... .................................................. ................101
6.8私有字段和方法............................................. .............................102
6.9定義操作符................ .................................................. ...............104
6.10 Scala中的標識符............................ ...........................................106
6.11方法重載.. .................................................. ...............................108
6.12隱式轉換.............. .................................................. ...................111
6.13注意事項........................... .................................................. ......112
6.14結語......................................... .................................................112
第7章內建的控制結構.......................................... ................................ 114
7.1 if表達式.............................................. ........................................115
7.2 while循環...... .................................................. ............................116
7.3 for表達式................. .................................................. .................119
7.4用try表達式實現異常處理........................ ..................................125
7.5 match表達式........... .................................................. ..................129
7.6沒有break和continue的日子........................ ................................131
7.7變量作用域............. .................................................. ..................134
7.8對指令式代碼進行重構....................... .......................................137
7.9結語................................................ ............................................139
第8章函數和閉包................................................ .................................140
8.1方法.............. .................................................. ............................140
8.2局部函數.................. .................................................. .................142
8.3一等函數............................ .................................................. .......144
8.4函數字面量的簡寫形式................................... ...........................147
8.5佔位符語法................. .................................................. ..............147
8.6部分應用的函數............................................. .............................149
8.7閉包................. .................................................. .........................152
8.8特殊的函數調用形式.................. ................................................156
8.9尾遞歸................................................ .........................................159
8.10結語...... .................................................. ..................................163
第9章控制抽象.......... .................................................. ........................164
9.1減少代碼重複..................... .................................................. ......164
9.2簡化調用方代碼...................................... ....................................169
9.3柯里化.............................................. ...........................................170
9.4編寫新的控制結構.................................................. ....................173
9.5傳名參數......................... .................................................. ..........176
9.6結語..................................... .................................................. .....178
第10章組合和繼承...................................... .........................................179
10.1一個二維的佈局類庫.................................................. ..............179
10.2抽像類................................ .................................................. .....180
10.3定義無參方法....................................... ....................................182
10.4擴展類............................................... ........................................184
10.5重寫方法和字段... .................................................. ...................186
10.6定義參數化字段......................... ...............................................188
10.7調用超類構造方法.............................................. ......................189
10.8使用override修飾符...................... ............................................190
10.9多態和動態綁定................................................ ........................192
10.10聲明final成員..................... .................................................. ...195
10.11使用組合和繼承......................................... .............................196
10.12實現above、beside和toString ........................................... .......198
10.13定義工廠對象...................................... ....................................201
10.14增高和增寬........ .................................................. ...................204
10.15放在一起.......................... .................................................. .....206
10.16結語.......................................... ..............................................207
第11章Scala的繼承關係.............................................. ........................208
11.1 Scala的類繼承關係................... ................................................208
11.2基本類型的實現機制............................................. ...................212
11.3底類型(bottom types) ........................................... ................215
11.4定義自己的值類型........................... .........................................216
11.5結語...... .................................................. ...................................219
第12章特質.......... .................................................. .............................220
12.1特質如何工作................ .................................................. .........220
12.2瘦接口和富接口.................................. ......................................224
12.3示例:矩形對象...... .................................................. ................225
12.4 Ordered特質.............................. ................................................227
12.5作為可疊加修改的特質........................................... ..................229
12.6為什麼不用多重繼承?.................................................. ...........234
12.7要特質還是不要特質?.................................................. ...........238
12.8結語.................................... .................................................. ....238
第13章包和引入....................................... ...........................................240
13.1將代碼放進包裡................................................. .......................240
13.2對相關代碼的精簡訪問................... ..........................................242
13.3引入..... .................................................. ...................................246
13.4隱式引入.............................................. .....................................249
13.5訪問修飾符........ .................................................. .....................250
13.6包對象......................... .................................................. ............256
13.7結語................................... .................................................. .....257
第14章斷言和測試...................................... .........................................258
14.1斷言...... .................................................. ..................................258
14.2用Scala寫測試.......... .................................................. ...............260
14.3翔實的失敗報告............................................. ...........................262
14.4作為規格說明的測試................ ................................................263
14.5基於性質的測試.............................................. ..........................267
14.6組織和運行測試.................. .................................................. ....268
14.7結語........................................... ...............................................270
第15章樣例類和模式匹配........................................... .........................271
15.1一個簡單的例子................... .................................................. ...271
15.2模式的種類.......................................... .....................................276
15.3模式守衛............................................... ....................................286
15.4模式重疊.......... .................................................. .......................287
15.5密封類....................... .................................................. ..............289
15.6 Option類型................................ ................................................291
15.7到處都是模式.............................................. .............................293
15.8一個複雜的例子............... .................................................. .......298
15.9結語........................................ .................................................. 306
第16章使用列表............................................. .....................................307
16.1 List字面量........ .................................................. .......................307
16.2 List類型....................... .................................................. ...........308
16.3構建列表................................... ................................................309
16.4列表的基本操作.............................................. ..........................309
16.5列表模式.................... .................................................. .............310
16.6 List類的初階方法............................. ........................................312
16.7 List類的高階方法.. .................................................. .................324
16.8 List對象的方法............................................. ............................333
16.9同時處理多個列表............... .................................................. ...335
16.10理解Scala的類型推斷算法....................................... ...............336
16.11結語................................ .................................................. .......339
第17章使用其他集合類...................................
.....................................340
17.1序列.......... .................................................. ..............................340
17.2集和映射............... .................................................. ..................344
17.3在可變和不可變集合類之間選擇................... ...........................352
17.4初始化集合............................................... ................................355
17.5元組.............. .................................................. ..........................358
17.6結語..................... .................................................. ...................361
第18章可變對象........................ .................................................. ........362
18.1什麼樣的對像是可變的?.................................................. .......362
18.2可被重新賦值的變量和屬性................................. ....................365
18.3案例分析:離散事件模擬...................... ...................................368
18.4用於描述數字電路的語言...... .................................................. .369
18.5 Simulation API ............................................... ...........................372
18.6電路模擬................... .................................................. ..............376
18.7結語................................. .................................................. .......384
第19章類型參數化.................................... ...........................................385
19.1函數式隊列.. .................................................. ...........................385
19.2信息隱藏................... .................................................. ..............389
19.3型變註解............................... .................................................. ..392
19.4檢查型變註解............................................. ..............................397
19.5下界................. .................................................. .......................400
19.6逆變....................... .................................................. .................402
19.7對象私有數據............................ ...............................................405
19.8上界................................................. .........................................407
19.9結語...... .................................................. ..................................410
第20章抽象成員.......... .................................................. ...................... 411
20.1抽象成員概覽.............................................. .............................411
20.2類型成員................. .................................................. ................412
20.3抽象的val ............................. .................................................. ...413
20.4抽象的var .......................................... ........................................414
20.5初始化抽象的val .... .................................................. .................415
20.6抽像類型............................. .................................................. ....423
20.7路徑依賴類型......................................... ..................................425
20.8改良類型............ .................................................. .....................428
20.9枚舉............................................... ...........................................429
20.10案例分析:貨幣. .................................................. ...................431
20.11結語............................
.................................................. ...........441
第21章隱式轉換和隱式參數............................ .....................................442
21.1隱式轉換........ .................................................. .........................442
21.2隱式規則.................... .................................................. .............445
21.3隱式轉換到一個預期的類型........................... ..........................448
21.4轉換接收端................... .................................................. ..........450
21.5隱式參數.............................................. .....................................454
21.6上下文界定......... .................................................. ....................460
21.7當有多個轉換可選時.................... ............................................463
21.8調試... .................................................. .....................................465
21.9結語.......... .................................................. ..............................467
第22章實現列表.............. .................................................. ..................468
22.1 List類的原理.......................... .................................................. .468
22.2 ListBuffer類............................................... ...............................475
22.3 List類的實踐............. .................................................. ..............476
22.4外部可見的函數式............................. .......................................479
22.5結語........ .................................................. ................................480
第23章重訪for表達式......... ................................................. ................481
23.1 for表達式............................. .................................................. ...483
23.2 N皇后問題.......................................... ......................................484
23.3用for推導式進行查詢.... .................................................. .........487
23.4用for推導式進行翻譯........................................... ....................489
23.5反過來.......................... .................................................. ...........494
23.6泛化for表達式................................ ...........................................495
23.7結語.... .................................................. ....................................497
第24章深入集合類....... .................................................. ......................498
24.1可變和不可變集合.................... ................................................499
24.2集合的一致性.............................................. .............................501
24.3 Traversable特質................. .................................................. .....503
24.4 Iterable特質............................................... ................................508
24.5序列型特質Seq、IndexedSeq和LinearSeq ........ ........................512
24.6集....................... .................................................. .....................517
24.7映射.......................... .................................................. ..............521
24.8具體的不可變集合類............................ ....................................525
24.9具體的可變集合類...... .................................................. ............532
24.10數組................................... .................................................. ...539
24.11字符串........................................... ..........................................544
24.12性能特徵............................................... ..................................545
24.13相等性............ .................................................. .......................547
24.14視圖........................ .................................................. ..............548
24.15迭代器................................ .................................................. ...554
24.16從頭創建集合.......................................... ................................561
24.17 Java和Scala集合互轉.......... .................................................. ..563
24.18結語............................................. ...........................................564
第25章Scala集合架構............................................ .............................565
25.1集合構建器................ .................................................. .............565
25.2抽取公共操作................................ ...........................................567
25.3集成新的集合. .................................................. ........................572
25.4結語....................... .................................................. .................588
第26章提取器........................... .................................................. ........589
26.1示例:提取電子郵箱地址.................................. .......................589
26.2提取器....................... .................................................. ..............591
26.3提取0或1個變量的模式......................................... ...................594
26.4提取可變長度參數的模式...................... ...................................596
26.5提取器和序列模式........ .................................................. ..........598
26.6提取器和样例類的對比.............................. ...............................599
26.7正則表達式.............. .................................................. ...............601
26.8結語................................ .................................................. ........604
第27章註解..................................... .................................................. ..606
27.1為什麼要有註解?.................................................. ..................606
27.2註解的語法.............................................. .................................607
27.3標準註解............. .................................................. ....................609
27.4結語........................... .................................................. .............613
第28章使用XML ............................... .................................................. .614
28.1半結構化數據........................................... ................................614
28.2 XML概覽.............. .................................................. ..................615
28.3 XML字面量........................... .................................................. .616
28.4序列化............................................... ........................................618
28.5拆解XML ..... .................................................. ...........................620
28.6反序列化.................. .................................................. ...............622
28.7加載和保存.............................. .................................................623
28.8對XML做模式匹配............................................ .......................624
28.9結語........................ .................................................. ................628
第29章用對象實現模塊化編程........................ ......................................629
29.1問題描述........ .................................................. .........................630
29.2食譜應用程序.............................................. .............................631
29.3抽象.................. .................................................. ......................634
29.4將模塊拆分成特質.................... ................................................638
29.5運行時鏈接............................................... ................................640
29.6跟踪模塊實例............. .................................................. ............642
29.7結語................................... .................................................. .....643
第30章對象相等性...................................... .........................................645
30.1 Scala中的相等性.. .................................................. ...................645
30.2編寫相等性方法............................................. ...........................646
30.3為參數化類型定義相等性.............. ...........................................660
30.4如何編寫equals和hashCode方法...............................................664
30.5結語................................................. .........................................668
第31章結合Scala和Java . .................................................. ..................669
31.1從Java使用Scala .......................... .............................................669
31.2註解.. .................................................. ......................................672
31.3通配類型....... .................................................. ..........................678
31.4同時編譯Scala和Java ............................................ ....................680
31.5基於Scala 2.12特性的Java 8集成.................... ..........................681
31.6結語..................... .................................................. ...................685
第32章Future和並發編程....................... ..............................................686
32.1天堂裡的煩惱................................................ ...........................687
32.2異步執行和Try ................. .................................................. .......688
32.3使用Future ....................................... .........................................690
32.4測試Future ..... .................................................. .........................705
32.5結語................................................ ..........................................708
第33章組合子解析. .................................................. ............................709
33.1示例:算術表達式............... .................................................. ...710
33.2運行你的解析器........................................ ................................712
33.3基本的正則表達式解析器......... ................................................713
33.4另一個示例:JSON ............................................. .....................714
33.5解析器輸出........................ .................................................. .....717
33.6實現組合子解析器...................................... ..............................723
33.7字符串字面量和正則表達式......................................... ............732
33.8詞法分析和解析................................ ........................................733
33.9錯誤報告...... .................................................. ...........................734
33.10回溯和LL(1) ............... .................................................. ...........736
33.11結語.................................... .................................................. ...737
第34章GUI編程......................................... ..........................................740
34.1第一個Swing應用程序.................................................. ............740
34.2面板和佈局................................. ..............................................743
34.3處理事件............................................... ....................................745
34.4示例:攝氏/華氏轉換器..... .................................................. ....748
34.5結語........................................... ...............................................750
第35章SCells試算表............................................. ..............................752
35.1可視化框架................ .................................................. .............752
35.2將數據錄入和顯示分開............................. ................................755
35.3公式............... .................................................. .........................758
35.4解析公式..................... .................................................. ............760
35.5求值............................................... ...........................................765
35.6操作類庫.. .................................................. ...............................768
35.7變更通知............... .................................................. ..................771
35.8結語............................. .................................................. ...........775
附錄A Unix和Windows環境的Scala腳本............................. ..................777
關於作者............................. .................................................. .................798
作者介紹
作者:(德)Martin Odersky(馬丁.奧德斯基)、(美)Lex Spoon(萊·斯彭),(美)Bill Venners(比爾·凡納斯) 譯者:高宇翔
Martin Odersky 是Scala 編程語言的締造者。他是瑞士洛桑理工學院(EPFL)的教授,同時也是Typesafe, Inc.(已更名為Lightbend)的創始人。他的研究方向是編程語言和系統,更具體地說,就是如何將面向對象和函數式編程風格有機地結合在一起。自2001 年起,他的主要精力集中在設計、實現和改進Scala 上。在此之前,他作為Java 泛型的合作設計者參與了Java 編程語言的開發,同時也是當前javac 參考實現的作者。他還是ACM 院士。Lex Spoon 是Semmle Ltd. 的一名軟件工程師。他在EPFL 作為博士後圍繞著Scala 開展了兩年的工作。他擁有Georgia Tech 的博士學位,在那裡他的主攻方向是動態編程語言的靜態分析。除Scala 外,他還幫助開發了各類編程語言,包括動態語言Smalltalk、科學計算語言X10,以及支撐Semmle 的邏輯編程語言。他和他的夫人一起生活在Atlanta,他們有兩隻貓和一隻吉娃娃。Bill Venners 是Artima Inc. 的主席,Artima 開發者網站(ww.artima.com)的發行人,以及Escalate Software, LLC 的聯合創始人。他著有《Inside theJava Virtual Machine》,這是一本面向程序員講解Java 平台架構和內部實現原理的書。他在《JavaWorld》雜誌上的專欄很受歡迎,主題涵蓋Java 內部實現、面向對象的設計和Jini。Bill 從Jini 社區創立之初便十分活躍,領導了Jini 社區的ServiceUI 項目,其API 成為Jini 服務事實上的UI 標準。他還是ScalaTest 的主要開發者和設計者,一個面向Scala 和Java 開發者的開源測試工具。
高宇翔,資深軟件開發工程師和系統架構師,同時也是Scala在國內的早期佈道者和實踐者,曾譯有《快學Scala(第2版)》、《快學Scala》(第1版)和《 Scala編程》(第1版)等專業書籍。