Programming in Scala

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

This book is the authoritative tutorial on the Scala programming language, co-written by the language's designer, Martin Odersky. This fifth edition is a major rewrite of the entire book, adding new material to cover the many changes in Scala 3.0. In fact we have added so much new material that we split the book into two volumes. This volume is a tutorial of Scala and functional programming.

Author(s): Martin Odersky, Lex Spoon, Bill Venners, Frank Sommers
Edition: 5
Publisher: Artima Press
Year: 2021

Language: English
Commentary: Vector PDF
Pages: 668
City: Walnut Creek, CA
Tags: Programming; Multithreading; GUI; Java; Functional Programming; Scala; Object-Oriented Programming; Syntactic Analysis; Closures; Testing; XML; Metaprogramming

Contents
List of Figures
List of Tables
List of Listings
Foreword
Acknowledgments
Introduction
A Scalable Language
A language that grows on you
What makes Scala scalable?
Why Scala?
Scala's roots
Conclusion
First Steps in Scala
Learn to use the Scala REPL
Define some variables
Define some functions
Write some Scala scripts
Loop with while; decide with if
Iterate with foreach and for-do
Conclusion
Next Steps in Scala
Parameterize arrays with types
Use lists
Use tuples
Use sets and maps
Learn to recognize the functional style
Transform with map and for-yield
Conclusion
Classes and Objects
Classes, fields, and methods
Semicolon inference
Singleton objects
Case classes
A Scala application
Conclusion
Basic Types and Operations
Some basic types
Literals
String interpolation
Operators are methods
Arithmetic operations
Relational and logical operations
Bitwise operations
Object equality
Operator precedence and associativity
Rich operations
Conclusion
Functional Objects
A specification for class Rational
Constructing a Rational
Reimplementing the toString method
Checking preconditions
Adding fields
Self references
Auxiliary constructors
Private fields and methods
Defining operators
Identifiers in Scala
Method overloading
Extension methods
A word of caution
Conclusion
Built-in Control Structures
If expressions
While loops
For expressions
Exception handling with try expressions
Match expressions
Living without break and continue
Variable scope
Refactoring imperative-style code
Conclusion
Functions and Closures
Methods
Local functions
First-class functions
Short forms of function literals
Placeholder syntax
Partially applied functions
Closures
Special function call forms
``SAM'' types
Tail recursion
Conclusion
Control Abstraction
Reducing code duplication
Simplifying client code
Currying
Writing new control structures
By-name parameters
Conclusion
Composition and Inheritance
A two-dimensional layout library
Abstract classes
Defining parameterless methods
Extending classes
Overriding methods and fields
Defining parametric fields
Invoking superclass constructors
Using override modifiers
Polymorphism and dynamic binding
Declaring final members
Using composition and inheritance
Implementing above, beside, and toString
Defining a factory object
Heighten and widen
Putting it all together
Conclusion
Traits
How traits work
Thin versus rich interfaces
Traits as stackable modifications
Why not multiple inheritance?
Trait parameters
Conclusion
Packages, Imports, and Exports
Putting code in packages
Concise access to related code
Imports
Implicit imports
Access modifiers
Top-level definitions
Exports
Conclusion
Pattern Matching
A simple example
Kinds of patterns
Pattern guards
Pattern overlaps
Sealed classes
Pattern matching Options
Patterns everywhere
A larger example
Conclusion
Working with Lists
List literals
The List type
Constructing lists
Basic operations on lists
List patterns
First-order methods on class List
Higher-order methods on class List
Methods of the List object
Processing multiple lists together
Understanding Scala's type inference algorithm
Conclusion
Working with Other Collections
Sequences
Sets and maps
Selecting mutable versus immutable collections
Initializing collections
Tuples
Conclusion
Mutable Objects
What makes an object mutable?
Reassignable variables and properties
Case study: Discrete event simulation
A language for digital circuits
The Simulation API
Circuit Simulation
Conclusion
Scala's Hierarchy
Scala's class hierarchy
How primitives are implemented
Bottom types
Defining your own value classes
Intersection types
Union types
Transparent traits
Conclusion
Type Parameterization
Functional queues
Information hiding
Variance annotations
Checking variance annotations
Lower bounds
Contravariance
Upper bounds
Conclusion
Enums
Enumerated data types
Algebraic data types
Generalized ADTs
What makes ADTs algebraic
Conclusion
Abstract Members
A quick tour of abstract members
Type members
Abstract vals
Abstract vars
Initializing abstract vals
Abstract types
Path-dependent types
Refinement types
Case study: Currencies
Conclusion
Givens
How it works
Parameterized given types
Anonymous givens
Parameterized givens as typeclasses
Given imports
Rules for context parameters
When multiple givens apply
Debugging givens
Conclusion
Extension Methods
The basics
Generic extensions
Collective extensions
Using a typeclass
Extension methods for givens
Where Scala looks for extension methods
Conclusion
Typeclasses
Why typeclasses?
Context bounds
Main methods
Multiversal equality
Implicit conversions
Typeclass case study: JSON serialization
Conclusion
Collections in Depth
Mutable and immutable collections
Collections consistency
Trait Iterable
The sequence traits Seq, IndexedSeq, and LinearSeq
Sets
Maps
Concrete immutable collection classes
Concrete mutable collection classes
Arrays
Strings
Performance characteristics
Equality
Views
Iterators
Creating collections from scratch
Conversions between Java and Scala collections
Conclusion
Assertions and Tests
Assertions
Testing in Scala
Informative failure reports
Tests as specifications
Property-based testing
Organizing and running tests
Conclusion
Glossary
Bibliography
About the Authors
Index