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 Servers265
15 Querying SQL Databases289

IV Program Design

311
16 Message-based Parallelism with Actors313
17 Multi-Process Applications333
18 Building a Real-time File Synchronizer349
19 Parsing Structured Text365
20 Implementing a Programming Language387

Conclusion

411

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 Parallelism246
13.3 Parallel Web Crawling248
13.4 Asynchronous Futures253
13.5 Asynchronous Web Crawling258
14 Simple Web and API Servers265
14.1 A Minimal Webserver266
14.2 Serving HTML269
14.3 Forms and Dynamic Data271
14.4 Dynamic Page Updates via API Requests279
14.5 Real-time Updates with Websockets283
15 Querying SQL Databases289
15.1 Setting up ScalaSql and PostgreSQL290
15.2 Mapping Tables to Case Classes292
15.3 Querying and Updating Data296
15.4 Transactions302
15.5 A Database-Backed Chat Website303

Part IV Program Design


16 Message-based Parallelism with Actors313
16.1 Castor Actors314
16.2 Actor-based Background Uploads316
16.3 Concurrent Logging Pipelines322
16.4 Debugging Actors328
17 Multi-Process Applications333
17.1 Two-Process Build Setup334
17.2 Remote Procedure Calls336
17.3 The Agent Process338
17.4 The Sync Process340
17.5 Pipelined Syncing342
18 Building a Real-time File Synchronizer349
18.1 Watching for Changes350
18.2 Real-time Syncing with Actors351
18.3 Testing the Syncer357
18.4 Pipelined Real-time Syncing359
18.5 Testing the Pipelined Syncer361
19 Parsing Structured Text365
19.1 Simple Parsers366
19.2 Parsing Structured Values371
19.3 Implementing a Calculator376
19.4 Parser Debugging and Error Reporting381
20 Implementing a Programming Language387
20.1 Interpreting Jsonnet388
20.2 Jsonnet Language Features388
20.3 Parsing Jsonnet390
20.4 Evaluating the Syntax Tree400
20.5 Serializing to JSON405