Table of Contents


Foreword

9

I Introduction to Scala

13
1 Hands-on Scala15
2 Setting Up25
3 Basic Scala39
4 Scala Collections63
5 Notable Scala Features83

II Local Development

109
6 Implementing Algorithms in Scala111
7 Files and Subprocesses131
8 JSON and Binary Data Serialization151
9 Self-Contained Scala Scripts167
10 Static Build Pipelines183

III Web Services

205
11 Scraping Websites207
12 Working with HTTP APIs223
13 Fork-Join Parallelism with Futures241
14 Simple Web and API Servers263
15 Querying SQL Databases287

IV Program Design

309
16 Message-based Parallelism with Actors311
17 Multi-Process Applications331
18 Building a Real-time File Synchronizer347
19 Parsing Structured Text363
20 Implementing a Programming Language385

Conclusion

409

Part I Introduction to Scala


1 Hands-on Scala15
1.1 Why Scala and Why This Book?16
1.2 How This Book Is Organized17
1.3 Code Snippet and Examples19
1.4 Online Materials21
2 Setting Up25
2.1 Installing Mill26
2.2 IDE Support34
3 Basic Scala39
3.1 Values40
3.2 Loops, Conditionals, Comprehensions47
3.3 Methods and Functions50
3.4 Classes54
3.5 Traits56
3.6 Singleton Objects57
3.7 Optional Braces and Indentation59
4 Scala Collections63
4.1 Operations64
4.2 Immutable Collections69
4.3 Mutable Collections75
4.4 Common Interfaces79
5 Notable Scala Features83
5.1 Case Classes, Sealed Traits, and Enums84
5.2 Pattern Matching88
5.3 By-Name Parameters94
5.4 Apply Methods96
5.5 Implicit Parameters97
5.6 Typeclass Inference99

Part II Local Development


6 Implementing Algorithms in Scala111
6.1 Merge Sort112
6.2 Prefix Tries115
6.3 Breadth First Search123
6.4 Shortest Paths125
7 Files and Subprocesses131
7.1 Paths132
7.2 Filesystem Operations134
7.3 Folder Syncing138
7.4 Simple Subprocess Invocations142
7.5 Interactive and Streaming Subprocesses146
8 JSON and Binary Data Serialization151
8.1 Manipulating JSON152
8.2 JSON Serialization of Scala Data Types155
8.3 Writing your own Generic Serialization Methods159
8.4 Binary Serialization162
9 Self-Contained Scala Scripts167
9.1 Reading Files Off Disk168
9.2 Rendering HTML with Scalatags169
9.3 Rendering Markdown with Commonmark-Java171
9.4 Links and Bootstrap175
9.5 Optionally Deploying the Static Site179
10 Static Build Pipelines183
10.1 Mill Build Pipelines184
10.2 Mill Modules188
10.3 Revisiting our Static Site Script192
10.4 Conversion to a Mill Build Pipeline192
10.5 Extending our Static Site Pipeline196

Part III Web Services


11 Scraping Websites207
11.1 Scraping Wikipedia208
11.2 MDN Web Documentation212
11.3 Scraping MDN214
11.4 Putting it Together218
12 Working with HTTP APIs223
12.1 The Task: Github Issue Migrator224
12.2 Creating Issues and Comments226
12.3 Fetching Issues and Comments228
12.4 Migrating Issues and Comments233
13 Fork-Join Parallelism with Futures241
13.1 Parallel Computation using Futures242
13.2 N-Ways Parallelism245
13.3 Parallel Web Crawling248
13.4 Asynchronous Futures253
13.5 Asynchronous Web Crawling257
14 Simple Web and API Servers263
14.1 A Minimal Webserver264
14.2 Serving HTML267
14.3 Forms and Dynamic Data269
14.4 Dynamic Page Updates via API Requests277
14.5 Real-time Updates with Websockets281
15 Querying SQL Databases287
15.1 Setting up ScalaSql and PostgreSQL288
15.2 Mapping Tables to Case Classes290
15.3 Querying and Updating Data294
15.4 Transactions300
15.5 A Database-Backed Chat Website301

Part IV Program Design


16 Message-based Parallelism with Actors311
16.1 Castor Actors312
16.2 Actor-based Background Uploads314
16.3 Concurrent Logging Pipelines320
16.4 Debugging Actors326
17 Multi-Process Applications331
17.1 Two-Process Build Setup332
17.2 Remote Procedure Calls334
17.3 The Agent Process336
17.4 The Sync Process338
17.5 Pipelined Syncing340
18 Building a Real-time File Synchronizer347
18.1 Watching for Changes348
18.2 Real-time Syncing with Actors349
18.3 Testing the Syncer355
18.4 Pipelined Real-time Syncing357
18.5 Testing the Pipelined Syncer359
19 Parsing Structured Text363
19.1 Simple Parsers364
19.2 Parsing Structured Values369
19.3 Implementing a Calculator374
19.4 Parser Debugging and Error Reporting379
20 Implementing a Programming Language385
20.1 Interpreting Jsonnet386
20.2 Jsonnet Language Features386
20.3 Parsing Jsonnet388
20.4 Evaluating the Syntax Tree398
20.5 Serializing to JSON403