Scala Programming Projects
Build real world projects using popular Scala frameworks like Play, Akka, and Spark
- 398 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
Scala Programming Projects
Build real world projects using popular Scala frameworks like Play, Akka, and Spark
About This Book
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.
Frequently asked questions
Information
Handling Errors
- Using exceptions when necessary
- Understanding referential transparency
- Using Option to represent optional values
- Using Either to handle errors sequentially
- Using Validated to handle errors in parallel
Setup
- Create an account at https://github.com/ if you do not have one already.
- Go to the retirement calculator project at https://github.com/PacktPublishing/Scala-Programming-Projects. Click on Fork in the top-right corner to fork the project into your account.
- Once the project is forked, click on Clone or download, and copy the URL into the clipboard.
- In IntelliJ, go to File | New | Project from Version Control | GitHub and make the following edits:
- Git repository URL: Paste the URL of your forked repository
- Parent directory: Choose a location
- Directory name: Keep retirement_calculator
- Click on Clone
- The project should be imported in IntelliJ. Click on git: master in the bottom-right of the screen and select Remote branches | origin/chapter2 | Checkout as new local branch. Name the new branch chapter3_yourusername to distinguish it from the final solution, which is in the origin/chapter3 branch.
- Build the project with Ctrl + F9. Everything should compile.
Using exceptions
- The throw exceptionObject statement stops the current function and passes the exception up to the caller.
- The try { myFunc() } catch { case pattern1 => recoverExpr1 } statement catches any exception thrown by myFunc() if the exception matches one of the patterns inside the catch block:
- If an exception is thrown by myFunc, but no pattern matches the exception, the function stops, and the exception is passed up to the caller again. If there is no try...catch block in the call chain that can catch the exception, the whole program stops.
- If an exception is thrown by myFunc, and the pattern1 pattern matches the exception, the try...catch block will return the recoverExpr1 expression at the right of the arrow.
- If no exception is thrown, the try...catch block returns the result returned by myFunc().
Throwing exceptions
case class Person(name: String, age: Int)
case class AgeNegativeException(message: String) extends Exception(message)
def createPerson(description: String): Person = {
val split = description.split(" ")
val age = split(1).toInt
if (age < 0)
throw AgeNegativeException(s"age: $age should be > 0")
else
Person(split(0), age)
scala> createPerson("John 25")
res0: Person = Person(John,25)
scala> createPerson("John25")
java.lang.ArrayIndexOutOfBoundsException: 1
at .createPerson(<console>:17)
... 24 elided
scala> createPerson("John -25")
AgeNegativeException: age: -25 should be > 0
at .createPerson(<console>:19)
... 24 elided
scala> createPerson("John 25.3")
java.lang.NumberFormatException: For input string: "25.3"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at scala.collection.immutable.StringLike.toInt(StringLike.scala:301)
at scala.collection.i...
Table of contents
- Title Page
- Copyright and Credits
- Packt Upsell
- Contributors
- Preface
- Writing Your First Program
- Developing a Retirement Calculator
- Handling Errors
- Advanced Features
- Type Classes
- Online Shopping - Persistence
- Online Shopping - REST API
- Online Shopping - User Interface
- Interactive Browser
- Fetching and Persisting Bitcoin Market Data
- Batch and Streaming Analytics
- Other Books You May Enjoy