Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

scala cookbook
Nội dung xem thử
Mô tả chi tiết
www.it-ebooks.info
www.it-ebooks.info
Alvin Alexander
Scala Cookbook
www.it-ebooks.info
Scala Cookbook
by Alvin Alexander
Copyright © 2013 Alvin Alexander. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or [email protected].
Editor: Courtney Nash
Production Editor: Rachel Steely
Copyeditor: Kim Cofer
Proofreader: Linley Dolby
Indexer: Ellen Troutman
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Rebecca Demarest
August 2013: First Edition
Revision History for the First Edition:
2013-07-30: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781449339616 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc. Scala Cookbook, the image of a long-beaked echidna, and related trade dress are trademarks of
O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐
mark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume no
responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-33961-6
[LSI]
www.it-ebooks.info
For my mom, who loves cookbooks.
www.it-ebooks.info
www.it-ebooks.info
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
1. Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1. Testing String Equality 4
1.2. Creating Multiline Strings 6
1.3. Splitting Strings 8
1.4. Substituting Variables into Strings 9
1.5. Processing a String One Character at a Time 13
1.6. Finding Patterns in Strings 18
1.7. Replacing Patterns in Strings 21
1.8. Extracting Parts of a String That Match Patterns 22
1.9. Accessing a Character in a String 24
1.10. Add Your Own Methods to the String Class 25
2. Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.1. Parsing a Number from a String 32
2.2. Converting Between Numeric Types (Casting) 36
2.3. Overriding the Default Numeric Type 37
2.4. Replacements for ++ and −− 39
2.5. Comparing Floating-Point Numbers 41
2.6. Handling Very Large Numbers 43
2.7. Generating Random Numbers 45
2.8. Creating a Range, List, or Array of Numbers 47
2.9. Formatting Numbers and Currency 49
3. Control Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.1. Looping with for and foreach 54
3.2. Using for Loops with Multiple Counters 60
3.3. Using a for Loop with Embedded if Statements (Guards) 62
3.4. Creating a for Comprehension (for/yield Combination) 63
v
www.it-ebooks.info
3.5. Implementing break and continue 65
3.6. Using the if Construct Like a Ternary Operator 71
3.7. Using a Match Expression Like a switch Statement 72
3.8. Matching Multiple Conditions with One Case Statement 76
3.9. Assigning the Result of a Match Expression to a Variable 77
3.10. Accessing the Value of the Default Case in a Match Expression 78
3.11. Using Pattern Matching in Match Expressions 79
3.12. Using Case Classes in Match Expressions 86
3.13. Adding if Expressions (Guards) to Case Statements 87
3.14. Using a Match Expression Instead of isInstanceOf 88
3.15. Working with a List in a Match Expression 89
3.16. Matching One or More Exceptions with try/catch 91
3.17. Declaring a Variable Before Using It in a try/catch/finally Block 92
3.18. Creating Your Own Control Structures 95
4. Classes and Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
4.1. Creating a Primary Constructor 100
4.2. Controlling the Visibility of Constructor Fields 104
4.3. Defining Auxiliary Constructors 108
4.4. Defining a Private Primary Constructor 112
4.5. Providing Default Values for Constructor Parameters 114
4.6. Overriding Default Accessors and Mutators 116
4.7. Preventing Getter and Setter Methods from Being Generated 119
4.8. Assigning a Field to a Block or Function 121
4.9. Setting Uninitialized var Field Types 122
4.10. Handling Constructor Parameters When Extending a Class 124
4.11. Calling a Superclass Constructor 127
4.12. When to Use an Abstract Class 129
4.13. Defining Properties in an Abstract Base Class (or Trait) 131
4.14. Generating Boilerplate Code with Case Classes 136
4.15. Defining an equals Method (Object Equality) 140
4.16. Creating Inner Classes 143
5. Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
5.1. Controlling Method Scope 148
5.2. Calling a Method on a Superclass 152
5.3. Setting Default Values for Method Parameters 154
5.4. Using Parameter Names When Calling a Method 157
5.5. Defining a Method That Returns Multiple Items (Tuples) 159
5.6. Forcing Callers to Leave Parentheses off Accessor Methods 161
5.7. Creating Methods That Take Variable-Argument Fields 163
5.8. Declaring That a Method Can Throw an Exception 165
vi | Table of Contents
www.it-ebooks.info
5.9. Supporting a Fluent Style of Programming 167
6. Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
6.1. Object Casting 172
6.2. The Scala Equivalent of Java’s .class 174
6.3. Determining the Class of an Object 174
6.4. Launching an Application with an Object 176
6.5. Creating Singletons with object 178
6.6. Creating Static Members with Companion Objects 180
6.7. Putting Common Code in Package Objects 182
6.8. Creating Object Instances Without Using the new Keyword 185
6.9. Implement the Factory Method in Scala with apply 189
7. Packaging and Imports. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
7.1. Packaging with the Curly Braces Style Notation 192
7.2. Importing One or More Members 193
7.3. Renaming Members on Import 195
7.4. Hiding a Class During the Import Process 196
7.5. Using Static Imports 197
7.6. Using Import Statements Anywhere 199
8. Traits. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
8.1. Using a Trait as an Interface 203
8.2. Using Abstract and Concrete Fields in Traits 206
8.3. Using a Trait Like an Abstract Class 207
8.4. Using Traits as Simple Mixins 208
8.5. Limiting Which Classes Can Use a Trait by Inheritance 209
8.6. Marking Traits So They Can Only Be Used by Subclasses of a Certain
Type 211
8.7. Ensuring a Trait Can Only Be Added to a Type That Has a Specific
Method 213
8.8. Adding a Trait to an Object Instance 215
8.9. Extending a Java Interface Like a Trait 216
9. Functional Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
9.1. Using Function Literals (Anonymous Functions) 218
9.2. Using Functions as Variables 219
9.3. Defining a Method That Accepts a Simple Function Parameter 223
9.4. More Complex Functions 226
9.5. Using Closures 229
9.6. Using Partially Applied Functions 234
9.7. Creating a Function That Returns a Function 236
Table of Contents | vii
www.it-ebooks.info
9.8. Creating Partial Functions 238
9.9. A Real-World Example 242
10. Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
10.1. Understanding the Collections Hierarchy 246
10.2. Choosing a Collection Class 250
10.3. Choosing a Collection Method to Solve a Problem 255
10.4. Understanding the Performance of Collections 261
10.5. Declaring a Type When Creating a Collection 264
10.6. Understanding Mutable Variables with Immutable Collections 265
10.7. Make Vector Your “Go To” Immutable Sequence 266
10.8. Make ArrayBuffer Your “Go To” Mutable Sequence 268
10.9. Looping over a Collection with foreach 270
10.10. Looping over a Collection with a for Loop 272
10.11. Using zipWithIndex or zip to Create Loop Counters 276
10.12. Using Iterators 278
10.13. Transforming One Collection to Another with for/yield 279
10.14. Transforming One Collection to Another with map 282
10.15. Flattening a List of Lists with flatten 285
10.16. Combining map and flatten with flatMap 286
10.17. Using filter to Filter a Collection 289
10.18. Extracting a Sequence of Elements from a Collection 291
10.19. Splitting Sequences into Subsets (groupBy, partition, etc.) 293
10.20. Walking Through a Collection with the reduce and fold Methods 295
10.21. Extracting Unique Elements from a Sequence 300
10.22. Merging Sequential Collections 302
10.23. Merging Two Sequential Collections into Pairs with zip 304
10.24. Creating a Lazy View on a Collection 306
10.25. Populating a Collection with a Range 309
10.26. Creating and Using Enumerations 311
10.27. Tuples, for When You Just Need a Bag of Things 312
10.28. Sorting a Collection 315
10.29. Converting a Collection to a String with mkString 318
11. List, Array, Map, Set (and More). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
11.1. Different Ways to Create and Populate a List 322
11.2. Creating a Mutable List 324
11.3. Adding Elements to a List 325
11.4. Deleting Elements from a List (or ListBuffer) 328
11.5. Merging (Concatenating) Lists 330
11.6. Using Stream, a Lazy Version of a List 331
11.7. Different Ways to Create and Update an Array 333
viii | Table of Contents
www.it-ebooks.info
11.8. Creating an Array Whose Size Can Change (ArrayBuffer) 335
11.9. Deleting Array and ArrayBuffer Elements 335
11.10. Sorting Arrays 337
11.11. Creating Multidimensional Arrays 338
11.12. Creating Maps 341
11.13. Choosing a Map Implementation 343
11.14. Adding, Updating, and Removing Elements with a Mutable Map 345
11.15. Adding, Updating, and Removing Elements with Immutable Maps 347
11.16. Accessing Map Values 349
11.17. Traversing a Map 350
11.18. Getting the Keys or Values from a Map 352
11.19. Reversing Keys and Values 352
11.20. Testing for the Existence of a Key or Value in a Map 353
11.21. Filtering a Map 354
11.22. Sorting an Existing Map by Key or Value 357
11.23. Finding the Largest Key or Value in a Map 360
11.24. Adding Elements to a Set 361
11.25. Deleting Elements from Sets 363
11.26. Using Sortable Sets 365
11.27. Using a Queue 367
11.28. Using a Stack 369
11.29. Using a Range 371
12. Files and Processes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
12.1. How to Open and Read a Text File 375
12.2. Writing Text Files 381
12.3. Reading and Writing Binary Files 382
12.4. How to Process Every Character in a Text File 383
12.5. How to Process a CSV File 384
12.6. Pretending that a String Is a File 387
12.7. Using Serialization 389
12.8. Listing Files in a Directory 391
12.9. Listing Subdirectories Beneath a Directory 392
12.10. Executing External Commands 394
12.11. Executing External Commands and Using STDOUT 397
12.12. Handling STDOUT and STDERR for External Commands 399
12.13. Building a Pipeline of Commands 401
12.14. Redirecting the STDOUT and STDIN of External Commands 402
12.15. Using AND (&&) and OR (||) with Processes 404
12.16. Handling Wildcard Characters in External Commands 405
12.17. How to Run a Process in a Different Directory 406
12.18. Setting Environment Variables When Running Commands 407
Table of Contents | ix
www.it-ebooks.info
12.19. An Index of Methods to Execute External Commands 408
13. Actors and Concurrency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
13.1. Getting Started with a Simple Actor 414
13.2. Creating an Actor Whose Class Constructor Requires Arguments 418
13.3. How to Communicate Between Actors 419
13.4. Understanding the Methods in the Akka Actor Lifecycle 422
13.5. Starting an Actor 425
13.6. Stopping Actors 427
13.7. Shutting Down the Akka Actor System 432
13.8. Monitoring the Death of an Actor with watch 433
13.9. Simple Concurrency with Futures 436
13.10. Sending a Message to an Actor and Waiting for a Reply 445
13.11. Switching Between Different States with become 446
13.12. Using Parallel Collections 448
14. Command-Line Tasks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
14.1. Getting Started with the Scala REPL 454
14.2. Pasting and Loading Blocks of Code into the REPL 459
14.3. Adding JAR Files and Classes to the REPL Classpath 461
14.4. Running a Shell Command from the REPL 462
14.5. Compiling with scalac and Running with scala 465
14.6. Disassembling and Decompiling Scala Code 466
14.7. Finding Scala Libraries 471
14.8. Generating Documentation with scaladoc 472
14.9. Faster Command-Line Compiling with fsc 479
14.10. Using Scala as a Scripting Language 480
14.11. Accessing Command-Line Arguments from a Script 483
14.12. Prompting for Input from a Scala Shell Script 485
14.13. Make Your Scala Scripts Run Faster 489
15. Web Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
15.1. Creating a JSON String from a Scala Object 491
15.2. Creating a JSON String from Classes That Have Collections 495
15.3. Creating a Simple Scala Object from a JSON String 500
15.4. Parsing JSON Data into an Array of Objects 501
15.5. Creating Web Services with Scalatra 503
15.6. Replacing XML Servlet Mappings with Scalatra Mounts 507
15.7. Accessing Scalatra Web Service GET Parameters 509
15.8. Accessing POST Request Data with Scalatra 510
15.9. Creating a Simple GET Request Client 514
15.10. Sending JSON Data to a POST URL 518
x | Table of Contents
www.it-ebooks.info
15.11. Getting URL Headers 519
15.12. Setting URL Headers When Sending a Request 520
15.13. Creating a GET Request Web Service with the Play Framework 521
15.14. POSTing JSON Data to a Play Framework Web Service 524
16. Databases and Persistence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
16.1. Connecting to MySQL with JDBC 528
16.2. Connecting to a Database with the Spring Framework 530
16.3. Connecting to MongoDB and Inserting Data 533
16.4. Inserting Documents into MongoDB with insert, save, or += 537
16.5. Searching a MongoDB Collection 539
16.6. Updating Documents in a MongoDB Collection 542
16.7. Accessing the MongoDB Document ID Field 544
16.8. Deleting Documents in a MongoDB Collection 545
16.9. A Quick Look at Slick 547
17. Interacting with Java. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
17.1. Going to and from Java Collections 549
17.2. Add Exception Annotations to Scala Methods to Work with Java 554
17.3. Using @SerialVersionUID and Other Annotations 556
17.4. Using the Spring Framework 557
17.5. Annotating varargs Methods 560
17.6. When Java Code Requires JavaBeans 562
17.7. Wrapping Traits with Implementations 565
18. The Simple Build Tool (SBT). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569
18.1. Creating a Project Directory Structure for SBT 570
18.2. Compiling, Running, and Packaging a Scala Project with SBT 574
18.3. Running Tests with SBT and ScalaTest 579
18.4. Managing Dependencies with SBT 581
18.5. Controlling Which Version of a Managed Dependency Is Used 584
18.6. Creating a Project with Subprojects 586
18.7. Using SBT with Eclipse 588
18.8. Generating Project API Documentation 590
18.9. Specifying a Main Class to Run 591
18.10. Using GitHub Projects as Project Dependencies 593
18.11. Telling SBT How to Find a Repository (Working with Resolvers) 595
18.12. Resolving Problems by Getting an SBT Stack Trace 596
18.13. Setting the SBT Log Level 597
18.14. Deploying a Single, Executable JAR File 597
18.15. Publishing Your Library 601
18.16. Using Build.scala Instead of build.sbt 602
Table of Contents | xi
www.it-ebooks.info
18.17. Using a Maven Repository Library with SBT 604
18.18. Building a Scala Project with Ant 606
19. Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
19.1. Creating Classes That Use Generic Types 614
19.2. Creating a Method That Takes a Simple Generic Type 617
19.3. Using Duck Typing (Structural Types) 618
19.4. Make Mutable Collections Invariant 620
19.5. Make Immutable Collections Covariant 622
19.6. Create a Collection Whose Elements Are All of Some Base Type 624
19.7. Selectively Adding New Behavior to a Closed Model 627
19.8. Building Functionality with Types 630
20. Idioms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635
20.1. Create Methods with No Side Effects (Pure Functions) 636
20.2. Prefer Immutable Objects 644
20.3. Think “Expression-Oriented Programming” 647
20.4. Use Match Expressions and Pattern Matching 650
20.5. Eliminate null Values from Your Code 654
20.6. Using the Option/Some/None Pattern 658
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
xii | Table of Contents
www.it-ebooks.info
Preface
This is a cookbook of problem-solving recipes about Scala, the most interesting pro‐
gramming language I’ve ever used. The book contains solutions to more than 250 com‐
mon problems, shown with possibly more than 700 examples. (I haven’t counted, but I
suspect that’s true.)
There are a few unique things about this book:
• As a cookbook, it’s intended to save you time by providing solutions to the most
common problems you’ll encounter.
• Almost all of the examples are shown in the Scala interpreter. As a result, whether
you’re sitting by a computer, on a plane, or reading in your favorite recliner, you
get the benefit of seeing their exact output. (Which often leads to, “Ah, so that’s how
that works.”)
• The book covers not only the Scala language, but also has large chapters on Scala
tools and libraries, including SBT, actors, the collections library (more than 100
pages), and JSON processing.
Just prior to its release, the book was updated to cover Scala 2.10.x and SBT 0.12.3.
The Scala Language
My (oversimplified) Scala elevator pitch is that it’s a child of Ruby and Java: it’s light,
concise, and readable like Ruby, but it compiles to class files that you package as JAR
files that run on the JVM; it uses traits and mixins, and feels dynamic, but it’s statically
typed. It uses the Actor model to simplify concurrent programming so you can keep
those multicore processors humming. The name Scala comes from the word scalable,
and true to that name, it’s used to power the busiest websites in the world, including
Twitter, Netflix, Tumblr, LinkedIn, Foursquare, and many more.
xiii
www.it-ebooks.info