Scala is now an established programming language developed by Martin Oderskey and his team at the EPFL. The name Scala is derived from Sca(lable) La(nguage). Scala is a multi-paradigm language, incorporating object oriented approaches with functional programming.
Although some familiarity with standard computing concepts is assumed (such as the idea of compiling a program and executing this compiled from etc.) and with basic procedural language concepts (such as variables and allocation of values to these variables) the early chapters of the book do not assume any familiarity with object orientation nor with functional programming These chapters also step through other concepts with which the reader may not be familiar (such as list processing).
From this background, the book provides a practical introduction to both object and functional approaches using Scala. These concepts are introduced through practical experience taking the reader beyond the level of the language syntax to the philosophy and practice of object oriented development and functional programming.
Students and those actively involved in the software industry will find this comprehensive introduction to Scala invaluable.
Author(s): John Hunt
Publisher: Springer
Year: 2018
Contents
1 Introduction
1.1 Introduction
1.2 What Is Scala?
1.3 Why Scala?
1.4 Java to Scala Quick Comparison
1.5 Scala Versions
1.6 Is This Book for You?
1.7 Approach Taken by This Book
2 Elements of Object Orientation
2.1 Introduction
2.2 Terminology
2.3 Types of Hierarchy
2.4 The Move to Object Technology
2.5 Summary
2.6 Exercises
2.7 Further Reading
3 Why Object Orientation?
3.1 Introduction
3.2 The Procedural Approach
3.2.1 A Naked Data Structure
3.2.2 Procedures for the Data Structure
3.2.3 Packages
3.3 Does Object Orientation Do Better?
3.3.1 Packages Versus Classes
3.3.2 Inheritance
3.4 Summary
4 Constructing an Object-Oriented System
4.1 Introduction
4.2 The Application: Windscreen Wipe Simulation
4.3 Where Do We Start?
4.4 Identifying the Objects
4.5 Identifying the Services or Methods
4.6 Refining the Objects
4.7 Bringing It All Together
4.8 Where Is the Structure?
4.9 Summary
4.10 Exercises
4.11 Further Reading
References
5 Functional Programming
5.1 Introduction
5.2 What Is Functional Programming?
5.3 Advantages to Functional Programming
5.4 Disadvantages of Functional Programming
5.5 Scala and Functional Programming
6 Scala Background
6.1 Introduction
6.2 The Class Person
6.3 Functional Programming
6.4 A Hybrid Language
7 A Little Scala
7.1 Introduction
7.2 The Scala Environment
7.3 The Scala Shell
7.4 The Scala IDE
7.4.1 Creating an IntelliJ Project
7.4.2 Inside IntelliJ
7.4.3 Creating a Module
7.5 Implementing the Object
7.6 Running the Application
7.6.1 Scala Interpreter Console
7.7 Scala Classpath
7.8 Compiling and Executing Scala
7.9 Memory Management
7.9.1 Why Have Automatic Memory Management?
7.9.2 Memory Management in Scala
7.9.3 When Is Garbage Collection Performed?
7.9.4 Checking the Available Memory
8 Scala Building Blocks
8.1 Introduction
8.2 Apps and Applications
8.3 The Basics of the Language
8.3.1 Some Terminology
8.3.2 The Message Passing Mechanism
8.3.3 The Statement Terminator
9 Scala Classes
9.1 Introduction
9.2 Classes
9.2.1 Class Definitions
9.2.2 Developing a Class Definition
9.2.3 Classes and Messages
9.2.4 Instances and Instance Variables
9.2.5 Classes and Inheritance
9.2.5.1 An Example of Inheritance
9.2.5.2 The Yo-Yo Problem
9.2.6 Instance Creation
9.2.7 Constructors
9.2.8 Auxiliary Constructors
9.2.9 Class Initialisation Behaviour
9.2.10 Review of Classes and Constructors
9.3 Case Classes
9.3.1 A Sample Class
10 Scala Methods
10.1 Introduction
10.2 Method Definitions
10.2.1 Method Parameters
10.2.2 Comments
10.2.3 The Local Variables Section
10.2.4 The Statements Section
10.2.5 The Return Operator
10.2.6 An Example Method
10.2.7 Overriding toString
10.2.8 Defining Property Methods
10.3 Named Parameters
11 Packages and Encapsulation
11.1 Introduction
11.2 Packages
11.2.1 Declaring a Package
11.2.2 Additional Package Definitions Options
11.2.2.1 Package Per File
11.2.2.2 Chain Package Definitions
11.2.2.3 Nested Package Definitions
11.2.3 An Example Package
11.2.4 Accessing Package Elements
11.2.5 An Example of Using a Package
11.3 Import Options
11.4 Additional Import Features
11.5 Package Objects
11.6 Key Scala Packages
11.7 Default Imports
11.8 Encapsulation
11.8.1 Scala Visibility Modifiers
11.8.2 Private Modified
11.8.3 Protected Modifier
12 Building a Class
12.1 Introduction
12.2 Create a New Module
12.3 Create a New Package
12.4 Create a New Class
12.5 Defining the Class
12.6 Adding Behaviour
12.7 Test Application
12.8 Override Tostring
12.9 Extras
13 Classes, Inheritance and Abstraction
13.1 Introduction
13.1.1 What Are Classes for?
13.2 Inheritance Between Types
13.3 Inheritance Between Classes
13.3.1 The Role of a Subclass
13.3.2 Capabilities of Classes
13.3.3 Overriding Behaviour
13.3.4 Protected Members
13.4 Restricting a Subclass
13.5 Abstract Classes
13.6 The Super Keyword
13.7 Scala Type Hierarchy
13.8 Polymorphism
14 Objects and Instances
14.1 Introduction
14.2 Singleton Objects
14.3 Companion Objects
14.3.1 Companion Object Behaviour
14.3.2 A Object or an Instance
15 Value Classes
15.1 Introduction
15.2 Value Classes
15.3 Simple Value Type Example
15.4 Additional Value Class Concepts
15.5 Negating Value Classes
16 Scala Constructs
16.1 Introduction
16.2 Numbers and Numeric Operators
16.2.1 Numeric Values
16.2.2 Arithmetic Operators
16.3 Characters and Strings
16.3.1 Characters
16.3.2 Strings
16.4 Assignments
16.5 Variables
16.5.1 Temporary Variables
16.5.2 Pseudo-Variables
16.5.3 Variable Scope
16.5.4 Option, Some and None
16.5.5 Boolean Values
16.5.6 Literals
16.6 Messages and Message Selectors
16.6.1 Invoking Methods
16.6.2 Precedence
16.7 Summary
17 Control and Iteration
17.1 Introduction
17.2 Control Structures
17.2.1 The if Statement
17.2.2 If Returns a Value
17.3 Iteration
17.3.1 For Loops
17.3.2 For Until
17.3.3 For Loop with a Filter
17.3.4 Longhand for Loop
17.3.5 For-Yield Loop
17.3.6 While Loops
17.3.7 Do Loops
17.3.8 An Example of Loops
17.4 Equality
17.5 Recursion
17.5.1 The Match Expression
18 Traits
18.1 Introduction
18.2 What Are Traits?
18.3 Defining a Trait
18.4 Using a Trait
18.5 Abstract Trait Members
18.6 Dynamic Binding of Traits
18.7 Sealed Traits
18.8 Marker Traits
18.9 Trait Dependencies
18.10 To Trait or not to Trait
19 Further Traits
19.1 Introduction
19.2 Stackable Modifications
19.3 Fat Versus Thin Traits
19.4 Universal Traits
19.5 Traits for a Data Type
19.6 Single Abstract Method (SAM) Traits
20 Arrays
20.1 Introduction
20.2 Arrays
20.2.1 Arrays of Objects
20.2.2 Ragged Arrays
20.3 Creating Square Arrays
20.4 Looping Through Arrays
20.5 The Main Method Revisited
21 Tuples
21.1 Introduction
21.2 Tuples
21.3 Tuple Characteristics
21.4 Tuple Classes
21.5 Creating a Tuple
21.6 Working with Tuples
21.7 Iterating Over a Tuple
21.8 Element Extraction
22 Functional Programming in Scala
22.1 Introduction
22.2 Scala as a Functional Language
22.3 Defining Scala Functions
22.4 Class, Objects, Methods and Functions
22.5 Lifting a Method
22.6 Single Abstract Method Traits
22.7 Closure
22.8 Referential Transparency
23 Higher-Order Functions
23.1 Introduction
23.2 Higher-Order Function Concepts
23.3 Scala Higher-Order Functions
23.4 Using Higher-Order Functions
23.5 Higher-Order Functions in Scala Collections
24 Partially Applied Functions and Currying
24.1 Introduction
24.2 Partially Applied Functions
24.3 Currying
24.3.1 Introduction to Currying
24.3.2 Defining Multiple Parameter List Functions
24.3.3 Using Curried Functions
24.3.4 Building Domain-Specific Languages
25 Scala Collections Framework
25.1 Introduction
25.2 What Are Collections?
25.3 Scala Collections
25.3.1 Package Scala.Collection
25.3.2 Common Seq Behaviour
25.3.3 Common Set Behaviour
25.3.4 Common Map Behaviour
26 Immutable Lists and Maps
26.1 Introduction
26.2 The Immutable List Collection
26.2.1 List Creation
26.2.2 List Concatenation
26.2.3 List Operations
26.2.4 List Processing
26.2.5 Further List Processing
26.2.6 Pattern Matching
26.2.7 Converting to a List
26.2.8 Lists of User Defined Types
26.3 The Immutable Map Type
27 Immutable and Mutable Collection Packages
27.1 Introduction
27.2 Package Scala.Collection.Immutable
27.2.1 Sequences
27.2.2 Sets
27.2.3 Maps
27.3 Package Scala.Collection.Mutable
27.3.1 ArrayBuffer
27.3.2 ListBuffer
27.3.3 LinkedList
27.3.4 Stack
27.3.5 HashMap
27.4 Generic Collections
27.5 Summary
28 Type Parameterisation
28.1 Introduction
28.2 The Set Class
28.3 Adding Type Parameterisation
28.3.1 The MyQueue Mutable Class
28.3.2 The Immutable Queue Class
28.4 Variance
28.5 Lower and Upper Bounds
28.6 Combining Variance and Bounds
29 Further Language Constructs
29.1 Introduction
29.2 Implicit Conversions
29.3 Implicit Parameters
29.4 Implicit Objects
29.5 Implicit Classes
29.6 Scala Annotations
29.7 Type Declarations
29.8 Enumerations
29.9 Lazy Evaluation
30 Exception Handling
30.1 Introduction
30.2 What Is an Exception?
30.3 What Is Exception Handling?
30.4 Throwing an Exception
30.5 Catching an Exception
30.6 Try Block Returns a Value
30.7 Defining an Exception
30.8 A More Functional Approach
30.9 The Try Type
31 Akka Actors
31.1 Introduction
31.2 The Actor Model
31.3 Some Terminology
31.4 Scala Threads
31.5 Akka Scala Actor Library
31.6 Concurrent Hello World
31.7 Concurrent Actors
31.8 The Akka Actor API
31.9 Actor Lifecycle
31.10 Akka Configuration
31.11 Actor DSL
32 Further Akka Actors
32.1 Introduction
32.2 Generating a Result from an Actor
32.3 Futures
32.4 Dispatchers
32.5 Actor Hierarchies
32.6 Actor Supervision
32.7 Good Practices
33 Scala and JDBC Database Access
33.1 Introduction
33.2 Why JDBC?
33.3 What Is JDBC?
33.4 Working with JDBC
33.5 The Database Driver
33.6 Registering Drivers
33.7 Setting Up MySQL
33.8 Setting Up the Database
33.8.1 Starting/Stopping/Connecting to MySQL
33.9 Creating a Database
33.9.1 Adding a User
33.9.2 Selecting to Work with a Database
33.9.3 Creating a Table
33.9.4 Adding Data to a Table
33.10 Opening a Connection
33.11 Inserting into a Table
33.12 Obtaining Data from a Database
33.13 Update an Existing Row
33.14 Deleting from a Table
33.15 Creating a Table
33.16 Stored Procedures
33.17 JDBC Data Sources
33.18 Connection Pooling
33.19 JDBC Metadata
33.19.1 DatabaseMetaData
33.19.2 ResultSetMetaData
34 Scala Style Database Access
34.1 Introduction
34.2 Slick
34.3 Querulus
34.4 Squeryl
34.5 O/R Broker
35 Slick: Functional Relational Mapping for Scala
35.1 Introduction
35.2 Obtaining Slick
35.3 Connecting to a Database
35.4 Mapping Tables to Scala Types
35.5 Table Query Interface
35.6 Creating Tables
35.7 Inserting Data
35.8 Composing Database I/O Actions
35.9 Example Setting Up a Database
35.10 Querying for Data
35.11 Updating, Upserting and Deleting Data
36 Testing
36.1 Introduction
36.2 Types of Testing
36.3 What Should Be Tested?
36.4 Types of Testing
36.4.1 Unit Testing
36.4.2 Integration Testing
36.4.3 System Testing
36.4.4 Installation Testing
36.4.5 Smoke Tests
36.5 Automating Testing
37 Scala Testing
37.1 Introduction
37.2 Scala Runtime Test Facilities
37.2.1 Validation Checks
37.2.2 Using Require and Assert
37.3 Test Libraries in Scala
37.3.1 ScalaTest
37.3.2 Spec
37.3.3 ScalaCheck
37.4 Scalatest Testing Framework
37.4.1 Setting up Your Scala Project
37.4.2 ScalaTest and JUnit
37.4.2.1 The package
37.4.2.2 RunWith Annotation
37.4.2.3 Naming Conventions
37.4.3 Scala Test and Functional Test Suites
37.5 Scalatest and Feature Tests
37.6 Test-Driven Development
37.6.1 The TDD Cycle
37.6.2 Test Complexity
37.6.3 Refactoring
38 Play Framework
38.1 Introduction
38.2 Introduction to Play
38.2.1 Working with a Web Application
38.2.2 How Play Changes the Stack
38.3 Starting with Play
38.3.1 Download and Install Play
38.3.2 Using a Play Starter Project
38.3.3 Starting the Application
38.4 Examining the Structure of the Application
38.5 Editing the Application
38.5.1 Working with Your IDE
38.5.2 Importing into IntelliJ
38.5.3 Working with the Application
38.6 Model–View–Controller
38.7 Exploring the Play Application
39 RESTful Services
39.1 Introduction
39.2 RESTful Services
39.3 A RESTful API
39.4 Creating the RESTful Web Application
39.5 JavaScript and jQuery
39.6 The jQuery Client
39.6.1 Obtaining jQuery
39.6.2 Adding jQuery to the Application
40 Scalaz
40.1 Introduction
40.2 Obtaining Scalaz
40.3 Scalaz Overview
40.4 Some Useful Typeclasses
40.4.1 Equals Typeclass
40.4.2 Order Typeclass
40.5 Standard Class Extensions
40.5.1 Extensions to Option
40.5.2 Boolean Extensions
40.5.3 Extensions to List
40.5.4 Extensions for Map
40.5.5 Extensions to String
40.6 The Other Either
40.7 Tagging
41 GUIs in Scala Swing
41.1 Introduction
41.2 Windows as Objects
41.3 Windows in Scala
41.4 Scala Swing
41.5 Scala Swing Packages
41.6 Swing Scala Worked Examples
41.6.1 Simple Hello World UI
41.6.2 Panels and UI Layout
41.6.3 Working with a BorderPanel
41.6.4 Working with a BoxPanel
41.6.5 Displaying a Table
42 User Input in Scala Swing
42.1 Introduction
42.2 Handling User Input
42.2.1 Scala Swing Actions
42.2.2 Working with Menus
42.3 A Simple GUI Example
43 Scala Build Tools
43.1 Introduction
43.2 Why We Need a Build Tool
43.3 Maven
43.3.1 Maven Repositories
43.3.2 The Maven POM
43.3.3 Scala and Maven
43.3.4 Maven Lifecycle Commands
43.4 SBT
43.4.1 Creating an SBT Project
43.4.2 SBT Lifecycle Commands
44 Scala & Java Interoperability
44.1 Introduction
44.2 A Simple Example
44.3 Inheritance
44.4 Issues
44.4.1 Scala Objects
44.4.2 Companion Modules
44.4.3 Traits
44.5 Functions
44.6 Collection Classes
44.7 Implementing a Java Interface