Scala for the Impatient, Second Edition

Book description

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.

    Table of contents

    1. About This E-Book
    2. Title Page
    3. Copyright Page
    4. Dedication Page
    5. Contents
    6. Foreword to the First Edition
    7. Preface
    8. About the Author
    9. Chapter 1. The Basics
      1. 1.1 The Scala Interpreter
      2. 1.2 Declaring Values and Variables
      3. 1.3 Commonly Used Types
      4. 1.4 Arithmetic and Operator Overloading
      5. 1.5 More about Calling Methods
      6. 1.6 The apply Method
      7. 1.7 Scaladoc
      8. Exercises
    10. Chapter 2. Control Structures and Functions
      1. 2.1 Conditional Expressions
      2. 2.2 Statement Termination
      3. 2.3 Block Expressions and Assignments
      4. 2.4 Input and Output
      5. 2.5 Loops
      6. 2.6 Advanced for Loops
      7. 2.7 Functions
      8. 2.8 Default and Named Arguments
      9. 2.9 Variable Arguments
      10. 2.10 Procedures
      11. 2.11 Lazy Values
      12. 2.12 Exceptions
      13. Exercises
    11. Chapter 3. Working with Arrays
      1. 3.1 Fixed-Length Arrays
      2. 3.2 Variable-Length Arrays: Array Buffers
      3. 3.3 Traversing Arrays and Array Buffers
      4. 3.4 Transforming Arrays
      5. 3.5 Common Algorithms
      6. 3.6 Deciphering Scaladoc
      7. 3.7 Multidimensional Arrays
      8. 3.8 Interoperating with Java
      9. Exercises
    12. Chapter 4. Maps and Tuples
      1. 4.1 Constructing a Map
      2. 4.2 Accessing Map Values
      3. 4.3 Updating Map Values
      4. 4.4 Iterating over Maps
      5. 4.5 Sorted Maps
      6. 4.6 Interoperating with Java
      7. 4.7 Tuples
      8. 4.8 Zipping
      9. Exercises
    13. Chapter 5. Classes
      1. 5.1 Simple Classes and Parameterless Methods
      2. 5.2 Properties with Getters and Setters
      3. 5.3 Properties with Only Getters
      4. 5.4 Object-Private Fields
      5. 5.5 Bean Properties
      6. 5.6 Auxiliary Constructors
      7. 5.7 The Primary Constructor
      8. 5.8 Nested Classes
      9. Exercises
    14. Chapter 6. Objects
      1. 6.1 Singletons
      2. 6.2 Companion Objects
      3. 6.3 Objects Extending a Class or Trait
      4. 6.4 The apply Method
      5. 6.5 Application Objects
      6. 6.6 Enumerations
      7. Exercises
    15. Chapter 7. Packages and Imports
      1. 7.1 Packages
      2. 7.2 Scope Rules
      3. 7.3 Chained Package Clauses
      4. 7.4 Top-of-File Notation
      5. 7.5 Package Objects
      6. 7.6 Package Visibility
      7. 7.7 Imports
      8. 7.8 Imports Can Be Anywhere
      9. 7.9 Renaming and Hiding Members
      10. 7.10 Implicit Imports
      11. Exercises
    16. Chapter 8. Inheritance
      1. 8.1 Extending a Class
      2. 8.2 Overriding Methods
      3. 8.3 Type Checks and Casts
      4. 8.4 Protected Fields and Methods
      5. 8.5 Superclass Construction
      6. 8.6 Overriding Fields
      7. 8.7 Anonymous Subclasses
      8. 8.8 Abstract Classes
      9. 8.9 Abstract Fields
      10. 8.10 Construction Order and Early Definitions
      11. 8.11 The Scala Inheritance Hierarchy
      12. 8.12 Object Equality
      13. 8.13 Value Classes
      14. Exercises
    17. Chapter 9. Files and Regular Expressions
      1. 9.1 Reading Lines
      2. 9.2 Reading Characters
      3. 9.3 Reading Tokens and Numbers
      4. 9.4 Reading from URLs and Other Sources
      5. 9.5 Reading Binary Files
      6. 9.6 Writing Text Files
      7. 9.7 Visiting Directories
      8. 9.8 Serialization
      9. 9.9 Process Control
      10. 9.10 Regular Expressions
      11. 9.11 Regular Expression Groups
      12. Exercises
    18. Chapter 10. Traits
      1. 10.1 Why No Multiple Inheritance?
      2. 10.2 Traits as Interfaces
      3. 10.3 Traits with Concrete Implementations
      4. 10.4 Objects with Traits
      5. 10.5 Layered Traits
      6. 10.6 Overriding Abstract Methods in Traits
      7. 10.7 Traits for Rich Interfaces
      8. 10.8 Concrete Fields in Traits
      9. 10.9 Abstract Fields in Traits
      10. 10.10 Trait Construction Order
      11. 10.11 Initializing Trait Fields
      12. 10.12 Traits Extending Classes
      13. 10.13 Self Types
      14. 10.14 What Happens under the Hood
      15. Exercises
    19. Chapter 11. Operators
      1. 11.1 Identifiers
      2. 11.2 Infix Operators
      3. 11.3 Unary Operators
      4. 11.4 Assignment Operators
      5. 11.5 Precedence
      6. 11.6 Associativity
      7. 11.7 The apply and update Methods
      8. 11.8 Extractors
      9. 11.9 Extractors with One or No Arguments
      10. 11.10 The unapplySeq Method
      11. 11.11 Dynamic Invocation
      12. Exercises
    20. Chapter 12. Higher-Order Functions
      1. 12.1 Functions as Values
      2. 12.2 Anonymous Functions
      3. 12.3 Functions with Function Parameters
      4. 12.4 Parameter Inference
      5. 12.5 Useful Higher-Order Functions
      6. 12.6 Closures
      7. 12.7 SAM Conversions
      8. 12.8 Currying
      9. 12.9 Control Abstractions
      10. 12.10 The return Expression
      11. Exercises
    21. Chapter 13. Collections
      1. 13.1 The Main Collections Traits
      2. 13.2 Mutable and Immutable Collections
      3. 13.3 Sequences
      4. 13.4 Lists
      5. 13.5 Sets
      6. 13.6 Operators for Adding or Removing Elements
      7. 13.7 Common Methods
      8. 13.8 Mapping a Function
      9. 13.9 Reducing, Folding, and Scanning
      10. 13.10 Zipping
      11. 13.11 Iterators
      12. 13.12 Streams
      13. 13.13 Lazy Views
      14. 13.14 Interoperability with Java Collections
      15. 13.15 Parallel Collections
      16. Exercises
    22. Chapter 14. Pattern Matching and Case Classes
      1. 14.1 A Better Switch
      2. 14.2 Guards
      3. 14.3 Variables in Patterns
      4. 14.4 Type Patterns
      5. 14.5 Matching Arrays, Lists, and Tuples
      6. 14.6 Extractors
      7. 14.7 Patterns in Variable Declarations
      8. 14.8 Patterns in for Expressions
      9. 14.9 Case Classes
      10. 14.10 The copy Method and Named Parameters
      11. 14.11 Infix Notation in case Clauses
      12. 14.12 Matching Nested Structures
      13. 14.13 Are Case Classes Evil?
      14. 14.14 Sealed Classes
      15. 14.15 Simulating Enumerations
      16. 14.16 The Option Type
      17. 14.17 Partial Functions
      18. Exercises
    23. Chapter 15. Annotations
      1. 15.1 What Are Annotations?
      2. 15.2 What Can Be Annotated?
      3. 15.3 Annotation Arguments
      4. 15.4 Annotation Implementations
      5. 15.5 Annotations for Java Features
        1. 15.5.1 Java Modifiers
        2. 15.5.2 Marker Interfaces
        3. 15.5.3 Checked Exceptions
        4. 15.5.4 Variable Arguments
        5. 15.5.5 JavaBeans
      6. 15.6 Annotations for Optimizations
        1. 15.6.1 Tail Recursion
        2. 15.6.2 Jump Table Generation and Inlining
        3. 15.6.3 Eliding Methods
        4. 15.6.4 Specialization for Primitive Types
      7. 15.7 Annotations for Errors and Warnings
      8. Exercises
    24. Chapter 16. XML Processing
      1. 16.1 XML Literals
      2. 16.2 XML Nodes
      3. 16.3 Element Attributes
      4. 16.4 Embedded Expressions
      5. 16.5 Expressions in Attributes
      6. 16.6 Uncommon Node Types
      7. 16.7 XPath-like Expressions
      8. 16.8 Pattern Matching
      9. 16.9 Modifying Elements and Attributes
      10. 16.10 Transforming XML
      11. 16.11 Loading and Saving
      12. 16.12 Namespaces
      13. Exercises
    25. Chapter 17. Futures
      1. 17.1 Running Tasks in the Future
      2. 17.2 Waiting for Results
      3. 17.3 The Try Class
      4. 17.4 Callbacks
      5. 17.5 Composing Future Tasks
      6. 17.6 Other Future Transformations
      7. 17.7 Methods in the Future Object
      8. 17.8 Promises
      9. 17.9 Execution Contexts
      10. Exercises
    26. Chapter 18. Type Parameters
      1. 18.1 Generic Classes
      2. 18.2 Generic Functions
      3. 18.3 Bounds for Type Variables
      4. 18.4 View Bounds
      5. 18.5 Context Bounds
      6. 18.6 The ClassTag Context Bound
      7. 18.7 Multiple Bounds
      8. 18.8 Type Constraints
      9. 18.9 Variance
      10. 18.10 Co- and Contravariant Positions
      11. 18.11 Objects Can’t Be Generic
      12. 18.12 Wildcards
      13. Exercises
    27. Chapter 19. Advanced Types
      1. 19.1 Singleton Types
      2. 19.2 Type Projections
      3. 19.3 Paths
      4. 19.4 Type Aliases
      5. 19.5 Structural Types
      6. 19.6 Compound Types
      7. 19.7 Infix Types
      8. 19.8 Existential Types
      9. 19.9 The Scala Type System
      10. 19.10 Self Types
      11. 19.11 Dependency Injection
      12. 19.12 Abstract Types
      13. 19.13 Family Polymorphism
      14. 19.14 Higher-Kinded Types
      15. Exercises
    28. Chapter 20. Parsing
      1. 20.1 Grammars
      2. 20.2 Combining Parser Operations
      3. 20.3 Transforming Parser Results
      4. 20.4 Discarding Tokens
      5. 20.5 Generating Parse Trees
      6. 20.6 Avoiding Left Recursion
      7. 20.7 More Combinators
      8. 20.8 Avoiding Backtracking
      9. 20.9 Packrat Parsers
      10. 20.10 What Exactly Are Parsers?
      11. 20.11 Regex Parsers
      12. 20.12 Token-Based Parsers
      13. 20.13 Error Handling
      14. Exercises
    29. Chapter 21. Implicits
      1. 21.1 Implicit Conversions
      2. 21.2 Using Implicits for Enriching Existing Classes
      3. 21.3 Importing Implicits
      4. 21.4 Rules for Implicit Conversions
      5. 21.5 Implicit Parameters
      6. 21.6 Implicit Conversions with Implicit Parameters
      7. 21.7 Context Bounds
      8. 21.8 Type Classes
      9. 21.9 Evidence
      10. 21.10 The @implicitNotFound Annotation
      11. 21.11 CanBuildFrom Demystified
      12. Exercises
    30. Index
    31. Code Snippets

    Product information

    • Title: Scala for the Impatient, Second Edition
    • Author(s): Cay S. Horstmann
    • Release date: December 2016
    • Publisher(s): Addison-Wesley Professional
    • ISBN: 9780134540627