SKIP THE SHIPPING
Use code NOSHIP during checkout to save 40% on eligible eBooks, now through January 5. Shop now.
Register your product to gain access to bonus material or receive a coupon.
This eBook includes the following formats, accessible from your Account page after purchase:
EPUB The open industry format known for its reflowable content and usability on supported mobile devices.
PDF The popular standard, used most often with the free Acrobat® 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.
This eBook includes the following formats, accessible from your Account page after purchase:
EPUB The open industry format known for its reflowable content and usability on supported mobile devices.
PDF The popular standard, used most often with the free Acrobat® 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.
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.
This indispensable tutorial offers a faster, easier 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.
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.
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 Other Future Transformations 253
16.7 Methods in the Future Object 255
16.8 Promises 257
16.9 Execution Contexts 258
Exercises 259
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.8 Variance 268
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