Get Programming with Scala

Book description

The perfect starting point for your journey into Scala and functional programming.

In Get Programming in Scala you will learn:

  • Object-oriented principles in Scala
  • Express program designs in functions
  • Use types to enforce program requirements
  • Use abstractions to avoid code duplication
  • Write meaningful tests and recognize code smells

Scala is a multi-style programming language for the JVM that supports both object-oriented and functional programming. Master Scala, and you'll be well-equipped to match your programming approach to the type of problem you're dealing with. Packed with examples and exercises, Get Programming with Scala is the perfect starting point for developers with some OO knowledge who want to learn Scala and pick up a few FP skills along the way.

About the Technology
Scala developers are in high demand. This flexible language blends object-oriented and functional programming styles so you can write flexible, easy-to-maintain code. Because Scala runs on the JVM, your programs can interact seamlessly with Java libraries and tools. If you’re comfortable writing Java, this easy-to-read book will get you programming with Scala fast.

About the Book
Get Programming with Scala is a fast-paced introduction to the Scala language, covering both Scala 2 and Scala 3. You’ll learn through lessons, quizzes, and hands-on projects that bring your new skills to life. Clear explanations make Scala’s features and abstractions easy to understand. As you go, you’ll learn to write familiar object-oriented code in Scala and also discover the possibilities of functional programming.

What's Inside
  • Apply object-oriented principles in Scala
  • Learn the core concepts of functional programming
  • Use types to enforce program requirements
  • Use abstractions to avoid code duplication
  • Write meaningful tests and recognize code smells


About the Reader
For developers who know an OOP language like Java, Python, or C#. No experience with Scala or functional programming required.

About the Author
Daniela Sfregola is a Senior Software Engineer and a Scala user since 2013. She is an active contributor to the Scala Community, a public speaker at Scala conferences and meetups, and a maintainer of open-source projects.

Quotes
This book is by far the best introduction to Scala that I have ever read.
- SCOTT DIERBECK, MINDUP

A refreshing approach to learning Scala!
- CHAD DAVIS, LUMEN TECHNOLOGIES, INC.

Simple and clear explanations with practical examples make this book a smart choice.
- DAN SHEIKH, BCG DIGITAL VENTURES

A pragmatic, clean, structured, concise, and practical book.
- RONALD TISCHLIAR, WWK LIVE INSURANCE

A well-paced introduction to Scala served up in compact palatable doses.
- GEORGE THOMAS, MANHATTAN ASSOCIATES

Publisher resources

View/Submit Errata

Table of contents

  1. inside front cover
  2. Get Programming with Scala
  3. Copyright
  4. dedication
  5. Contents
  6. front matter
    1. Preface
    2. Acknowledgments
    3. About this book
      1. Who should read this book
      2. How this book is organized: A roadmap
      3. About the code
      4. liveBook discussion forum
    4. About the author
  7. Unit 0. Hello Scala!
  8. 1 Why Scala?
    1. 1.1 Why Scala?
    2. 1.2 Scala and the JVM
    3. 1.3 Scala’s key features
    4. Summary
  9. 2 The Scala environment
    1. 2.1 The REPL installation
    2. 2.2 The REPL commands
    3. 2.3 The REPL code evaluation
    4. 2.4 Other development tools
      1. 2.4.1 Git installation
      2. 2.4.2 Docker installation
    5. Summary
  10. 3 Scala Build Tool (sbt)
    1. 3.1 Why sbt?
    2. 3.2 sbt installation
    3. 3.3 sbt commands
    4. 3.4 Your first sbt project
    5. 3.5 sbt project structure
    6. Summary
  11. Unit 1. The Basics
  12. 4 Values and variables
    1. 4.1 Values
    2. 4.2 Variables
    3. Summary
    4. Answers to quick checks
  13. 5 Conditional constructs and loops
    1. 5.1 If-else construct
    2. 5.2 While loop
    3. 5.3 For loop
    4. Summary
    5. Answers to quick checks
  14. 6 Function as the most fundamental block of code
    1. 6.1 Functions
    2. Summary
    3. Answers to quick checks
  15. 7 Classes and subclasses to represent the world
    1. 7.1 Class
    2. 7.2 Subclass
    3. 7.3 Abstract class
    4. Summary
    5. Answers to quick checks
  16. 8 The vending machine
    1. 8.1 Setting up the vending machine
      1. 8.1.1 The VendingMachine class and its APIs
      2. 8.1.2 The vending machine and its operations
      3. 8.1.3 Let’s try it out
    2. 8.2 Possible improvements to our solution
    3. Summary
  17. Unit 2. Object-oriented fundamentals
  18. 9 Import and create packages
    1. 9.1 Import an existing package
    2. 9.2 Create a package
    3. Summary
    4. Answers to quick checks
  19. 10 Scope your code with access modifiers
    1. 10.1 Public, the default access modifier
    2. 10.2 Private
    3. 10.3 Protected
    4. 10.4 Which access level to use?
    5. Summary
    6. Answers to quick checks
  20. 11 Singleton objects
    1. 11.1 Object
    2. 11.2 Executable object
    3. 11.3 Companion object
    4. 11.4 The apply method
    5. Summary
    6. Answers to quick checks
  21. 12 Traits as interfaces
    1. 12.1 Defining traits
    2. 12.2 Extending traits
    3. 12.3 Sealed traits
    4. 12.4 Enumeration in Scala 3
    5. Summary
    6. Answers to quick checks
  22. 13 What time is it?
    1. 13.1 What time is it?
      1. 13.1.1 sbt project setup
      2. 13.1.2 The business logic layer
      3. 13.1.3 The TimeApp executable object
      4. 13.1.4 Let’s try it out!
    2. 13.2 Possible improvements to our solution
    3. Summary
  23. Unit 3. HTTP server
  24. 14 Pattern matching
    1. 14.1 If-else construct vs. pattern matching
    2. 14.2 Sealed pattern matching
    3. Summary
    4. Answers to quick checks
  25. 15 Anonymous functions
    1. 15.1 Function vs. anonymous function
    2. 15.2 Concise notation for anonymous functions
    3. Summary
    4. Answers to quick checks
  26. 16 Partial functions
    1. 16.1 Partial functions
      1. 16.1.1 Implementing a partial function
      2. 16.1.2 Function composition
    2. 16.2 Use case: Exception handling
    3. Summary
    4. Answers to quick checks
  27. 17 HTTP API with http4s
    1. 17.1 An overview of http4s
    2. 17.2 A ping server using http4s
      1. 17.2.1 Initial setup
      2. 17.2.2 Implementing the API
      3. 17.2.3 Building a server
      4. 17.2.4 Let’s try it out!
    3. Summary
    4. Answers to quick checks
  28. 18 The time HTTP server
    1. 18.1 What time is it?
      1. 18.1.1 Setting your sbt project up
      2. 18.1.2 The TimePrinter class
      3. 18.1.3 The API routes
      4. 18.1.4 The HTTP server
      5. 18.1.5 Let’s try it out!
    2. 18.2 Possible improvements to our solution
    3. Summary
  29. Unit 4. Immutable data and structures
  30. 19 Case classes to structure your data
    1. 19.1 Case class
    2. 19.2 Pattern matching and case classes
    3. 19.3 Case object
    4. Summary
    5. Answers to quick checks
  31. 20 Higher order functions
    1. 20.1 Functions as parameters
    2. 20.2 Functions as return values
    3. Summary
    4. Answers to quick checks
  32. 21 What is purity?
    1. 21.1 A definition of purity
    2. 21.2 Differentiating between pure and impure functions
    3. Summary
    4. Answers to quick checks
  33. 22 Option
    1. 22.1 Why Option?
    2. 22.2 Creating an Option
    3. 22.3 Pattern matching on Option
    4. Summary
    5. Answers to quick checks
  34. 23 Working with Option: map and flatMap
    1. 23.1 Transforming an Option
      1. 23.1.1 The map function
      2. 23.1.2 The flatten function
      3. 23.1.3 The flatMap function
    2. Summary
    3. Answers to quick checks
  35. 24 Working with Option: For-comprehension
    1. 24.1 For-comprehension on Option
      1. 24.1.1 For-comprehension as syntactic sugar for nested map and flatMap calls
      2. 24.1.2 Filtering values within for-comprehension
    2. 24.2 Other operations on Option
    3. Summary
    4. Answers to quick checks
  36. 25 Tuple and unapply
    1. 25.1 Tuples
    2. 25.2 Implementing the unapply method
    3. Summary
    4. Answers to quick checks
  37. 26 Rock, Paper, Scissors, Lizard, Spock!
    1. 26.1 Implementing Rock, Paper, Scissors, Lizard, Spock!
      1. 26.1.1 sbt project setup and packages
      2. 26.1.2 Defining a symbol
      3. 26.1.3 Representing a player
      4. 26.1.4 Defining a game
      5. 26.1.5 The API routes
      6. 26.1.6 The HTTP server
      7. 26.1.7 Let’s try it out!
    2. 26.2 Possible improvements to our solution
    3. Summary
  38. Unit 5. List
  39. 27 List
    1. 27.1 Creating a list
    2. 27.2 Adding elements to a list
    3. 27.3 Pattern matching on a list
    4. Summary
    5. Answers to quick checks
  40. 28 Working with List: map and flatMap
    1. 28.1 The map, flatten, and flatMap operations
      1. 28.1.1 The map function
      2. 28.1.2 The flatten function
      3. 28.1.3 The flatMap function
    2. 28.2 For-comprehension
    3. Summary
    4. Answers to quick checks
  41. 29 Working with List: Properties
    1. 29.1 Size of a list
    2. 29.2 Properties of the elements in a list
    3. Summary
    4. Answers to quick checks
  42. 30 Working with List: Element selection
    1. 30.1 Selecting an element by its position
    2. 30.2 Finding an element with given features
    3. 30.3 Picking the minimum or maximum item
    4. Summary
    5. Answers to quick checks
  43. 31 Working with List: Filtering
    1. 31.1 Dropping and taking elements
    2. 31.2 Filtering Items of a list
    3. 31.3 Removing duplicates
    4. Summary
    5. Answers to quick checks
  44. 32 Working with List: Sorting and other operations
    1. 32.1 Sorting elements
    2. 32.2 Converting a list to a string
    3. 32.3 Sum elements of numerical sequences
    4. 32.4 Grouping elements by feature
    5. Summary
    6. Answers to quick checks
  45. 33 The movies dataset
    1. 33.1 Download the base project
    2. 33.2 Parsing a movie entity
    3. 33.3 Printing query results
    4. 33.4 Querying the movie data set
    5. Summary
  46. Unit 6. Other collections and error handling
  47. 34 Set
    1. 34.1 Creating a set
    2. 34.2 Adding and removing elements
    3. 34.3 The map, flatten, and flatMap operations
      1. 34.3.1 The map function
      2. 34.3.2 The flatten function
      3. 34.3.3 The flatMap function
    4. 34.4 For-comprehension
    5. Summary
    6. Answers to quick checks
  48. 35 Working with Set
    1. 35.1 The Union, Intersection, and Difference operations
    2. 35.2 Other operations on Set
    3. Summary
    4. Answers to quick checks
  49. 36 Map
    1. 36.1 Creating Map
    2. 36.2 Adding and removing elements
    3. 36.3 Merge and remove multiple entries
    4. 36.4 The map and flatMap operations
      1. 36.4.1 The map function
      2. 36.4.2 The flatMap function
    5. 36.5 For-comprehension
    6. Summary
    7. Answers to quick checks
  50. 37 Working with Map
    1. 37.1 Retrieving a value for a given key
    2. 37.2 Getting all keys and values
    3. 37.3 Other operations on Map
    4. Summary
    5. Answers to quick checks
  51. 38 Either
    1. 38.1 Why Either?
    2. 38.2 Creating an Either
    3. 38.3 Pattern matching on Either
    4. 38.4 The map and flatMap operations
      1. 38.4.1 The map function
      2. 38.4.2 The flatMap function
    5. 38.5 For-comprehension
    6. Summary
    7. Answers to quick checks
  52. 39 Working with Either
    1. 39.1 Retrieving an Either value
    2. 39.2 Properties of an Either value
    3. Summary
    4. Answers to quick checks
  53. 40 Error handling with Try
    1. 40.1 Creating a Try
    2. 40.2 Pattern matching on Try
    3. 40.3 The map, flatten, and flatMap operations
      1. 40.3.1 The map function
      2. 40.3.2 The flatten function
      3. 40.3.3 The flatMap function
    4. 40.4 For-comprehension
    5. 40.5 Other operations on Try
    6. Summary
    7. Answers to quick checks
  54. 41 The library application
    1. 41.1 Download the base project
    2. 41.2 Parsing a book entity
    3. 41.3 The business logic layer
      1. 41.3.1 Performing a book search
      2. 41.3.2 Reserving a book
      3. 41.3.3 Returning a book
    4. 41.4 Let’s give it a try!
    5. 41.5 Possible improvements to our solution
    6. Summary
  55. Unit 7. Concurrency
  56. 42 Implicit and type classes
    1. 42.1 Implicit parameters and values
    2. 42.2 Implicit resolution
    3. 42.3 Type classes
    4. Summary
    5. Answers to quick checks
  57. 43 Future
    1. 43.1 Why Future?
    2. 43.2 Creating an instance of Future
    3. 43.3 Processing Future on completion
    4. Summary
    5. Answers to quick checks
  58. 44 Working with Future: map and flatMap
    1. 44.1 The map, flatten, and flatMap operations
      1. 44.1.1 The map function
      2. 44.1.2 The flatten function
      3. 44.1.3 The flatMap function
    2. Summary
    3. Answers to quick checks
  59. 45 Working with Future: For-comprehension and other operations
    1. 45.1 For-comprehension
    2. 45.2 Retrieving the first Future to complete
    3. Summary
    4. Answers to quick checks
  60. 46 Database queries with Quill
    1. 46.1 Project setup
      1. 46.1.1 Download the base project
      2. 46.1.2 Starting the PostgreSQL server
    2. 46.2 Connecting to the PostgreSQL server
    3. 46.3 Executing queries
    4. 46.4 Running generated queries
    5. Summary
    6. Answers to quick checks
  61. 47 The quiz application: Part 1
    1. 47.1 Download the base project
    2. 47.2 Health check queries
    3. 47.3 Category queries
    4. 47.4 Question and answer queries
    5. 47.5 Let’s give it a try!
    6. Summary
  62. Unit 8. JSON (de)serialization
  63. 48 JSON (de)serialization with circe
    1. 48.1 Project setup
    2. 48.2 JSON serialization: From instance to JSON
    3. 48.3 JSON deserialization: From JSON to instance
    4. Summary
    5. Answers to quick checks
  64. 49 Lazy evaluation
    1. 49.1 By-name parameters
    2. 49.2 Lazy values
    3. Summary
    4. Answers to quick checks
  65. 50 The IO type
    1. 50.1 Why IO?
    2. 50.2 Project setup
    3. 50.3 Synchronous side effect
    4. 50.4 Asynchronous side effect
    5. Summary
    6. Answers to quick checks
  66. 51 Working with the IO type
    1. 51.1 The map and flatMap operations
      1. 51.1.1 The map function
      2. 51.1.2 The flatMap function
    2. 51.2 For-comprehension
    3. 51.3 Parallel execution
    4. Summary
    5. Answers to quick checks
  67. 52 Testing with ScalaTest
    1. 52.1 Project setup
    2. 52.2 Your first test
    3. 52.3 Asynchronous testing
    4. Summary
    5. Answers to quick checks
  68. 53 The quiz application: Part 2
    1. 53.1 Download the base project
    2. 53.2 Generic endpoints
    3. 53.3 Displaying the available categories
    4. 53.4 Creating a quiz
    5. 53.5 Answering a quiz
    6. 53.6 The HTTP server
    7. 53.7 Writing tests
    8. 53.8 Let’s give it a try!
    9. Summary
  69. Index
  70. inside back cover

Product information

  • Title: Get Programming with Scala
  • Author(s): Daniela Sfregola
  • Release date: September 2021
  • Publisher(s): Manning Publications
  • ISBN: 9781617295270