Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Scala Programming Projects
Scala Programming Projects

Scala Programming Projects: Build real-world projects using popular Scala frameworks such as Play, Akka, and Spark

eBook
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Scala Programming Projects

Developing a Retirement Calculator

In this chapter, we will put into practice the features of the Scala language seen in the first chapter. We will also introduce other elements of the Scala language and SDK to develop the model and logic for a retirement calculator. This calculator will help people work out how long and how much to save to have a comfortable retirement.

We will use the test-driven development (TDD) technique to develop the different functions. I encourage you to try writing the body of the functions yourself before looking at the solution. Also, it would be better to retype the code rather than copy/pasting it. You will remember it more and will have a sense of what it feels like to use IntelliJ's completion and editor. Do abuse the autocompletion with Ctrl + spacebar. You will not only type faster, but you will also discover what functions are...

Project overview

Using some parameters such as your net income, your expenses, your initial capital, and so on, we will create functions to calculate the following:

  • Your future capital at retirement
  • Your capital after a number of years in retirement
  • How long you need to save to be able to retire

We will first use a fixed interest rate for these calculations. After that, we will load market data from .tsv files, then refactor the previous functions to simulate what could happen during the investment period.

Calculating the future capital

The first thing you need to know when planning for retirement is how much capital you can get at your chosen retirement date. For now, we will assume that you invest your savings every month at a constant rate. To simplify things, we will ignore the effects of inflation, hence the capital calculated will be in today's money and the interest rate will be calculated as real rate = nominal interest rate - inflation rate.

We intentionally do not mention any currency in the rest of this chapter. You can consider that the amounts are in USD, EUR, or any other currency. It will not change the results as long as all the amounts are expressed in the same currency.

Writing a unit test for the accumulation phase

...

Calculating when you can retire

If you have tried to call simulatePlan from the Scala Console, you probably tried different values for nbOfMonths and observed the resulting capital at retirement and after death. It would be useful to have a function that finds the optimal nbOfMonths so that you have enough capital to never run out of money during your retirement.

Writing a failing test for nbOfMonthsSaving

As usual, let's start with a new unit test to clarify what we expect from this function:

"RetCalc.nbOfMonthsSaving" should {
"calculate how long I need to save before I can retire" in {
val actual = RetCalc.nbOfMonthsSaving(
interestRate = 0.04 / 12, nbOfMonthsInRetirement = 40 * 12...

Using market rates

In our calculations, we have always assumed that the interest rate of return was constant, but the reality is more complex. It would be more accurate to use real rates from market data to gain more confidence with our retirement plan. For this, we first need to change our code to be able to perform the same calculations using variable interest rates. Then, we will load real market data to simulate regular investments in a fund by tracking the S & P 500 index.

Defining an algebraic data type

In order to support variable rates, we need to change the signature of all functions that accept interestRate: Double. Instead of a double, we need a type that can represent either a constant rate or a sequence of...

Packaging the application

We have now implemented some useful building blocks, and it is time to create an executable so that end users can use our calculator with their own parameters.

Creating the App object

We are going to build a simple executable around RetCalc.simulatePlan. It will take a list of parameters separated by spaces, and print the results on the console.

The test we are going to write integrates several components together and will use a full market data set. As such, it is not really a unit test anymore; it is an integration test. For this reason, we suffixed it with IT instead of Spec.

First, copy sp500.tsv and cpi.tsv from https://github.com/PacktPublishing/Scala-Programming-Projects/blob/master...

Summary

We have covered how to create a small project in Scala from scratch to packaging. We used TDD along the way so that it is guaranteed that our code was well-designed and robust. It gives us confidence when we refactor code: as long as all tests pass, we know that our code stills works. We modeled our domain with immutable data structures and processed them using pure functions that have no side effects.

We used some basic features of the language that are used in most projects, and you should now be familiar with enough building blocks to implement a wide variety of projects.

As a further exercise, you could enhance this calculator with one or many of these features:

  • Create an app for the function RetCalc.nbOfMonthsSaving, which calculates how long you need to save before you can retire.
  • Create a function called RetCalc.annualizedTotalReturn, which calculates...
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

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

Description

Scala Programming Projects is a comprehensive project-based introduction for those who are new to Scala. Complete with step-by-step instructions and easy-to-follow tutorials that demonstrate best practices when building applications, this Scala book will have you building real-world projects in no time. Starting with the fundamentals of software development, you’ll begin with simple projects, such as developing a financial independence calculator, and then advance to more complex projects, such as a building a shopping application and a Bitcoin transaction analyzer. You’ll explore a variety of Scala features, including its OOP and FP capabilities, and learn how to write concise, reactive, and concurrent applications in a type-safe manner. You’ll also understand how to use libraries such as Akka and Play. Furthermore, you’ll be able to integrate your Scala apps with Kafka, Spark, and Zeppelin, along with deploying applications on a cloud platform. By the end of the book, you’ll have a firm foundation in Java programming that’ll enable you to solve a variety of real-world problems, and you’ll have built impressive projects to add to your professional portfolio.

Who is this book for?

If you’re new to programming and want to learn how to use Scala, this book is for you. Though not necessary, some knowledge of Java will help you understand the concepts covered in this book.

What you will learn

  • Build, test, and package code using sbt (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 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
Estimated delivery fee Deliver to Austria

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Sep 29, 2018
Length: 398 pages
Edition : 1st
Language : English
ISBN-13 : 9781788397643
Category :
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Austria

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Publication date : Sep 29, 2018
Length: 398 pages
Edition : 1st
Language : English
ISBN-13 : 9781788397643
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 120.97
Modern Scala Projects
€41.99
Scala Programming Projects
€41.99
Scala Design Patterns
€36.99
Total 120.97 Stars icon
Banner background image

Table of Contents

12 Chapters
Writing Your First Program Chevron down icon Chevron up icon
Developing a Retirement Calculator Chevron down icon Chevron up icon
Handling Errors Chevron down icon Chevron up icon
Advanced Features Chevron down icon Chevron up icon
Type Classes Chevron down icon Chevron up icon
Online Shopping - Persistence Chevron down icon Chevron up icon
Online Shopping - REST API Chevron down icon Chevron up icon
Online Shopping - User Interface Chevron down icon Chevron up icon
Interactive Browser Chevron down icon Chevron up icon
Fetching and Persisting Bitcoin Market Data Chevron down icon Chevron up icon
Batch and Streaming Analytics Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8
(5 Ratings)
5 star 40%
4 star 20%
3 star 20%
2 star 20%
1 star 0%
Melvin Apr 03, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is in my opinion the best scala book out there to start your journey with. I have looked at quite a few scala books and some are good but take forever to get to so called advanced concepts. This book gets you up to speed with programming concepts unique to scala without needing to read over a thousand pages first. Additionally, this book incorporated the use of intellij IDE which can be a pain when you first start using scala. The other thing that I liked about the book is that the projects show you how to tackle various problems by writing in the scala style of the functional programming. I think this book probably was the most useful scala book I have ever bought.
Amazon Verified review Amazon
Misha Tavkhelidze Jan 12, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excellent book to start your Scala journey. Read it before jumping to more advanced, say, Functional Programming in Scala.Apart from introducing the language (and FP paradigm) from very basic to comparatively advanced aspects, the book also teaches you how to set up projects using standard build tools and frameworks, which I found to be invaluable for someone who is new to the whole eco-system.Some familiarity with the language itself is nessesary, but, I believe, not required.
Amazon Verified review Amazon
Cameron Smith Sep 03, 2019
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Good introduction to specific frameworks, approaches and libraries for someone who already knows Scala.Opinionated but justifies his opinions based on realistic project examples, rather than just ranting.Still have not finished the book - because every two pages give a wealth of examples and ideas to try out.
Amazon Verified review Amazon
Amazon Customer Aug 08, 2019
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Books does not teach you scala. Project descriptions not best but enough. That is book for people that have learned scala, akka etc and looking for good project example and some example how things are done in scala ecosystem.
Amazon Verified review Amazon
Mark Tanser Jul 09, 2019
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
The overall content and exercises are fine, but it is dogged by typos and omissions which render it a huge struggle to follow for a beginner. Without consulting the online code on gitHub it would be impossible to complete the sections. There are also code exceptions and errors which are not covered in the text and you're left to search for answers on Stackoverflow. Really, a book like this should be thoroughly proof-read and tested by a first-sight user going through the instructions cover-to-cover, before committing to print and offering for sale.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela