Programming in Scala

Download Programming in Scala

Post on 26-Oct-2014

59 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

Good book on scala.

TRANSCRIPT

<p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>Programming in Scala</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>Programming in ScalaMartin Odersky, Lex Spoon, Bill Venners</p> <p>artimaA RTIMA P RESSM OUNTAIN V IEW, C ALIFORNIA</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>ivProgramming in Scala First Edition, Version 6 Martin Odersky is the creator of the Scala language and a professor at EPFL in Lausanne, Switzerland. Lex Spoon worked on Scala for two years as a post-doc with Martin Odersky. Bill Venners is president of Artima, Inc. Artima Press is an imprint of Artima, Inc. P.O. Box 390122, Mountain View, California 94039 Copyright 2007, 2008 Martin Odersky, Lex Spoon, and Bill Venners. All rights reserved. First edition published as PrePrint eBook 2007 First edition published 2008 Produced in the United States of America 12 11 10 09 08 56789</p> <p>ISBN-10: 0-9815316-1-X ISBN-13: 978-0-9815316-1-8 No part of this publication may be reproduced, modied, distributed, stored in a retrieval system, republished, displayed, or performed, for commercial or noncommercial purposes or for compensation of any kind without prior written permission from Artima, Inc. All information and materials in this book are provided "as is" and without warranty of any kind. The term Artima and the Artima logo are trademarks or registered trademarks of Artima, Inc. All other company and/or product names may be trademarks or registered trademarks of their owners.</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>to Nastaran - M.O. to Fay - L.S. to Siew - B.V.</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>OverviewContents List of Figures List of Tables List of Listings Foreword Acknowledgments Introduction 1. A Scalable Language 2. First Steps in Scala 3. Next Steps in Scala 4. Classes and Objects 5. Basic Types and Operations 6. Functional Objects 7. Built-in Control Structures 8. Functions and Closures 9. Control Abstraction 10. Composition and Inheritance 11. Scalas Hierarchy 12. Traits 13. Packages and Imports 14. Assertions and Unit Testing 15. Case Classes and Pattern Matching 16. Working with Lists 17. Collections 18. Stateful Objects 19. Type Parameterization 20. Abstract Members 21. Implicit Conversions and Parameters 22. Implementing Lists 23. For Expressions Revisited 24. Extractors 25. Annotations 26. Working with XML 27. Modular Programming Using Objects 28. Object Equality 29. Combining Scala and Java 30. Actors and Concurrency 31. Combinator Parsing 32. GUI Programming 33. The SCells Spreadsheet A. Scala scripts on Unix and Windows Glossary Bibliography About the Authors Indexvii xvii xix xx xxvii xxix xxxi 39 58 71 93 107 129 149 173 194 209 237 245 264 279 293 327 359 387 410 435 465 487 500 516 532 539 553 568 594 607 642 671 683 708 709 725 728 729</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>Contents</p> <p>Contents List of Figures List of Tables List of Listings Foreword Acknowledgments Introduction 1 A Scalable Language 1.1 A language that grows on you 1.2 What makes Scala scalable? . 1.3 Why Scala? . . . . . . . . . . 1.4 Scalas roots . . . . . . . . . 1.5 Conclusion . . . . . . . . . .</p> <p>vii xvii xix xx xxvii xxix xxxi 39 40 45 48 55 57 58 58 60 62 64 65 67 70</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>. . . . .</p> <p>2</p> <p>First Steps in Scala Step 1. Learn to use the Scala interpreter . Step 2. Dene some variables . . . . . . Step 3. Dene some functions . . . . . . Step 4. Write some Scala scripts . . . . . Step 5. Loop with while; decide with if Step 6. Iterate with foreach and for . . Conclusion . . . . . . . . . . . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>Contents 3 Next Steps in Scala Step 7. Parameterize arrays with types . . . . Step 8. Use lists . . . . . . . . . . . . . . . . Step 9. Use tuples . . . . . . . . . . . . . . . Step 10. Use sets and maps . . . . . . . . . . . Step 11. Learn to recognize the functional style Step 12. Read lines from a le . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . . . Classes and Objects 4.1 Classes, elds, and methods 4.2 Semicolon inference . . . . 4.3 Singleton objects . . . . . . 4.4 A Scala application . . . . 4.5 The Application trait . . . 4.6 Conclusion . . . . . . . . . 71 71 75 80 81 86 89 92 93 93 98 99 102 105 106 107 107 108 115 118 119 121 123 124 127 127 129 129 130 132 133 133 135 136 138</p> <p>viii</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>4</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>5</p> <p>Basic Types and Operations 5.1 Some basic types . . . . . . . . . . . 5.2 Literals . . . . . . . . . . . . . . . . 5.3 Operators are methods . . . . . . . . 5.4 Arithmetic operations . . . . . . . . 5.5 Relational and logical operations . . 5.6 Bitwise operations . . . . . . . . . . 5.7 Object equality . . . . . . . . . . . . 5.8 Operator precedence and associativity 5.9 Rich wrappers . . . . . . . . . . . . 5.10 Conclusion . . . . . . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>6</p> <p>Functional Objects 6.1 A specication for class Rational . . 6.2 Constructing a Rational . . . . . . . 6.3 Reimplementing the toString method 6.4 Checking preconditions . . . . . . . . 6.5 Adding elds . . . . . . . . . . . . . . 6.6 Self references . . . . . . . . . . . . . 6.7 Auxiliary constructors . . . . . . . . . 6.8 Private elds and methods . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>Contents 6.9 6.10 6.11 6.12 6.13 6.14 7 Dening operators . Identiers in Scala . Method overloading Implicit conversions A word of caution . Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 141 144 146 147 147 149 150 151 154 159 163 165 167 170 172 173 173 175 177 179 180 181 184 188 189 193 194 194 198 200 202 205 208</p> <p>ix</p> <p>Built-in Control Structures 7.1 If expressions . . . . . . . . . . . . . . 7.2 While loops . . . . . . . . . . . . . . . 7.3 For expressions . . . . . . . . . . . . . . 7.4 Exception handling with try expressions 7.5 Match expressions . . . . . . . . . . . . 7.6 Living without break and continue . . 7.7 Variable scope . . . . . . . . . . . . . . 7.8 Refactoring imperative-style code . . . . 7.9 Conclusion . . . . . . . . . . . . . . . . Functions and Closures 8.1 Methods . . . . . . . . . . . . 8.2 Local functions . . . . . . . . . 8.3 First-class functions . . . . . . 8.4 Short forms of function literals 8.5 Placeholder syntax . . . . . . . 8.6 Partially applied functions . . . 8.7 Closures . . . . . . . . . . . . 8.8 Repeated parameters . . . . . . 8.9 Tail recursion . . . . . . . . . . 8.10 Conclusion . . . . . . . . . . . Control Abstraction 9.1 Reducing code duplication . . 9.2 Simplifying client code . . . 9.3 Currying . . . . . . . . . . . 9.4 Writing new control structures 9.5 By-name parameters . . . . . 9.6 Conclusion . . . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>8</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>9</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>. . . . . .</p> <p>Cover Overview Contents Discuss Suggest Glossary Index</p> <p>Contents 10 Composition and Inheritance 10.1 A two-dimensional layout library . . . . . . 10.2 Abstract classes . . . . . . . . . . . . . . . 10.3 Dening parameterless methods . . . . . . . 10.4 Extending classes . . . . . . . . . . . . . . 10.5 Overriding methods and elds . . . . . . . . 10.6 Dening parametric elds . . . . . . . . . . 10.7 Invoking superclass constructors . . . . . . . 10.8 Using override modiers . . . . . . . . . . 10.9 Polymorphism and dynamic binding . . . . 10.10 Declaring nal members . . . . . . . . . . . 10.11 Using composition and inheritance . . . . . 10.12 Implementing above, beside, and toString 10.13 Dening a factory object . . . . . . . . . . . 10.14 Heighten and widen . . . . . . . . . . . . . 10.15 Putting it all together . . . . . . . . . . . . . 10.16 Conclusion . . . . . . . . . . . . . . . . . . 11 Scalas Hierarchy 11.1 Scalas class hierarchy . . . . . . 11.2 How primitives are implemented 11.3 Bottom types . . . . . . . . . . . 11.4 Conclusion . . . . . . . . . . . . 12 Traits 12.1 How traits work . . . . . . . . . 12.2 Thin versus rich interfaces . . . . 12.3 Example: Rectangular objects . . 12.4 The Ordered trait . . . . . . . . 12.5 Traits as stackable modications . 12.6 Why not multiple inheritance? . . 12.7 To trait, or not to trait? . . . . . . 12.8 Conclusion . . . . . . . . . . . . 209 209 210 211 214 216 217 219 220 222 224 226 227 229 231 235 236 237 237 241 243 244 245 245 248 249 252 254 258 262 263 264 264 268 272</p> <p>x</p> <p>. . . . . . . . . . . . . . . .</p> <p>. . . . . . . . . . . . . . . .</p> <p>. . . . . . . . . . . . . . . .</p> <p>. . . . . . . . . . . . . . . .</p> <p>. . . . . . . . . . . . . . . .</p> <p>. . . . . . . . . . . . . . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>13 Packages and Imports 13.1 Packages . . . . . . . . . . . . . . . . . . . . . . . . . 13.2 Imports . . . . . . . . . . . . . . . . . . . . . . . . . . 13.3 Implicit imports . . . . . . . . . . . . . . . . . . . . . Cover Overview Contents Discuss Suggest Glossary Index</p> <p>Contents 13.4 Access modiers . . . . . . . . . . . . . . . . . . . . . 13.5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . 14 Assertions and Unit Testing 14.1 Assertions . . . . . . . . . . 14.2 Unit testing in Scala . . . . . 14.3 Informative failure reports . . 14.4 Using JUnit and TestNG . . . 14.5 Tests as specications . . . . 14.6 Property-based testing . . . . 14.7 Organizing and running tests 14.8 Conclusion . . . . . . . . . . 273 278 279 279 281 282 284 286 288 290 292 293 293 298 307 309 310 312 314 318 326 327 327 328 328 329 330 332 343 351 355 358 359 359</p> <p>xi</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>. . . . . . . .</p> <p>15 Case Classes and Pattern Matching 15.1 A simple example . . . . . . . 15.2 Kinds of patterns . . . . . . . . 15.3 Pattern guards . . . . . . . . . 15.4 Pattern overlaps . . . . . . . . 15.5 Sealed classes . . . . . . . . . 15.6 The Option type . . . . . . . . 15.7 Patterns everywhere . . . . . . 15.8 A larger example . . . . . . . . 15.9 Conclusion . . . . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>16 Working with Lists 16.1 List literals . . . . . . . . . . . . . . . . . . . . 16.2 The List type . . . . . . . . . . . . . . . . . . 16.3 Constructing lists . . . . . . . . . . . . . . . . . 16.4 Basic operations on lists . . . . . . . . . . . . . 16.5 List patterns . . . . . . . . . . . . . . . . . . . 16.6 First-order methods on class List . . . . . . . . 16.7 Higher-order methods on class List . . . . . . 16.8 Methods of the List object . . . . . . . . . . . 16.9 Understanding Scalas type inference algorithm . 16.10 Conclusion . . . . . . . . . . . . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>. . . . . . . . . .</p> <p>17 Collections 17.1 Overview of the library . . . . . . . . . . . . . . . . . Cover Overview Contents Discuss Suggest Glossary Index</p> <p>Contents 17.2 17.3 17.4 17.5 17.6 17.7 Sequences . . . . . . . . . . . . . . . . . . . . Sets and maps . . . . . . . . . . . . . . . . . . Selecting mutable versus immutable collections Initializing collections . . . . . . . . . . . . . . Tuples . . . . . . . . . . . . . . . . . . . . . . Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 367 377 380 383 386 387 387 390 393 394 397 401 409 410 410 414 417 421 424 426 429 431 434 435 435 436 437 438 439 447 449 452 454 463</p> <p>xii</p> <p>18 Stateful Objects 18.1 What makes an object stateful? . . . 18.2 Reassignable variables and properties 18.3 Case study: Discrete event simulation 18.4 A language for digital circuits . . . . 18.5 The Simulation API . . . . . . . . 18.6 Circuit Simulation . . . . . . . . . . 18.7 Conclusion . . . . . . . . . . . . . . 19 Type Parameterization 19.1 Functional queues . . . . . . . 19.2 Information hiding . . . . . . . 19.3 Variance annotations . . . . . . 19.4 Checking variance annotations . 19.5 Lower bounds . . . . . . . . . 19.6 Contravariance . . . . . . . . . 19.7 Object private data . . . . . . . 19.8 Upper bounds . . . . . . . . . 19.9 Conclusion . . . . . . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . . .</p> <p>. . . . . . . ....</p>