目录


前言

9

I Scala导论

13
1 上手Scala15
2 搭建环境25
3 Scala基础39
4 Scala集合61
5 Scala特性81

II Local Development

105
6 Implementing Algorithms in Scala107
7 Files and Subprocesses116
8 JSON and Binary Data Serialization124
9 Self-Contained Scala Scripts133
10 Static Build Pipelines142

III Web Services

150
11 Scraping Websites152
12 Working with HTTP APIs161
13 Fork-Join Parallelism with Futures169
14 Simple Web and API Servers178
15 Querying SQL Databases186

IV Program Design

194
16 Message-based Parallelism with Actors196
17 Multi-Process Applications204
18 Building a Real-time File Synchronizer213
19 Parsing Structured Text225
20 Implementing a Programming Language233

Conclusion

241

Part I Scala导论


1 上手Scala15
1.1 为什么选Scala?16
1.2 为什么选择本书?17
1.3 本书如何组织18
1.4 代码片段和样例19
1.5 在线资料21
2 搭建环境25
2.1 Windows搭建(可选)26
2.2 安装Java26
2.3 安装Ammonite27
2.4 安装Mill30
2.5 IDE支持33
3 Scala基础39
3.1 数据结构40
3.2 循环,条件,Comprehensions46
3.3 方法与函数51
3.4 类和Trait54
4 Scala集合61
4.1 通用操作62
4.2 不可变集合68
4.3 可变集合73
4.4 公共接口77
5 Scala特性81
5.1 Case Class和Sealed Trait82
5.2 模式匹配84
5.3 传名参数89
5.4 隐式参数92
5.5 Typeclass推断94

Part II Local Development


6 Implementing Algorithms in Scala107
6.1 Merge Sort108
6.2 Prefix Tries109
6.3 Breadth First Search110
6.4 Shortest Paths111
6.5 Conclusion111
7 Files and Subprocesses116
7.1 Paths117
7.2 Filesystem Operations118
7.3 Folder Syncing119
7.4 Simple Subprocess Invocations120
7.5 Interactive and Streaming Subprocesses120
7.6 Conclusion121
8 JSON and Binary Data Serialization124
8.1 Manipulating JSON125
8.2 JSON Serialization of Scala Data Types126
8.3 Writing your own Generic Serialization Methods127
8.4 Binary Serialization128
8.5 Conclusion128
9 Self-Contained Scala Scripts133
9.1 Reading Files Off Disk134
9.2 Rendering HTML with Scalatags134
9.3 Rendering Markdown with Commonmark-Java135
9.4 Links and Bootstrap136
9.5 Optionally Deploying the Static Site136
9.6 Conclusion138
10 Static Build Pipelines142
10.1 Mill Build Pipelines143
10.2 Mill Modules144
10.3 Revisiting our Static Site Script145
10.4 Conversion to a Mill Build Pipeline146
10.5 Extending our Static Site Pipeline147
10.6 Conclusion148

Part III Web Services


11 Scraping Websites152
11.1 Scraping Wikipedia153
11.2 MDN Web Documentation154
11.3 Scraping MDN155
11.4 Putting it Together156
11.5 Conclusion156
12 Working with HTTP APIs161
12.1 The Task: Github Issue Migrator162
12.2 Creating Issues and Comments163
12.3 Fetching Issues and Comments164
12.4 Migrating Issues and Comments165
12.5 Conclusion166
13 Fork-Join Parallelism with Futures169
13.1 Parallel Computation using Futures170
13.2 N-Ways Parallelism171
13.3 Parallel Web Crawling172
13.4 Asynchronous Futures173
13.5 Asynchronous Web Crawling174
13.6 Conclusion175
14 Simple Web and API Servers178
14.1 A Minimal Webserver179
14.2 Serving HTML180
14.3 Forms and Dynamic Data181
14.4 Dynamic Page Updates via API Requests182
14.5 Real-time Updates with Websocks183
14.6 Conclusion184
15 Querying SQL Databases186
15.1 Setting up Quill and PostgreSQL187
15.2 Mapping Tables to Case Classes188
15.3 Querying and Updating Data189
15.4 Transactions190
15.5 A Database-Backed Chat Website191
15.6 Conclusion192

Part IV Program Design


16 Message-based Parallelism with Actors196
16.1 Castor Actors197
16.2 Actor-based Background Uploads198
16.3 Concurrent Logging Pipelines199
16.4 Debugging Actors200
16.5 Conclusion200
17 Multi-Process Applications204
17.1 Two-Process Build Setup205
17.2 Remote Procedure Calls206
17.3 The Agent Process207
17.4 The Sync Process208
17.5 Pipelined Syncing208
17.6 Conclusion210
18 Building a Real-time File Synchronizer213
18.1 Watching for Changes214
18.2 Real-time Syncing with Actors215
18.3 Testing the Syncer216
18.4 Pipelined Real-time Syncing217
18.5 Testing the Pipelined Syncer218
18.6 Conclusion220
19 Parsing Structured Text225
19.1 Simple Parsers226
19.2 Parsing Stuctured Values227
19.3 Implementing a Calculator228
19.4 Parser Debugging and Error Reporting229
19.5 Conclusion230
20 Implementing a Programming Language233
20.1 Interpreting Jsonnet234
20.2 Jsonnet Language Features235
20.3 Parsing Jsonnet236
20.4 Evaluating the Syntax Tree237
20.5 Serializing to JSON237
20.6 Conclusion239

Hands-on Scala Programming


AuthorLi Haoyi
Published1 June 2020
Websitewww.handsonscala.com
ISBN978-981-14-5693-0

Hands-on Scala Programming Copyright (c) 2020 Li Haoyi (haoyi.sg@gmail.com)

First Edition, published June 1 2020

No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the Author.

ISBN 978-981-14-5693-0

Written and Published by Li Haoyi

Book Website: https://www.handsonscala.com/

Chapter Discussion: https://www.handsonscala.com/discuss

Live Chat: https://www.handsonscala.com/chat

Online materials: https://github.com/handsonscala/handsonscala

For inquiries on distribution, translations, or bulk sales, please contact the author directly at haoyi.sg@gmail.com.

The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, the Author shall not have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.

Reviewers

Thanks to all the reviewers who helped review portions of this book and provide the feedback that helped refine this book and make it what it is today.

In alphabetical order:

Alex Allain, Alwyn Tan, Bryan Jadot, Chan Ying Hao, Choo Yang, Dean Wampler, Dimitar Simeonov, Eric Marion, Grace Tang, Guido Van Rossum, Jez Ng, Karan Malik, Liang Yuan Ruo, Mao Ting, Martin MacKerel, Martin Odersky, Michael Wu, Olafur Pall Geirsson, Ong Ming Yang, Pathikrit Bhowmick