Learning Scala Programming

Book description

Learn how to write scalable and concurrent programs in Scala, a language that grows with you.

About This Book

  • Get a grip on the functional features of the Scala programming language
  • Understand and develop optimal applications using object-oriented and functional Scala constructs
  • Learn reactive principles with Scala and work with the Akka framework

Who This Book Is For

This book is for programmers who choose to get a grip over Scala to write concurrent, scalable, and reactive programs. No prior experience with any programming language is required to learn the concepts explained in this book. Knowledge of any programming language would help the reader understanding concepts faster though.

What You Will Learn

  • Get to know the reasons for choosing Scala: its use and the advantages it provides over other languages
  • Bring together functional and object-oriented programming constructs to make a manageable application
  • Master basic to advanced Scala constructs
  • Test your applications using advanced testing methodologies such as TDD
  • Select preferred language constructs from the wide variety of constructs provided by Scala
  • Make the transition from the object-oriented paradigm to the functional programming paradigm
  • Write clean, concise, and powerful code with a functional mindset
  • Create concurrent, scalable, and reactive applications utilizing the advantages of Scala

In Detail

Scala is a general-purpose programming language that supports both functional and object-oriented programming paradigms. Due to its concise design and versatility, Scala's applications have been extended to a wide variety of fields such as data science and cluster computing. You will learn to write highly scalable, concurrent, and testable programs to meet everyday software requirements.

We will begin by understanding the language basics, syntax, core data types, literals, variables, and more. From here you will be introduced to data structures with Scala and you will learn to work with higher-order functions. Scala's powerful collections framework will help you get the best out of immutable data structures and utilize them effectively. You will then be introduced to concepts such as pattern matching, case classes, and functional programming features. From here, you will learn to work with Scala's object-oriented features. Going forward, you will learn about asynchronous and reactive programming with Scala, where you will be introduced to the Akka framework. Finally, you will learn the interoperability of Scala and Java.

After reading this book, you'll be well versed with this language and its features, and you will be able to write scalable, concurrent, and reactive programs in Scala.

Style and approach

This book is for programmers who want to master Scala to write concurrent, scalable, and reactive programs. Though no experience with any programming language is needed, some basic knowledge would help understand concepts faster.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Publisher resources

Download Example Code

Table of contents

  1. Getting Started with Scala Programming
    1. Introduction to Scala
      1. A programming paradigm
      2. Object-oriented versus functional paradigms 
      3. Scala is multi-paradigm
    2. Scala advantages
      1. Runs on JVM
      2. Super smart syntax
      3. Best of both worlds
      4. Type is the core
      5. Concurrency made easy
      6. Asynchronous code
        1. Asynchronous versus parallel versus concurrent programming
      7. Now available for the frontend
      8. Smart IDEs
      9. Extensive language
      10. Online support
    3. Working with Scala
      1. Java installation
      2. SBT installation
      3. Scala REPL
      4. Scala IDEs
    4. Running our first program
    5. Summary
  2. Building Blocks of Scala
    1. What is underneath a Scala program?
    2. Vals and vars
    3. Literals
      1. Integer literals
      2. Floating point literals
      3. Boolean literals
      4. Character literals
      5. String literals
      6. Symbol literals
      7. Tuple literals
      8. Function literals
    4. Data types
      1. Scala's class hierarchy
      2. Any
        1. AnyVal and AnyRef
          1. AnyRef
          2. AnyVal
      3. Unit
      4. Boolean
      5. Null and Nothing
    5. Type inference
    6. Operators in Scala
      1. Arithmetic operators
      2. Relational operators
      3. Logical operators
      4. Bitwise operators
      5. Operator precedence
    7. Wrapper classes
    8. String Interpolators
      1. The s interpolator
      2. f Interpolator
      3. The raw interpolator
    9. Summary
  3. Shaping our Scala Program
    1. Looping
      1. The for loop
      2. The while Loop
      3. The do while loop
    2. The for expressions
      1. The for yield expressions
    3. Recursion
      1. Why recursion over iteration?
      2. Limitations of recursion
      3. The ideal way to write recursive functions
    4. Conditional statements
      1. The if else conditional expression
    5. Pattern matching
    6. Summary
  4. Giving Meaning to Programs with Functions
    1. Function syntax
      1. Nesting of functions
    2. Calling a function
      1. Passing a variable number of arguments
      2. Calling a function with a default parameter value
      3. Calling a function while passing named arguments
    3. Function literals
    4. Evaluation strategies
      1. Call by name
      2. Call by value
    5. Partial functions
    6. Summary
  5. Getting Familiar with Scala Collections
    1. Motivation
    2. Immutable and mutable collections
      1. Differences between the root and immutable collections
    3. Hierarchy of collections in Scala
      1. Traversable
      2. Iterable
        1. Seq
          1. Sequences
        2. Map
        3. Set
    4. Commonly used collections in Scala
      1. List
      2. Map
      3. SortedSet
      4. Streams
      5. Vector
      6. Immutable stack
      7. Immutable queue
      8. Ranges
      9. ArrayBuffer
      10. ListBuffer
      11. StringBuilder
      12. Array
    5. Rich operations performed on collections
    6. Parallel collections in Scala
      1. ParArray
      2. ParVector
    7. Converting a Java collection into a Scala collection
    8. Choosing a collection
    9. Collection performance
    10. Summary
  6. Object-Oriented Scala Basics
    1. Classes
    2. Abstract classes
      1. Abstract classes and traits
      2. The final classes
    3. Objects as singletons
    4. Companion objects
    5. Case classes
    6. Summary
  7. Next Steps in Object-Oriented Scala
    1. Composition and inheritance
    2. Class inheritance
      1. Extending classes
      2. Subtyping versus subclassing
      3. Overriding data and behavior
      4. Restricting inheritance – final keyword
      5. Dynamic binding in function invocation
      6. Misusing inheritance
    3. Default and parameterized constructors
    4. Traits
    5. Traits as mix-ins
      1. Traits as composable mix-ins
      2. Traits as stackable modifications
    6. Linearization
    7. Packaging and importing
      1. Package statements
      2. Multiple package statements in a file
      3. Nested package statements
      4. Chaining package statements
      5. Importing packages
    8. Visibility rules
    9. Sealed traits
    10. Summary
  8. More on Functions
    1. Function literals
    2. Methods
    3. Functions versus methods
      1. Methods or functions?
    4. What are closures?
    5. Higher-order functions
    6. Currying
      1. Converting a function with multiple parameters to curried form
    7. Partially applied functions
    8. Summary
  9. Using Powerful Functional Constructs
    1. For expressions
    2. Pattern matching
    3. Different ways we can pattern match
      1. Matching a variable
      2. Matching a constant
      3. Matching a constructor
    4. Option type
    5. Lazy declaration
    6. Tail call optimization
    7. Combinators
    8. Type parameterization
    9. Summary
  10. Advanced Functional Programming
    1. Why so serious about types?
    2. Here comes type parameterization
    3. Another way around - generic classes and traits
    4. Type parameter names
    5. Container types
    6. Type erasure
    7. Variance under inheritance
      1. When to use what type of variance relation
    8. Abstract types
    9. Type bounds
    10. Abstract versus parameterized types
    11. Type-classes
    12. Summary
  11. Working with Implicits and Exceptions
    1. Exception handling – the old way
    2. Using the Option way
    3. Either left or right
    4. Implicits - what and why
    5. Implicit parameters
    6. The implicitly method
    7. Implicit conversions
    8. Looking for implicits
    9. Type-classes ahead!
    10. Summary
  12. Introduction to Akka
    1. Why do we care about Akka?
    2. What's up with the Actor Model?
    3. Understanding the Actor system
    4. Props
    5. Actor references and paths
    6. Selecting existing actorRefs via actorSelection
    7. How the Actor life cycle works
    8. Hello world in Akka
      1. Setting up the environment
    9. Writing our first Actor
    10. The tell versus ask versus forward method
    11. Stopping Actors
    12. The preStart and postStop hooks
    13. Actor communication via messages and its semantics
    14. Supervising fault in our actors
    15. OneForOne versus AllForOne strategy
    16. Default supervision strategy
    17. Applying the supervision strategy
    18. Testing actors
    19. Summary
  13. Concurrent Programming in Scala
    1. Concurrent programming
    2. Building blocks of concurrency
      1. Understanding processes and threads
      2. Locks and synchronization
      3. Executor and ExecutionContext
    3. Asynchronous programming
      1. Working with Futures
      2. What if Future computations go wrong?
      3. Why not compose two or more Futures?
      4. Working with Promises
    4. Parallel collections
    5. Summary
  14. Programming with Reactive Extensions
    1. Reactive programming
    2. Reactive extensions
    3. React to RxScala
      1. Creating Observables
    4. Summary
  15. Testing in Scala
    1. The why and what of TDD
      1. The process of TDD
        1. Step 1 - write a test that will fail
        2. Step 2 - write code to pass the failing test
        3. Step 3 - refactor the code to improve the quality
        4. Step 4 - repeat steps 1 to 3
          1. Scenario
      2. Behavior driven development (BDD)
    2. ScalaTest
      1. Setting up for testing
      2. Testing in style using ScalaTest
        1. Assertions
        2. Matchers
    3. ScalaMock – a native library to mock objects
    4. Summary
  16. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Learning Scala Programming
  • Author(s): Vikash Sharma
  • Release date: January 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788392822