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
Professional Scala
Professional Scala

Professional Scala: Combine object-oriented and functional programming to build high-performance applications

eBook
£7.99 £26.99
Paperback
£32.99
Subscription
Free Trial
Renews at £16.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
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

Billing Address

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

Professional Scala

Chapter 2. Basic Language Features

In the previous chapter, we learned the various aspects of setting up the development environment wherein we covered the structure of a Scala project and identified the use of sbt for building and running projects. We covered REPL, which is a command-line interface for running Scala code, and how to develop and run code over the IDEA IDE. Finally, we implemented interactions with our simple chatbot application.

In this chapter, we will explore the so-called 'OO' part of Scala, which allows us to build constructions similar to analogs in any mainstream language, such as Java or C++. The object-oriented part of Scala will cover classes and objects, traits, pattern matching, case class, and so on. Finally, we will implement the object-oriented concepts that we learn to our chatbot application.

Looking at the history of programming paradigms, we will notice that the first generation of high-level programming languages (Fortran, C, Pascal...

Objects, Classes, and Traits

Scala is a multiparadigm language, which unites functional and OO programming. Now, we will explore Scala's traditional object-oriented programming facilities: object, classes, and traits.

These facilities are similar in the sense that each one contains some sets of data and methods, but they are different regarding life cycle and instance management:

  • Objects are used when we need a type with one instance (such as singletons)
  • Classes are used when we need to have many instances, which can be created with the help of the new operator
  • Traits are used for mix-ins into other classes

Note that it is not worth navigating through code, as this is exposed in examples.

Object

We have seen an object in the previous chapter. Let's scroll through our codebase and open the file named Main in Lesson 2/3-project:

object Chatbot3 {
val effects = DefaultEffects
def main(args: Array[String]): Unit = {
   ….}
   def createInitMode() = (Bye or CurrentDate or CurrentTime...

OO in Our Chatbot

Now that you know the theoretical basics, let's look at these facilities and how they are used in our program. Let's open Lesson 2/3-project in our IDE and extend our chatbot, which was developed in the previous chapter.

Decoupling Logic and Environment

To do this, we must decouple the environment and logic, and integrate only one in the main method.

Let's open the EffectsProvider class:

Note

For full code, refer to Code Snippets/Lesson 2.scala file.

trait EffectsProvider extends TimeProvider {

 def input: UserInput

 def output: UserOutput

}

object DefaultEffects extends EffectsProvider
{
 override def input: UserInput = ConsoleInput

 override def output: UserOutput = ConsoleOutput

 override def currentTime(): LocalTime = LocalTime.now()

 override def currentDate(): LocalDate = LocalDate.now()
}

Here, we encapsulate all of the effects into our traits, which can have different implementations.

For example, let's look at UserOutput:

For full...

Function Calls

Now, we'll look at how function calls are implemented in Scala.

Syntax Goodies

Scala provides flexible syntax and it is worth dedicating a few minutes to this concept.

Named Parameters

The following is a function, f(a:Int, b:Int). We can call this function using the named parameter syntax: f(a = 5, b=10). If we swap the parameters but leave the correct names, the method will still be correct.

It is possible to combine positional and named function calls—the first few arguments can be positional.

For example:

def f(x:Int, y:Int) = x*2 + y
f(x=1,y=2) // 4
f(y=1,x=2) // 5

Default Parameters

When specifying a function, we can set default parameters. Then, later, when we call this function, we can omit parameters and the compiler will substitute defaults:

def f(x:Int, y:Int=2) = x*2 + y
f(1) // 4

It is possible to create a comfortable API with the help of the combination of named and default arguments. For example, for case classes with N components, the compiler generates...

Summary

We have now reached the end of this chapter. In this chapter, we covered the object-oriented aspects of Scala such as classes, objects, pattern matching, self-types, case classes, and so on. We also implemented object-oriented concepts that we learned in our chatbot application.

In the next chapter, we will cover functional programming with Scala and how object-oriented and functional approaches complete each other. We will also cover generic classes, which are often used with pattern matching. We will also cover how to create user-defined pattern matching and why is it useful.

Left arrow icon Right arrow icon

Key benefits

  • Expert guidance that shows you to efficiently use both object-oriented and functional programming techniques
  • Understand functional programming libraries, such as Cats and Scalaz, and use them to augment your Scala development
  • Perfectly balances theory and hands-on exercises, assessments, and activities

Description

This book teaches you how to build and contribute to Scala programs, recognizing common patterns and techniques used with the language. You’ll learn how to write concise, functional code with Scala. After an introduction to core concepts, syntax, and writing example applications with scalac, you’ll learn about the Scala Collections API and how the language handles type safety via static types out-of-the-box. You’ll then learn about advanced functional programming patterns, and how you can write your own Domain Specific Languages (DSLs). By the end of the book, you’ll be equipped with the skills you need to successfully build smart, efficient applications in Scala that can be compiled to the JVM.

Who is this book for?

This is an ideal book for developers who are looking to learn Scala, and is particularly well suited for Java developers looking to migrate across to Scala for application development on the JVM.

What you will learn

  • Understand the key language syntax and core concepts for application development
  • Master the type system to create scalable type-safe applications while cutting down your time spent debugging
  • Understand how you can work with advanced data structures via built-in features such as the Collections library
  • Use classes, objects, and traits to transform a trivial chatbot program into a useful assistant
  • Understand what are pure functions, immutability, and higher-order functions
  • Recognize and implement popular functional programming design patterns

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 31, 2018
Length: 186 pages
Edition : 1st
Language : English
ISBN-13 : 9781789534702
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
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

Billing Address

Product Details

Publication date : Jul 31, 2018
Length: 186 pages
Edition : 1st
Language : English
ISBN-13 : 9781789534702
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
£16.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
£169.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
£234.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 £ 116.97
Modern Scala Projects
£41.99
Professional Scala
£32.99
Scala Programming Projects
£41.99
Total £ 116.97 Stars icon
Banner background image

Table of Contents

9 Chapters
1. Setting up the Development Environment Chevron down icon Chevron up icon
2. Basic Language Features Chevron down icon Chevron up icon
3. Functions Chevron down icon Chevron up icon
4. Scala Collections Chevron down icon Chevron up icon
5. Scala Type System Chevron down icon Chevron up icon
6. Implicits Chevron down icon Chevron up icon
7. Functional Idioms Chevron down icon Chevron up icon
8. Domain Specific Languages Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.2
(13 Ratings)
5 star 53.8%
4 star 23.1%
3 star 15.4%
2 star 0%
1 star 7.7%
Filter icon Filter
Top Reviews

Filter reviews by




Sviatoslav Ksondzyk May 01, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Udemy Verified review Udemy
Alex Kalinichev Nov 26, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Udemy Verified review Udemy
Parthasarathy Chakravarthy Nov 17, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Udemy Verified review Udemy
Mikhail Kalugin Feb 06, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Udemy Verified review Udemy
Paritosh sharma Dec 10, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Udemy Verified review Udemy
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.