Scala Programming Projects

Book description

Discover unique features and powerful capabilities of Scala Programming as you build projects in a wide range of domains

Key Features

  • Develop a range of Scala projects from web applications to big data analysis
  • Leverage full power of modern web programming using Play Framework
  • Build real-time data pipelines in Scala with a Bitcoin transaction analysis app

Book Description

Scala is a type-safe JVM language that incorporates object-oriented and functional programming (OOP and FP) aspects. This book gets you started with essentials of software development by guiding you through various aspects of Scala programming, helping you bridge the gap between learning and implementing. You will learn about the unique features of Scala through diverse applications and experience simple yet powerful approaches for software development.

Scala Programming Projects will help you build a number of applications, beginning with simple projects, such as a financial independence calculator, and advancing to other projects, such as a shopping application and a Bitcoin transaction analyzer. You will be able to use various Scala features, such as its OOP and FP capabilities, and learn how to write concise, reactive, and concurrent applications in a type-safe manner. You will also learn how to use top-notch libraries such as Akka and Play and integrate Scala apps with Kafka, Spark, and Zeppelin, along with deploying applications on a cloud platform.

By the end of the book, you will not only know the ins and outs of Scala, but you will also be able to apply it to solve a variety of real-world problems

What you will learn

  • Build, test, and package code using Scala Build Tool
  • Decompose code into functions, classes, and packages for maintainability
  • Implement the functional programming capabilities of Scala
  • Develop a simple CRUD REST API using the Play framework
  • Access a relational database using Slick
  • Develop a dynamic web UI using Scala.js
  • Source streaming data using Spark Streaming and write a Kafka producer
  • Use Spark and Zeppelin to analyze data

Who this book is for

If you are an amateur programmer who wishes to learn how to use Scala, this book is for you. Knowledge of Java will be beneficial, but not necessary, to understand the concepts covered in this book.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Scala Programming Projects
  3. Packt Upsell
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  6. Writing Your First Program
    1. Setting up your environment
      1. Installing the Java SDK
      2. Installing IntelliJ IDEA
    2. Using the basic features
      1. SBT synchronization
      2. Build
    3. Running the Scala Console
    4. Using the Scala Console and Worksheet
      1. Using the Scala Console
        1. Declaring variables
        2. Types
        3. Declaring and calling functions
        4. Side effects
        5. If...else expression
        6. Class
      2. Using the worksheet
        1. Class inheritance
        2. Subclass assignment
        3. Overriding methods
        4. Abstract class
        5. Trait
        6. Scala class hierarchy
        7. Case class
        8. Companion object
    5. Creating my first project
      1. Creating the project
      2. Creating the Main object
      3. Writing the first unit test
      4. Implementing another feature
      5. Implementing the Main method
    6. Summary
  7. Developing a Retirement Calculator
    1. Project overview
    2. Calculating the future capital
      1. Writing a unit test for the accumulation phase
      2. Implementing futureCapital
      3. Refactoring the production code
      4. Writing a test for the decumulation phase
      5. Simulating a retirement plan
        1. Writing a failing unit test
        2. Working with tuples
        3. Implementing simulatePlan
    3. Calculating when you can retire
      1. Writing a failing test for nbOfMonthsSaving
      2. Writing the function body
      3. Understanding tail-recursion
      4. Ensuring termination
    4. Using market rates
      1. Defining an algebraic data type
      2. Filtering returns for a specific period
      3. Pattern matching
      4. Refactoring simulatePlan
      5. Loading market data
        1. Writing the unit test using the column selection mode
        2. Loading the file with Source
        3. Loading inflation data
      6. Computing real returns
    5. Packaging the application
      1. Creating the App object
      2. Packaging the application
    6. Summary
  8. Handling Errors
    1. Setup
    2. Using exceptions
      1. Throwing exceptions
      2. Catching exceptions
      3. Using the finally block
    3. Ensuring referential transparency
      1. Defining pure functions
      2. Best practices
      3. Showing how exceptions break referential transparency
    4. Using Option
      1. Manipulating instances of Option
      2. Composing transformations with for... yield
      3. Refactoring the retirement calculator to use Option
    5. Using Either
      1. Manipulating Either
      2. Refactoring the retirement calculator to use Either
        1. Refactoring nbOfMonthsSavings
        2. Refactoring monthlyRate
        3. Refactoring simulatePlan
        4. Refactoring SimulatePlanApp
    6. Using ValidatedNel
      1. Adding the cats dependency
      2. Introducing NonEmptyList
      3. Introducing Validated
      4. Refactoring the retirement calculator to use ValidatedNel
        1. Adding unit tests
        2. Implementing parsing functions
        3. Implementing SimulatePlanApp.strSimulatePlan
        4. Refactoring SimulatePlanApp.strMain
    7. Summary
    8. Questions
    9. Further reading
  9. Advanced Features
    1. Project setup
    2. Strictness and laziness
      1. Strict val
      2. lazy val
      3. By-name parameters
      4. Lazy data structures
    3. Covariance and contravariance
      1. InvariantDecoder
      2. CovariantDecoder
      3. Contravariant encoder
      4. Covariance in collections
    4. Currying and partially applied functions
      1. Function value
      2. Currying
      3. Partially applied functions
    5. Implicits
      1. Implicit parameters
      2. Implicit parameter usage
        1. Passing a timeout
        2. Passing an application context
        3. Examples in the SDK
          1. breakOut
          2. executionContext
      3. Implicit conversion
        1. Implicit class
      4. How are implicits resolved?
    6. Summary
  10. Type Classes
    1. Understanding type classes
      1. Type class recipe
    2. Common type classes
      1. scala.math.Ordering
      2. org.scalactic.Equality
      3. cats.Semigroup
        1. Laws
        2. Usage examples
      4. cats.Monoid
        1. Laws
        2. Usage examples
      5. Higher-kinded types
        1. Arity
        2. Higher-order function
        3. Higher-kinded types
      6. cats.Functor
        1. Laws
        2. Usage examples
      7. cats.Apply
        1. Laws
        2. Usage examples
      8. cats.Applicative
        1. Laws
        2. Usage examples
      9. cats.Monad
        1. Laws
        2. Usage examples
    3. Summary
  11. Online Shopping - Persistence
    1. Creating the project
    2. Persistence
      1. Setting up Slick
      2. Setting up the database
      3. Database test
        1. Product test
        2. Cart test
      4. Future
        1. Getting a concrete value
        2. Waiting on a Future
        3. Callback
      5. Composing Futures
        1. Using for-comprehension
      6. Execution context
      7. Rounding up Futures
      8. Database creation
      9. Data Access Object creation
      10. Running the test
    3. Deploying the application
      1. Setting up an account
      2. Deploying your application
    4. Heroku configuration
    5. Summary
  12. Online Shopping - REST API
    1. The REST API
      1. Writing the tests
      2. Defining the routes
      3. Running the test
        1. Checking the API
    2. Implementing the API with persistence
      1. Completing the product tests
        1. Implementing the product API
          1. Product list
        2. Encoding JSON with Circe
        3. Action definition 
        4. Adding a product
      2. Completing the cart test
        1. Logging in
          1. Unit test
          2. Implementation
          3. Passing the cookie
        2. Listing products in cart
    3. Swagger
      1. Installing Swagger
      2. Declaring endpoints
      3. Running the application
        1. Login
        2. List of products
        3. Cart endpoints
    4. Deploying on Heroku
    5. Summary
  13. Online Shopping - User Interface
    1. Learning objectives
    2. Setting up
    3. Defining the layout
    4. Creating the layout
    5. Building the page
    6. Main layout
    7. Product list panel
    8. Cart panel
    9. Introducing the UI manager
      1. Adding jQuery to our project
      2. Calling our API
        1. Setting the main method
        2. Initializing the user interface
      3. Implementing UI actions
        1. Adding a product to the cart
        2. Removing a product from the cart
        3. Updating the quantity
      4. Deploying the user interface
      5. Debugging the interface
    10. Summary
  14. Interactive Browser
    1. Actors
    2. Setting up
    3. Implementing the server side
      1. Creating the web socket route
      2. Implementing BrowserManager
      3. Handling WebSocket
      4. A diagram of Actors
    4. Implementing the client side
      1. Adding the web socket
      2. Notifying the user
        1. Extending jQuery
    5. Summary
  15. Fetching and Persisting Bitcoin Market Data
    1. Setting up the project
    2. Understanding Apache Spark
      1. RDD, DataFrame, and Dataset
      2. Spark SQL
        1. Dataframe
        2. Dataset
      3. Exploring the Spark API with the Scala console
        1. Transforming rows using map
        2. Transforming rows using select
      4. Execution model
    3. Implementing the transaction batch producer
      1. Calling the Bitstamp REST API
      2. Parsing the JSON response
        1. Unit testing jsonToHttpTransaction
        2. Implementing jsonToHttpTransaction
        3. Unit testing httpToDomainTransactions
        4. Implementing httpToDomainTransactions
      3. Saving transactions
        1. Introducing the Parquet format
        2. Writing transactions in Parquet
        3. Using the IO Monad
      4. Putting it all together
        1. Testing processOneBatch
        2. Implementing processOneBatch
        3. Implementing processRepeatedly
        4. Implementing BatchProducerApp
      5. Running the application with spark-submit
        1. Installing Apache Spark
        2. Packaging the assembly JAR
        3. Running spark-submit
    4. Summary
  16. Batch and Streaming Analytics
    1. Introduction to Zeppelin
      1. Installing Zeppelin
      2. Starting Zeppelin
      3. Testing Zeppelin
      4. Structure of a notebook
      5. Writing a paragraph
      6. Drawing charts
    2. Analyzing transactions with Zeppelin
      1. Drawing our first chart
      2. Drawing more charts
    3. Introducing Apache Kafka
      1. Topics, partitions, and offsets
      2. Producing data into Kafka
      3. Consuming data from Kafka
        1. Consumer group
        2. Offset management
      4. Connecting to Kafka
    4. Streaming transactions to Kafka
      1. Subscribing with Pusher
      2. Deserializing live transactions
      3. Converting to transaction and serializing
      4. Putting it all together
      5. Running StreamingProducerApp
    5. Introducing Spark Streaming
    6. Analyzing streaming transactions with Zeppelin
      1. Reading transactions from Kafka
      2. Writing to an in-memory sink
      3. Drawing a scatter chart
      4. Aggregating streaming transactions
    7. Summary
  17. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Scala Programming Projects
  • Author(s): Mikael Valot, Nicolas Jorand
  • Release date: September 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788397643