Home > Store

Scala for the Impatient, 2nd Edition

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

Scala for the Impatient, 2nd Edition


  • Your Price: $35.99
  • List Price: $44.99
  • Usually ships in 24 hours.



  • By Cay Horstmann, author of Core Java -- the book that has helped hundreds of thousands of programmers discover and master Java
  • Guides programmers through four levels of Scala mastery, from the absolute basics to expert-level
  • Includes 12 complete sample application projects


  • Copyright 2017
  • Dimensions: 7" x 9-1/8"
  • Pages: 384
  • Edition: 2nd
  • Book
  • ISBN-10: 0-13-454056-5
  • ISBN-13: 978-0-13-454056-6

Interest in the Scala programming language continues to grow for many reasons. Scala embraces the functional programming style without abandoning the object-oriented paradigm, and it allows you to write programs more concisely than in Java. Because Scala runs on the JVM, it can access any Java library and is interoperable with familiar Java frameworks. Scala also makes it easier to leverage the full power of concurrency.

Written for experienced Java, C++, or C# programmers who are new to Scala or functional programming, Scala for the Impatient, Second Edition, introduces the key Scala concepts and techniques you need in order to be productive quickly. It is the perfect introduction to the language, particularly for impatient readers who want to learn the fundamentals of Scala so they can start coding quickly. It doesn’t attempt to exhaustively list all the features of the language or make you suffer through long and contrived examples. Instead, carefully crafted examples and hands-on activities guide you through well-defined stages of competency, from basic to expert.

This revised edition has been thoroughly updated for Scala 2.12 and reflects current Scala usage. It includes added coverage of recent Scala features, including string interpolation, dynamic invocation, implicit classes, and futures. Scala is a big language, but you can use it effectively without knowing all of its details intimately. This title provides precisely the information that you need to get started in compact, easy-to-understand chunks.

  • Get started quickly with Scala’s interpreter, syntax, tools, and unique idioms
  • Master core language features: functions, arrays, maps, tuples, packages, imports, exception handling, and more
  • Become familiar with object-oriented programming in Scala: classes, inheritance, and traits
  • Use Scala for real-world programming tasks: working with files, regular expressions, and XML
  • Work with higher-order functions and the powerful Scala collections library
  • Leverage Scala’s powerful pattern matching and case classes
  • Create concurrent programs with Scala futures
  • Implement domain-specific languages
  • Understand the Scala type system
  • Apply advanced “power tools,” such as annotations, implicits, and type classes

Register your product at informit.com/register for convenient access to downloads, updates, and corrections as they become available.


Author's Site

Please visit the author's site at horstmann.com/scala>.

Sample Content

Online Sample Chapter

Classes in Scala

Sample Pages

Download the sample pages (includes Chapter 5 and Index)

Table of Contents

Foreword to the First Edition xvii

Preface xix

About the Author xxi

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 6

1.5 More about Calling Methods 8

1.6 The apply Method 9

1.7 Scaladoc 10

Exercises 15

Chapter 2: Control Structures and Functions 17

2.1 Conditional Expressions 18

2.2 Statement Termination 19

2.3 Block Expressions and Assignments 20

2.4 Input and Output 21

2.5 Loops 22

2.6 Advanced for Loops 24

2.7 Functions 25

2.8 Default and Named Arguments 26

2.9 Variable Arguments 26

2.10 Procedures 28

2.11 Lazy Values 28

2.12 Exceptions 29

Exercises 31

Chapter 3: Working with Arrays 35

3.1 Fixed-Length Arrays 35

3.2 Variable-Length Arrays: Array Buffers 36

3.3 Traversing Arrays and Array Buffers 37

3.4 Transforming Arrays 38

3.5 Common Algorithms 40

3.6 Deciphering Scaladoc 41

3.7 Multidimensional Arrays 42

3.8 Interoperating with Java 43

Exercises 44

Chapter 4: Maps and Tuples 47

4.1 Constructing a Map 48

4.2 Accessing Map Values 48

4.3 Updating Map Values 49

4.4 Iterating over Maps 50

4.5 Sorted Maps 50

4.6 Interoperating with Java 50

4.7 Tuples 51

4.8 Zipping 52

Exercises 52

Chapter 5: Classes 55

5.1 Simple Classes and Parameterless Methods 55

5.2 Properties with Getters and Setters 56

5.3 Properties with Only Getters 59

5.4 Object-Private Fields 60

5.5 Bean Properties 61

5.6 Auxiliary Constructors 62

5.7 The Primary Constructor 63

5.8 Nested Classes 66

Exercises 68

Chapter 6: Objects 71

6.1 Singletons 71

6.2 Companion Objects 72

6.3 Objects Extending a Class or Trait 73

6.4 The apply Method 73

6.5 Application Objects 74

6.6 Enumerations 75

Exercises 77

Chapter 7: Packages and Imports 79

7.1 Packages 80

7.2 Scope Rules 81

7.3 Chained Package Clauses 83

7.4 Top-of-File Notation 83

7.5 Package Objects 83

7.6 Package Visibility 84

7.7 Imports 85

7.8 Imports Can Be Anywhere 85

7.9 Renaming and Hiding Members 86

7.10 Implicit Imports 86

Exercises 87

Chapter 8: Inheritance 91

8.1 Extending a Class 91

8.2 Overriding Methods 92

8.3 Type Checks and Casts 93

8.4 Protected Fields and Methods 94

8.5 Superclass Construction 94

8.6 Overriding Fields 95

8.7 Anonymous Subclasses 97

8.8 Abstract Classes 97

8.9 Abstract Fields 97

8.10 Construction Order and Early Definitions 98

8.11 The Scala Inheritance Hierarchy 100

8.12 Object Equality 102

8.13 Value Classes 103

Exercises 105

Chapter 9: Files and Regular Expressions 109

9.1 Reading Lines 109

9.2 Reading Characters 110

9.3 Reading Tokens and Numbers 111

9.4 Reading from URLs and Other Sources 111

9.5 Reading Binary Files 112

9.6 Writing Text Files 112

9.7 Visiting Directories 112

9.8 Serialization 113

9.9 Process Control 114

9.10 Regular Expressions 116

9.11 Regular Expression Groups 117

Exercises 118

Chapter 10: Traits 121

10.1 Why No Multiple Inheritance? 121

10.2 Traits as Interfaces 123

10.3 Traits with Concrete Implementations 124

10.4 Objects with Traits 125

10.5 Layered Traits 125

10.6 Overriding Abstract Methods in Traits 127

10.7 Traits for Rich Interfaces 127

10.8 Concrete Fields in Traits 128

10.9 Abstract Fields in Traits 130

10.10 Trait Construction Order 130

10.11 Initializing Trait Fields 132

10.12 Traits Extending Classes 133

10.13 Self Types 134

10.14 What Happens under the Hood 135

Exercises 137

Chapter 11: Operators 141

11.1 Identifiers 142

11.2 Infix Operators 143

11.3 Unary Operators 143

11.4 Assignment Operators 144

11.5 Precedence 144

11.6 Associativity 145

11.7 The apply and update Methods 146

11.8 Extractors 147

11.9 Extractors with One or No Arguments 149

11.10 The unapplySeq Method 149

11.11 Dynamic Invocation 150

Exercises 153

Chapter 12: Higher-Order Functions 157

12.1 Functions as Values 157

12.2 Anonymous Functions 159

12.3 Functions with Function Parameters 160

12.4 Parameter Inference 160

12.5 Useful Higher-Order Functions 161

12.6 Closures 162

12.7 SAM Conversions 163

12.8 Currying 164

12.9 Control Abstractions 166

12.10 The return Expression 167

Exercises 168

Chapter 13: Collections 171

13.1 The Main Collections Traits 172

13.2 Mutable and Immutable Collections 173

13.3 Sequences 174

13.4 Lists 175

13.5 Sets 177

13.6 Operators for Adding or Removing Elements 178

13.7 Common Methods 180

13.8 Mapping a Function 182

13.9 Reducing, Folding, and Scanning 184

13.10 Zipping 187

13.11 Iterators 188

13.12 Streams 189

13.13 Lazy Views 190

13.14 Interoperability with Java Collections 191

13.15 Parallel Collections 193

Exercises 194

Chapter 14: Pattern Matching and Case Classes 197

14.1 A Better Switch 198

14.2 Guards 199

14.3 Variables in Patterns 199

14.4 Type Patterns 200

14.5 Matching Arrays, Lists, and Tuples 201

14.6 Extractors 202

14.7 Patterns in Variable Declarations 203

14.8 Patterns in for Expressions 204

14.9 Case Classes 205

14.10 The copy Method and Named Parameters 205

14.11 Infix Notation in case Clauses 206

14.12 Matching Nested Structures 207

14.13 Are Case Classes Evil? 208

14.14 Sealed Classes 209

14.15 Simulating Enumerations 209

14.16 The Option Type 210

14.17 Partial Functions 211

Exercises 212

Chapter 15: Annotations 215

15.1 What Are Annotations? 216

15.2 What Can Be Annotated? 216

15.3 Annotation Arguments 217

15.4 Annotation Implementations 218

15.5 Annotations for Java Features 219

15.6 Annotations for Optimizations 222

15.7 Annotations for Errors and Warnings 226

Exercises 227

Chapter 16: XML Processing 229

16.1 XML Literals 230

16.2 XML Nodes 230

16.3 Element Attributes 232

16.4 Embedded Expressions 233

16.5 Expressions in Attributes 234

16.6 Uncommon Node Types 235

16.7 XPath-like Expressions 235

16.8 Pattern Matching 237

16.9 Modifying Elements and Attributes 238

16.10 Transforming XML 239

16.11 Loading and Saving 239

16.12 Namespaces 242

Exercises 243

Chapter 17: Futures 247

17.1 Running Tasks in the Future 248

17.2 Waiting for Results 250

17.3 The Try Class 251

17.4 Callbacks 251

17.5 Composing Future Tasks 252

17.6 Other Future Transformations 255

17.7 Methods in the Future Object 256

17.8 Promises 258

17.9 Execution Contexts 260

Exercises 260

Chapter 18: Type Parameters 265

18.1 Generic Classes 266

18.2 Generic Functions 266

18.3 Bounds for Type Variables 266

18.4 View Bounds 268

18.5 Context Bounds 268

18.6 The ClassTag Context Bound 269

18.7 Multiple Bounds 269

18.8 Type Constraints 269

18.9 Variance 271

18.10 Co- and Contravariant Positions 272

18.11 Objects Can’t Be Generic 274

18.12 Wildcards 275

Exercises 275

Chapter 19: Advanced Types 279

19.1 Singleton Types 280

19.2 Type Projections 281

19.3 Paths 282

19.4 Type Aliases 283

19.5 Structural Types 283

19.6 Compound Types 284

19.7 Infix Types 285

19.8 Existential Types 286

19.9 The Scala Type System 287

19.10 Self Types 288

19.11 Dependency Injection 289

19.12 Abstract Types 291

19.13 Family Polymorphism 293

19.14 Higher-Kinded Types 296

Exercises 299

Chapter 20: Parsing 303

20.1 Grammars 304

20.2 Combining Parser Operations 305

20.3 Transforming Parser Results 307

20.4 Discarding Tokens 308

20.5 Generating Parse Trees 309

20.6 Avoiding Left Recursion 310

20.7 More Combinators 311

20.8 Avoiding Backtracking 314

20.9 Packrat Parsers 314

20.10 What Exactly Are Parsers? 315

20.11 Regex Parsers 316

20.12 Token-Based Parsers 317

20.13 Error Handling 319

Exercises 320

Chapter 21: Implicits 323

21.1 Implicit Conversions 324

21.2 Using Implicits for Enriching Existing Classes 324

21.3 Importing Implicits 325

21.4 Rules for Implicit Conversions 326

21.5 Implicit Parameters 328

21.6 Implicit Conversions with Implicit Parameters 329

21.7 Context Bounds 329

21.8 Type Classes 331

21.9 Evidence 333

21.10 The @implicitNotFound Annotation 334

21.11 CanBuildFrom Demystified 334

Exercises 336

Index 338


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

For inquiries and questions, we collect the 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, simply email information@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 our Contact 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

This site uses cookies and similar technologies to 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 the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and 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.informit.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 to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains 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

Please contact us about 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