Scala for the Impatient, 3rd Edition

Register your productto gain access to bonus material or receive a coupon.

Scala for the Impatient, 3rd Edition

Best Value Purchase

Book + eBook Bundle

  • Your Price: $53.99
  • List Price: $89.98
  • Includes EPUB and PDF
  • About eBook Formats
  • This eBook includes the following formats, accessible from yourAccountpage after purchase:

    ePubEPUBThe open industry format known for its reflowable content and usability on supported mobile devices.

    Adobe ReaderPDFThe popular standard, used most often with the freeAdobe® Reader®software.

    This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.

More Purchase Options


  • Your Price: $39.99
  • List Price: $49.99
  • Usually ships in 24 hours.

eBook (Watermarked)

  • Your Price: $31.99
  • List Price: $39.99
  • Includes EPUB and PDF
  • About eBook Formats
  • This eBook includes the following formats, accessible from yourAccountpage after purchase:

    ePubEPUBThe open industry format known for its reflowable content and usability on supported mobile devices.

    Adobe ReaderPDFThe popular standard, used most often with the freeAdobe® Reader®software.

    This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.


  • Copyright 2023
  • Dimensions: 7" x 9-1/8"
  • Pages: 400
  • Edition: 3rd
  • Book
  • ISBN-10: 0-13-803365-X
  • ISBN-13: 978-0-13-803365-1

Scala 3--A Clear, Concise Guide

Scala 3 is concise, consistent, flexible, robust, and efficient, but there's a lot to learn and navigating features and improvements can be challenging.Scala for the Impatient, Third Edition,is a complete yet concise guide that reflects the major enhancements of Scala 3, from improved syntax and revamped type system to powerful contextual abstractions.

这一不可或缺的指南提供了一个更快,据r pathway for learning today's Scala. Horstmann--author of the programming classic,Core Java--covers everything working developers need to know, focusing on hands-on solutions, not academic theory. Given the size and scope of Scala 3, there's plenty to cover but it's presented in small chunks organized for quick access and easy understanding, with plenty of practical insights and focused sample code.

  • Get started quickly with Scala 3 interpreter, syntax, tools, and current usage
  • Master core language features: functions, arrays, maps, tuples, packages, imports, exception handling, and more
  • Design and build better object-oriented code with Scala 3
  • Use Scala for real-world programming tasks: working with files, regular expressions, and processes
  • Work with higher-order functions and the powerful Scala collections library
  • Create concurrent programs with Scala futures
  • Understand the Scala type system, including revamped enums, intersection and union types, and enhanced type inference
  • Use contextual abstractions to easily extend class hierarchies, enrich existing classes, perform automatic conversions, and elegantly hide tedious details
  • Apply advanced "power tools" such as annotations and given values
  • Discover how to "program with types," analyzing and generating types at compile time
  • Get a taste of what's now possible with Scala macros

If you're a Java, Python, C++, or C# programmer who's new to Scala or functional programming--or even if you've already used earlier versions of Scala--this guide will help you write code that's more robust, more efficient, and more secure.

Register your book for convenient access to downloads, updates, and/or corrections as they become available. See inside book for details.


Author's Site

Please visit the author's site athorstmann.com/scala.

Sample Content

Online Sample Chapters

Higher-Order Functions

Maps, Options, and Tuples

Table of Contents

Foreword to the First Edition xix
Preface xxi
About the Author xxv

Chapter 1: The Basics 1
1.1 The Scala Interpreter 1
1.2 Declaring Values and Variables 4
1.3 Commonly Used Types 5
1.4 Arithmetic and Operator Overloading 7
1.5 More about Calling Methods 8
1.6 The apply Method 9
1.7 Scaladoc 11
Exercises 14

Chapter 2: Control Structures and Functions 17
2.1 Conditional Expressions 18
2.2 Statement Termination 20
2.3 Block Expressions and Assignments 21
2.4 Input and Output 22
2.5 Loops 23
2.6 More about the for Loop 25
2.7 Functions 27
2.8 Default and Named Arguments 29
2.9 Variable Arguments 30
2.10 The Main Function 31
2.11 Functions without Parameters 31
2.12 Lazy Values 32
2.13 Exceptions 33
Exercises 36

Chapter 3: Working with Arrays 39
3.1 Fixed-Length Arrays 39
3.2 Variable-Length Arrays: Array Buffers 40
3.3 Traversing Arrays and Array Buffers 41
3.4 Transforming Arrays 42
3.5 Common Algorithms 44
3.6 Deciphering Scaladoc 45
3.7 Multidimensional Arrays 46
3.8 Interoperating with Java 47
Exercises 48

Chapter 4: Maps, Options, and Tuples 51
4.1 Constructing a Map 52
4.2 Accessing Map Values 52
4.3 Updating Map Values 53
4.4 Iterating over Maps 54
4.5 Linked and Sorted Maps 54
4.6 Interoperating with Java 55
4.7 The Option Type 56
4.8 Tuples 57
4.9 Zipping 59
Exercises 59

Chapter 5: Classes 63
5.1 Simple Classes and Parameterless Methods 63
5.2 Properties with Getters and Setters 65
5.3 Properties with Only Getters 67
5.4 Private Fields 68
5.5 Auxiliary Constructors 69
5.6 The Primary Constructor 70
5.7 Nested Classes 73
Exercises 74

Chapter 6: Objects and Enumerations 77
6.1 Singletons 78
6.2 Companion Objects 78
6.3 Objects Extending a Class or Trait 79
6.4 The apply Method 80
6.5 Application Objects 81
6.6 Enumerations 81
Exercises 83

Chapter 7: Packages, Imports, and Exports 85
7.1 Packages 86
7.2 Package Scope Nesting 87
7.3 Chained Package Clauses 88
7.4 Top-of-File Notation 88
7.5 Package-Level Functions and Variables 89
7.6 Package Visibility 89
7.7 Imports 90
7.8 Imports Can Be Anywhere 91
7.9 Renaming and Hiding Members 91
7.10 Implicit Imports 92
7.11 Exports 92
Exercises 93

Chapter 8: Inheritance 97
8.1 Extending a Class 98
8.2 Overriding Methods 98
8.3 Type Checks and Casts 99
8.4 Superclass Construction 100
8.5 Anonymous Subclasses 101
8.6 Abstract Classes 101
8.7 Abstract Fields 102
8.8 Overriding Fields 102
8.9 Open and Sealed Classes 104
8.10 Protected Fields and Methods 105
8.11 Construction Order 105
8.12 The Scala Inheritance Hierarchy 106
8.13 Object Equality 109
8.14 Multiversal Equality 110
8.15 Value Classes 111
Exercises 112

Chapter 9: Files and Regular Expressions 117
9.1 Reading Lines 118
9.2 Reading Characters 118
9.3 Reading Tokens and Numbers 119
9.4 Reading from URLs and Other Sources 120
9.5 Writing Files 120
9.6 Visiting Directories 120
9.7 Serialization 121
9.8 Process Control 122
9.9 Regular Expressions 124
9.10 Regular Expression Groups 126
Exercises 126

Chapter 10: Traits 129
10.1 Why No Multiple Inheritance? 129
10.2 Traits as Interfaces 131
10.3 Traits with Concrete Methods 132
10.4 Traits for Rich Interfaces 133
10.5 Objects with Traits 133
10.6 Layered Traits 134
10.7 Overriding Abstract Methods in Traits 135
10.8 Concrete Fields in Traits 136
10.9 Abstract Fields in Traits 137
10.10 Trait Construction Order 138
10.11 Trait Constructors with Parameters 139
10.12 Traits Extending Classes 140
10.13 What Happens under the Hood 141
10.14 Transparent Traits 143
10.15 Self Types 143
Exercises 145

Chapter 11: Operators 149
11.1 Identifiers 150
11.2 Infix Operators 151
11.3 Unary Operators 152
11.4 Assignment Operators 153
11.5 Precedence 153
11.6 Associativity 154
11.7 The apply and update Methods 155
11.8 The unapply Method 155
11.9 The unapplySeq Method 157
11.10 Alternative Forms of the unapply and unapplySeq Methods 158
11.11 Dynamic Invocation 159
11.12 Typesafe Selection and Application 162
Exercises 163

Chapter 12: Higher-Order Functions 169
12.1 Functions as Values 170
12.2 Anonymous Functions 170
12.3 Parameters That Are Functions 171
12.4 Parameter Inference 172
12.5 Useful Higher-Order Functions 173
12.6 Closures 174
12.7 Interoperability with Lambda Expressions 175
12.8 Currying 176
12.9 Methods for Composing, Currying, and Tupling 177
12.10 Control Abstractions 178
12.11 Nonlocal Returns 179
Exercises 180

Chapter 13: Collections 183
13.1 The Main Collections Traits 184
13.2 Mutable and Immutable Collections 185
13.3 Sequences 187
13.4 Lists 188
13.5 Sets 189
13.6 Operators for Adding or Removing Elements 190
13.7 Common Methods 193
13.8 Mapping a Function 195
13.9 Reducing, Folding, and Scanning 197
13.10 Zipping 200
13.11 Iterators 201
13.12 Lazy Lists 202
13.13 Interoperability with Java Collections 204
Exercises 205

Chapter 14: Pattern Matching 209
14.1 A Better Switch 210
14.2 Guards 211
14.3 Variables in Patterns 211
14.4 Type Patterns 212
14.5 The Matchable Trait 213
14.6 Matching Arrays, Lists, and Tuples 214
14.7 Extractors 216
14.8 Patterns in Variable Declarations 217
14.9 Patterns in for Expressions 218
14.10 Case Classes 219
14.11 Matching Nested Structures 221
14.12 Sealed Classes 222
14.13 Parameterized Enumerations 223
14.14 Partial Functions 224
14.15 Infix Notation in case Clauses 225
Exercises 226

Chapter 15: Annotations 231
15.1 What Are Annotations? 232
15.2 Annotation Placement 232
15.3 Annotation Arguments 233
15.4 Annotations for Java Features 234
15.5 Annotations for Optimizations 236
15.6 Annotations for Errors and Warnings 238
15.7 Annotation Declarations 239
Exercises 241

Chapter 16: Futures 245
16.1 Running Tasks in the Future 246
16.2 Waiting for Results 248
16.3 The Try Class 249
16.4 Callbacks 250
16.5 Composing Future Tasks 251
16.6其他未来的特兰sformations 253
16.7 Methods in the Future Object 255
16.8 Promises 257
16.9 Execution Contexts 258

Chapter 17: Type Parameters 263
17.1 Generic Classes 264
17.2 Generic Functions 264
17.3 Bounds for Type Variables 265
17.4 Context Bounds 266
17.5 The ClassTag Context Bound 267
17.6 Multiple Bounds 267
17.7 Type Constraints 267
17.9 Co- and Contravariant Positions 270
17.10 Objects Can't Be Generic 271
17.11 Wildcards 272
17.12 Polymorphic Functions 273
Exercises 274

Chapter 18: Advanced Types 277
18.1 Union Types 277
18.2 Intersection Types 278
18.3 Type Aliases 279
18.4 Structural Types 281
18.5 Literal Types 283
18.6 The Singleton Type Operator 284
18.7 Abstract Types 286
18.8 Dependent Types 287
18.9 Abstract Type Bounds 289
Exercises 290

Chapter 19: Contextual Abstractions 295
19.1 Context Parameters 296
19.2 More about Context Parameters 297
19.3 Declaring Given Instances 299
19.4 Givens in for and match Expressions 301
19.5 Importing Givens 302
19.6 Extension Methods 303
19.7 Where Extension Methods Are Found 304
19.8 Implicit Conversions 306
19.9 Rules for Implicit Conversions 307
19.10 Importing Implicit Conversions 308
19.11 Context Functions 309
19.12 Evidence 311
19.13 The @implicitNotFound Annotation 312
Exercises 313

Chapter 20: Type-Level Programming 317
20.1 Match Types 318
20.2 Heterogeneous Lists 319
20.3 Literal Type Arithmetic 322
20.4 Inline Code 323
20.5 Type Classes 326
20.6 Mirrors 328
20.7 Type Class Derivation 330
20.8 Higher-Kinded Types 332
20.9 Type Lambdas 334
20.10 A Brief Introduction into Macros 335
Exercises 339

Index 343


Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

调查和问题,我们收集inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simplyemailinformation@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through ourContact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

这个网站使用cookie和类似的技术personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on theAccount page. If a user no longer desires our service and desires to delete his or her account, please contact us atcustomer-service@informit.comand we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive:www.e-skidka.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information toNevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read ourSupplemental privacy statement for California residentsin conjunction with this Privacy Notice. TheSupplemental privacy statement for California residentsexplains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Pleasecontact usabout this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020