Table of Contents


I Introduction to Scala

12
1 Hands-on Scala14
2 Setting Up24
3 Basic Scala38
4 Scala Collections58
5 Notable Scala Features80

II Local Development

104
6 Implementing Algorithms in Scala106
7 Files and Subprocesses126
8 JSON and Binary Data Serialization146
9 Self-Contained Scala Scripts162
10 Static Build Pipelines178

III Web Services

200
11 Scraping Websites202
12 Working with HTTP APIs218
13 Fork-Join Parallelism with Futures236
14 Simple Web and API Servers258
15 Querying SQL Databases284

IV Program Design

304
16 Message-based Parallelism with Actors306
17 Multi-Process Applications326
18 Building a Real-time File Synchronizer344
19 Parsing Structured Text362
20 Implementing a Programming Language384

Conclusion

408

Part I Introduction to Scala


1 Hands-on Scala14
1.1 Why Scala?15
1.2 Why This Book?16
1.3 How This Book Is Organized17
1.4 Code Snippet and Examples19
1.5 Online Materials21
2 Setting Up24
2.1 Windows Setup (Optional)25
2.2 Installing Java25
2.3 Installing Ammonite26
2.4 Installing Mill30
2.5 IDE Support33
3 Basic Scala38
3.1 Values39
3.2 Loops, Conditionals, Comprehensions46
3.3 Methods and Functions50
3.4 Classes and Traits54
4 Scala Collections58
4.1 Operations59
4.2 Immutable Collections65
4.3 Mutable Collections71
4.4 Common Interfaces75
5 Notable Scala Features80
5.1 Case Classes and Sealed Traits81
5.2 Pattern Matching83
5.3 By-Name Parameters89
5.4 Implicit Parameters91
5.5 Typeclass Inference94

Part II Local Development


6 Implementing Algorithms in Scala106
6.1 Merge Sort107
6.2 Prefix Tries111
6.3 Breadth First Search117
6.4 Shortest Paths120
7 Files and Subprocesses126
7.1 Paths127
7.2 Filesystem Operations129
7.3 Folder Syncing133
7.4 Simple Subprocess Invocations137
7.5 Interactive and Streaming Subprocesses141
8 JSON and Binary Data Serialization146
8.1 Manipulating JSON147
8.2 JSON Serialization of Scala Data Types150
8.3 Writing your own Generic Serialization Methods154
8.4 Binary Serialization157
9 Self-Contained Scala Scripts162
9.1 Reading Files Off Disk163
9.2 Rendering HTML with Scalatags164
9.3 Rendering Markdown with Commonmark-Java166
9.4 Links and Bootstrap171
9.5 Optionally Deploying the Static Site174
10 Static Build Pipelines178
10.1 Mill Build Pipelines179
10.2 Mill Modules183
10.3 Revisiting our Static Site Script187
10.4 Conversion to a Mill Build Pipeline188
10.5 Extending our Static Site Pipeline192

Part III Web Services


11 Scraping Websites202
11.1 Scraping Wikipedia203
11.2 MDN Web Documentation207
11.3 Scraping MDN209
11.4 Putting it Together213
12 Working with HTTP APIs218
12.1 The Task: Github Issue Migrator219
12.2 Creating Issues and Comments221
12.3 Fetching Issues and Comments223
12.4 Migrating Issues and Comments228
13 Fork-Join Parallelism with Futures236
13.1 Parallel Computation using Futures237
13.2 N-Ways Parallelism240
13.3 Parallel Web Crawling243
13.4 Asynchronous Futures248
13.5 Asynchronous Web Crawling252
14 Simple Web and API Servers258
14.1 A Minimal Webserver259
14.2 Serving HTML263
14.3 Forms and Dynamic Data265
14.4 Dynamic Page Updates via API Requests272
14.5 Real-time Updates with Websockets276
15 Querying SQL Databases284
15.1 Setting up Quill and PostgreSQL285
15.2 Mapping Tables to Case Classes287
15.3 Querying and Updating Data290
15.4 Transactions295
15.5 A Database-Backed Chat Website297

Part IV Program Design


16 Message-based Parallelism with Actors306
16.1 Castor Actors307
16.2 Actor-based Background Uploads308
16.3 Concurrent Logging Pipelines314
16.4 Debugging Actors321
17 Multi-Process Applications326
17.1 Two-Process Build Setup327
17.2 Remote Procedure Calls330
17.3 The Agent Process332
17.4 The Sync Process334
17.5 Pipelined Syncing337
18 Building a Real-time File Synchronizer344
18.1 Watching for Changes345
18.2 Real-time Syncing with Actors346
18.3 Testing the Syncer353
18.4 Pipelined Real-time Syncing355
18.5 Testing the Pipelined Syncer358
19 Parsing Structured Text362
19.1 Simple Parsers363
19.2 Parsing Structured Values368
19.3 Implementing a Calculator372
19.4 Parser Debugging and Error Reporting378
20 Implementing a Programming Language384
20.1 Interpreting Jsonnet385
20.2 Jsonnet Language Features386
20.3 Parsing Jsonnet387
20.4 Evaluating the Syntax Tree396
20.5 Serializing to JSON402