Professional Scala

Book description

If your application source code is overly verbose, it can be a nightmare to maintain. Write concise and expressive, type-safe code in an environment that lets you build for the JVM, browser, and more.

Key Features

  • Expert guidance that shows you to efficiently use both object-oriented and functional programming techniques
  • Understand functional programming libraries, such as Cats and Scalaz, and use them to augment your Scala development
  • Perfectly balances theory and hands-on exercises, assessments, and activities

Book Description

This book teaches you how to build and contribute to Scala programs, recognizing common patterns and techniques used with the language. You'll learn how to write concise, functional code with Scala. After an introduction to core concepts, syntax, and writing example applications with scalac, you'll learn about the Scala Collections API and how the language handles type safety via static types out-of-the-box. You'll then learn about advanced functional programming patterns, and how you can write your own Domain Specific Languages (DSLs). By the end of the book, you'll be equipped with the skills you need to successfully build smart, efficient applications in Scala that can be compiled to the JVM.

What you will learn

  • Understand the key language syntax and core concepts for application development
  • Master the type system to create scalable type-safe applications while cutting down your time spent debugging
  • Understand how you can work with advanced data structures via built-in features such as the Collections library
  • Use classes, objects, and traits to transform a trivial chatbot program into a useful assistant
  • Understand what are pure functions, immutability, and higher-order functions
  • Recognize and implement popular functional programming design patterns

Who this book is for

This is an ideal book for developers who are looking to learn Scala, and is particularly well suited for Java developers looking to migrate across to Scala for application development on the JVM.

Publisher resources

View/Submit Errata

Table of contents

  1. Professional Scala
    1. Table of Contents
    2. Professional Scala
      1. Why Subscribe?
      2. PacktPub.com
    3. Contributors
      1. About the Authors
    4. Preface
      1. Who This Book Is For
      2. What This Book Covers
        1. What You Need for This Book
      3. Conventions
      4. Installation and Setup
      5. Installing IntelliJ IDE
      6. Installing Scala plugin (make heading)
      7. Reader Feedback
      8. Customer Support
        1. Downloading the Example Code
        2. Errata
        3. Piracy
        4. Questions
    5. 1. Setting up the Development Environment
      1. Simple Program
        1. Definitions: Packages, Imports, and Objects
          1. Object Definition
          2. The main Method
        2. Inside main
      2. Structure of a Scala Project
        1. Basic sbt Commands
        2. Activity: Performing Basic Operations with sbt: Build, Run, Package
        3. IDE
        4. Activity: Loading and Running a Sample Project in the IDE
        5. REPL
        6. Obtaining the Time Request from Our Chatbot Program
      3. Base Syntax
        1. Base Syntax for Definitions
        2. Base Syntax for Expressions
      4. Unit Testing
        1. Adding a Test to Our Project
        2. Inside Tests
        3. Running Tests for Chatbot
      5. Summary
    6. 2. Basic Language Features
      1. Objects, Classes, and Traits
        1. Object
        2. Classes
        3. Equality and Case Classes
        4. Pattern Matching
        5. Traits
        6. Self-Types
        7. Special Classes
      2. OO in Our Chatbot
        1. Decoupling Logic and Environment
        2. Sealed Traits and Algebraic Datatypes
      3. Function Calls
        1. Syntax Goodies
          1. Named Parameters
          2. Default Parameters
          3. Currying Forms (Multiple Argument Lists)
          4. Special Magic Methods
        2. Implementing + in CartesianPoint
        3. Parameter-Passing Mode
        4. By Value
        5. By Name
        6. By Need
        7. Creating a Runnable Construction
        8. Printing the log Argument to the Console and File
      4. Summary
    7. 3. Functions
      1. Functions
        1. Function Values
        2. Function from an OO Point of View
        3. Conversions
        4. Defining and Measuring the Time of a Unit Function
        5. Syntax Sugar in a Function Definition
        6. Partial Functions
      2. Exploring Pattern Matching
        1. Binding a Sequence of Variables in the Pattern Matcher
      3. Partial Functions in Practice
        1. Representing ChatbotMode as a Partial Function
        2. Implementing RemindStore as a Set of Partial Functions
        3. Using Lifting for Conversations between Total and Partial Functions
      4. Summary
    8. 4. Scala Collections
      1. Working with Lists
        1. Constructing Lists
        2. Operations on Lists
        3. Pattern Matching on Lists
        4. First-Order Methods on List
        5. Appending and Concatenation
        6. Taking the Length of a List
        7. Reversing a List
        8. Prefixes and Suffixes
        9. Element Selection
        10. Display
        11. Activity: Creating a New Mode for Chatbot Using Lists
      2. Abstracting on Sequences
        1. The Traversable Trait
        2. Iterators
        3. Streams
        4. Activity: Implementing Fibonacci Series Using Streams and Iterators
      3. Other Collections
        1. Sets
        2. Tuples
        3. Maps
        4. Mutable and Immutable Collections
        5. Activity: Implementing the Tower of Hanoi Problem
      4. Summary
    9. 5. Scala Type System
      1. Type Basics and Polymorphism
        1. A Unified Type System
        2. Parametric Polymorphism
        3. Type Inference
        4. Parameterized Types
        5. Bounds
        6. Existential Types
        7. Activity: Generalizing the Implementation of the Binary Tree
      2. Variance
        1. Covariance
        2. Contravariance
        3. Invariance
        4. Activity: Implementing Covariance and the Database for Tools
      3. Advanced Types
        1. Abstract Type Members
        2. Structural Types
        3. Option
        4. Higher Kind Types
        5. Type Erasure
        6. Activity: Finding an Element Based on a Given Predicate
      4. Summary
    10. 6. Implicits
      1. Implicit Parameters and Implicit Conversions
        1. Implicit Parameters
        2. Implicit Conversions
        3. Implicit Resolution
        4. Activity: Creation of Extension Methods
      2. Ad Hoc Polymorphism and Type Classes
        1. Types of Polymorphism
        2. Type Classes
        3. Context Bounds and Implicitly
        4. Type Classes in the Standard Library
          1. Activity: Implementing Type Classes to Support Conversion
      3. Summary
    11. 7. Functional Idioms
      1. Introduction to Functional Programming Concepts
        1. Pure Functions
        2. Immutability
          1. Implementing the standard library
        3. Higher-Order Functions
      2. Functional Design Patterns
        1. Monoids
          1. Using Monoids to Write Polymorphic Functions
        2. Functor
        3. Monads
      3. Popular Libraries
        1. Validating Data Using Cats
        2. Prerequisites for Using Cats
        3. Introduction to Cats
        4. Validating Data
        5. Validating Using Validated
        6. Communicating with Databases Using Doobie
          1. Prerequisites for Doobie
        7. Doobie
          1. ConnectionIO
          2. Transactor
          3. Selecting Rows
          4. Querying Using Parameters
          5. Deleting, Inserting, and Updating Rows
          6. A Complete Example
        8. Activity: Adding Priority to the Todo List
      4. Summary
    12. 8. Domain Specific Languages
      1. DSLs and Types of DSLs
        1. External DSLs
        2. Internal DSLs
      2. ScalaTest – A Popular DSL
        1. Adding ScalaTest to Your Project
        2. Overview of ScalaTest Styles
        3. Activity: Implementing ScalaTest Styles
      3. Language Features for Writing DSLs
        1. Flexible Syntax for Method Invocation
        2. By-Name Parameters
        3. Extension Methods and Value Classes
      4. Writing a Small DSL
        1. Modeling Test Cases
        2. DSL for TestCase
        3. Activity: Creating a DSL to Specify Assertions
      5. Beyond This Book
        1. Various Scala Libraries
          1. Akka
          2. Apache Spark
          3. Shapeless
        2. Uncovered Language Features
          1. Macros
          2. Def Macros
          3. Implicit Macros
          4. Quasiquotes
          5. Reflection
        3. Resources to Keep You Updated
          1. Scala Improvement Process
          2. Scala Times
      6. Summary
    13. Index

Product information

  • Title: Professional Scala
  • Author(s): Mads Hartmann, Ruslan Shevchenko
  • Release date: July 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789533835