You are previewing Beginning Scala.
O'Reilly logo
Beginning Scala

Book Description

The open source Scala language is a Java-based dynamic scripting, functional programming language. Moreover, this highly scalable scripting language lends itself well to building Cloud-based/deliverable Software as a Service (SaaS) online applications.

Written by Lift Scala web framework founder and lead Dave Pollak, Beginning Scala takes a down-to-earth approach to teaching Scala that leads you through simple examples that can be combined to build complex, scalable systems and applications.

This book introduces you to the Scala programming language and then guides you through Scala constructs and libraries that allow small and large teams to assemble small components into high-performance, scalable systems. You will learn why Scala is becoming the language of choice for Web 2.0 companies such as Twitter as well as enterprises such as Seimens and SAP.

Table of Contents

  1. Copyright
  2. Foreword
  3. About the Author
  4. About the Technical Reviewer
  5. Acknowledgments
  6. Introduction
    1. Who This Book Is For
    2. How This Book Is Structured
    3. Prerequisites
    4. Downloading the Code
    5. Contacting the Author
  7. 1. About Scala and How to Install It
    1. 1.1. Scala: A Great Language for the Java Virtual Machine
      1. 1.1.1. Beyond Java
      2. 1.1.2. Finding Scala
    2. 1.2. Scala's History
    3. 1.3. Installing Scala
      1. 1.3.1. Installing on Windows
      2. 1.3.2. Installing on Mac OS X and Linux
    4. 1.4. The Scala Community
    5. 1.5. Summary
  8. 2. Scala Syntax, Scripts, and Your First Scala Programs
    1. 2.1. Scala at the Command Line and Scala Scripts
      1. 2.1.1. Interactive Scala
      2. 2.1.2. Scala Scripts
      3. 2.1.3. Compiling Scala Programs
    2. 2.2. Your First Scala Programs
      1. 2.2.1. Hello World
      2. 2.2.2. Printing Some Numbers
      3. 2.2.3. Adding Things Up
        1. 2.2.3.1. Importing Stuff
        2. 2.2.3.2. Parsing a String to an Int
        3. 2.2.3.3. Summing Things Up
        4. 2.2.3.4. Program Body
    3. 2.3. Basic Scala Syntax
      1. 2.3.1. Number, String, and XML Constants
      2. 2.3.2. package
      3. 2.3.3. import
      4. 2.3.4. Class, Trait, and Object Definition
      5. 2.3.5. Scala's Class Hierarchy
      6. 2.3.6. Method Declaration
      7. 2.3.7. Variable Declaration
      8. 2.3.8. Code Blocks
      9. 2.3.9. Call-by-Name
      10. 2.3.10. Method Invocation
      11. 2.3.11. Functions, apply, update, and Compiler Magic
      12. 2.3.12. Case Classes
      13. 2.3.13. Basic Pattern Matching
      14. 2.3.14. if/else and while
      15. 2.3.15. The for comprehension
      16. 2.3.16. throw, try/catch/finally, and synchronized
      17. 2.3.17. Comments
    4. 2.4. Scala vs. Java vs. Ruby
      1. 2.4.1. Classes and Instances
      2. 2.4.2. Traits, Interfaces, and Mixins
      3. 2.4.3. Object, Static, and Singletons
      4. 2.4.4. Functions, Anonymous Inner Classes, and Lambdas/Procs
    5. 2.5. Summary
  9. 3. Collections and the Joy of Immutability
    1. 3.1. Thinking Immutably
    2. 3.2. Scala List, Tuple, Option, and Map Classes
      1. 3.2.1. List[T]
        1. 3.2.1.1. Getting Functional
        2. 3.2.1.2. Contrast with Java
        3. 3.2.1.3. Transformation
        4. 3.2.1.4. Transformation vs. Mutation
        5. 3.2.1.5. Reduxio
        6. 3.2.1.6. Look Ma, No Loops
      2. 3.2.2. Tuples
      3. 3.2.3. Map[K, V]
      4. 3.2.4. Option[T]
      5. 3.2.5. Wrapping Up List, Tuple, Map, and Option
    3. 3.3. XML Creation and Manipulation
      1. 3.3.1. XML in Your Scala Code
      2. 3.3.2. Parsing XML
      3. 3.3.3. Modifying XML
    4. 3.4. Concurrency Without Synchronization
    5. 3.5. Summary
  10. 4. Fun with Functions, and Never Having to Close That JDBC Connection
    1. 4.1. A Function Is an Instance
      1. 4.1.1. Passing Functions As Parameters
      2. 4.1.2. Partial Application and Functions
      3. 4.1.3. Functions and Type Parameters
      4. 4.1.4. Functions Bound to Variables in Scope
      5. 4.1.5. Putting Functions in Containers
      6. 4.1.6. Functions and Interactive Applications
      7. 4.1.7. Building New Functions
    2. 4.2. Call-by-Name, Call-by-Value, and General Laziness
    3. 4.3. Build Your Own Control Structures
      1. 4.3.1. JDBC Looping
    4. 4.4. Summary
  11. 5. Pattern Matching
    1. 5.1. Basic Pattern Matching
      1. 5.1.1. Matching Any Type
      2. 5.1.2. More Pattern Matching
      3. 5.1.3. Testing Data Types
      4. 5.1.4. Case Classes
      5. 5.1.5. Pattern Matching in Lists
      6. 5.1.6. Pattern Matching and Lists
      7. 5.1.7. Nested Pattern Matching in Case Classes
      8. 5.1.8. Examining the Internals of Pattern Matching
    2. 5.2. Pattern Matching As Functions
    3. 5.3. Object-Oriented and Functional Tensions
      1. 5.3.1. Shape Abstractions
    4. 5.4. Summary
  12. 6. Actors and Concurrency
    1. 6.1. A Different Approach to Concurrency: Look Ma, No Locks
    2. 6.2. Defining an Actor
      1. 6.2.1. Defining an Actor
      2. 6.2.2. Would You Like State With That?
      3. 6.2.3. Instantiating an Actor
    3. 6.3. Implementing a Listener
      1. 6.3.1. As an Actor
    4. 6.4. Actors: More Than Write-Only
    5. 6.5. Beyond the Basics
      1. 6.5.1. Protocol Handler
      2. 6.5.2. Actors, Atomic Updates, and Transactions
        1. 6.5.2.1. Performing an Atomic Update
        2. 6.5.2.2. Performing Transactions
        3. 6.5.2.3. Testing the Transactional Actor
      3. 6.5.3. Composing Actors
    6. 6.6. Summary
  13. 7. Traits and Types and Gnarly Stuff for Architects
    1. 7.1. Show Me Some Bad Java Code
    2. 7.2. Library Pimping, Implicit Conversions, and Vampires
      1. 7.2.1. Library Pimping
      2. 7.2.2. Inviting Vampires Into Your Code
    3. 7.3. Traits: Interfaces on Steroids
      1. 7.3.1. Traits and Class Hierarchies
    4. 7.4. Types—It's Beyond Generic
      1. 7.4.1. Table Code
      2. 7.4.2. The Column
      3. 7.4.3. The Table
      4. 7.4.4. Concrete Columns
      5. 7.4.5. Building a Query
      6. 7.4.6. Making Queries Type-Safe
      7. 7.4.7. Boilerplate
      8. 7.4.8. Vending JDBC Connections
      9. 7.4.9. Making a Concrete Class That Implements Table
      10. 7.4.10. Type-Safe Fun
    5. 7.5. Variance
      1. 7.5.1. Invariant Parameter Types
      2. 7.5.2. Covariant Parameter Types
      3. 7.5.3. Contravariant Parameter Types
      4. 7.5.4. Rules of Variance
    6. 7.6. Poignantly Killing Bunnies: Dwemthy's Stairs
      1. 7.6.1. The Immutable World
      2. 7.6.2. The Code
      3. 7.6.3. Walking Through the Code
    7. 7.7. Summary
  14. 8. Parsers—Because BNF Is Not Just for Academics Anymore
    1. 8.1. Higher-Order Functions and Combinators
      1. 8.1.1. Higher-Order Functions
      2. 8.1.2. Combinators
    2. 8.2. The Calculator Parser
    3. 8.3. JSON Parsing
    4. 8.4. Twitter JSON Parsing
      1. 8.4.1. Utilities
      2. 8.4.2. Parsing the JSON
    5. 8.5. Summary
  15. 9. Scaling Your Team
    1. 9.1. Testing: Behavior-Driven Development
      1. 9.1.1. Specs
      2. 9.1.2. Scala Test
    2. 9.2. Build Tools
      1. 9.2.1. Maven
      2. 9.2.2. Ant
      3. 9.2.3. Buildr
      4. 9.2.4. Other Build Tools
    3. 9.3. Team Structure
      1. 9.3.1. Bringing New Team Members on Board
      2. 9.3.2. Organizing Teams Around Scala
    4. 9.4. Best Practices
      1. 9.4.1. Options Instead of Null Testing
      2. 9.4.2. Focus on Immutability
      3. 9.4.3. Keep Methods Short
      4. 9.4.4. Refactor Mercilessly
      5. 9.4.5. Compose Functions and Compose Classes
    5. 9.5. Selling Scala in Your Organization
      1. 9.5.1. The Super-Soft Sell
      2. 9.5.2. The Back-Office Project
      3. 9.5.3. The Big Sell
        1. 9.5.3.1. Addressing the Potential Risks of Scala
      4. 9.5.4. My Direct Experience (YMMV)
      5. 9.5.5. Selecting Team Members
      6. 9.5.6. Yeah, But How Compatible?
      7. 9.5.7. A Tad Bit of History
      8. 9.5.8. Present-Day Compatibility
      9. 9.5.9. Popping the Stack to the CEO or Board
      10. 9.5.10. Why Is Scala Different?
      11. 9.5.11. Anything You Can Do, I Can Do Better (in Scala)
      12. 9.5.12. Giving Scala a Lift
      13. 9.5.13. How Lift's Comet Support Works
    6. 9.6. Summary