Professional Scala
eBook - ePub

Professional Scala

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

  1. 186 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Professional Scala

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

About this book

If your application source code is overly verbose, it can be a nightmare to maintain. Write concise and expressive, type-safe code in an environment that lets you build for the JVM, browser, and more.

Key Features

  • 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

Book 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.

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

Who this book is 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.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Professional Scala


Table of Contents

Professional Scala
Why Subscribe?
PacktPub.com
Contributors
About the Authors
Preface
Who This Book Is For
What This Book Covers
What You Need for This Book
Conventions
Installation and Setup
Installing IntelliJ IDE
Installing Scala plugin (make heading)
Reader Feedback
Customer Support
Downloading the Example Code
Errata
Piracy
Questions
1. Setting up the Development Environment
Simple Program
Definitions: Packages, Imports, and Objects
Object Definition
The main Method
Inside main
Structure of a Scala Project
Basic sbt Commands
Activity: Performing Basic Operations with sbt: Build, Run, Package
IDE
Activity: Loading and Running a Sample Project in the IDE
REPL
Obtaining the Time Request from Our Chatbot Program
Base Syntax
Base Syntax for Definitions
Base Syntax for Expressions
Unit Testing
Adding a Test to Our Project
Inside Tests
Running Tests for Chatbot
Summary
2. Basic Language Features
Objects, Classes, and Traits
Object
Classes
Equality and Case Classes
Pattern Matching
Traits
Self-Types
Special Classes
OO in Our Chatbot
Decoupling Logic and Environment
Sealed Traits and Algebraic Datatypes
Function Calls
Syntax Goodies
Named Parameters
Default Parameters
Currying Forms (Multiple Argument Lists)
Special Magic Methods
Implementing + in CartesianPoint
Parameter-Passing Mode
By Value
By Name
By Need
Creating a Runnable Construction
Printing the log Argument to the Console and File
Summary
3. Functions
Functions
Function Values
Function from an OO Point of View
Conversions
Defining and Measuring the Time of a Unit Function
Syntax Sugar in a Function Definition
Partial Functions
Exploring Pattern Matching
Binding a Sequence of Variables in the Pattern Matcher
Partial Functions in Practice
Representing ChatbotMode as a Partial Function
Implementing RemindStore as a Set of Partial Functions
Using Lifting for Conversations between Total and Partial Functions
Summary
4. Scala Collections
Working with Lists
Constructing Lists
Operations on Lists
Pattern Matching on Lists
First-Order Methods on List
Appending and Concatenation
Taking the Length of a List
Reversing a List
Prefixes and Suffixes
Element Selection
Display
Activity: Creating a New Mode for Chatbot Using Lists
Abstracting on Sequences
The Traversable Trait
Iterators
Streams
Activity: Implementing Fibonacci Series Using Streams and Iterators
Other Collections
Sets
Tuples
Maps
Mutable and Immutable Collections
Activity: Implementing the Tower of Hanoi Problem
Summary
5. Scala Type System
Type Basics and Polymorphism
A Unified Type System
Parametric Polymorphism
Type Inference
Parameterized Types
Bounds
Existential Types
Activity: Generalizing the Implementation of the Binary Tree
Variance
Covariance
Contravariance
Invariance
Activity: Implementing Covariance and the Database for Tools
Advanced Types
Abstract Type Members
Structural Types
Option
Higher Kind Types
Type Erasure
Activity: Finding an Element Based on a Given Predicate
Summary
6. Implicits
Implicit Parameters and Implicit Conversions
Implicit Parameters
Implicit Conversions
Implicit Resolution
Activity: Creation of Extension Methods
Ad Hoc Polymorphism and Type Classes
Types of Polymorphism
Type Classes
Context Bounds and Implicitly
Type Classes in the Standard Library
Activity: Implementing Type Classes to Support Conversion
Summary
7. Functional Idioms
Introduction to Functional Programming Concepts
Pure Functions
Immutability
Implementing the standard library
Higher-Order Functions
Functional Design Patterns
Monoids
Using Monoids to Write Polymorphic Functions
Functor
Monads
Popular Libraries
Validating Data Using Cats
Prerequisites for Using Cats
Introduction to Cats
Validating Data
Validating Using Validated
Communicating with Databases Using Doobie
Prerequisites for Doobie
Doobie
ConnectionIO
Transactor
Selecting Rows
Querying Using Parameters
Deleting, Inserting, and Updating Rows
A Complete Example
Activity: Adding Priority to the Todo List
Summary
8. Domain Specific Languages
DSLs and Types of DSLs
External DSLs
Internal DSLs
ScalaTest – A Popular DSL
Adding ScalaTest to Your Project
Overview of ScalaTest Styles
Activity: Implementing ScalaTest Styles
Language Features for Writing DSLs
Flexible Syntax for Method Invocation
By-Name Parameters
Extension Methods and Value Classes
Writing a Small DSL
Modeling Test Cases
DSL for TestCase
Activity: Creating a DSL to Specify Assertions
Beyond This Book
Various Scala Libraries
Akka
Apache Spark
Shapeless
Uncovered Language Features
Macros
Def Macros
Implicit Macros
Quasiquotes
Reflection
Resources to Keep You Updated
Scala Improvement Process
Scala Times
Summary
Index

Professional Scala

Copyright © 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information ...

Table of contents

  1. Professional Scala

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Professional Scala by Mads Hartmann, Ruslan Shevchenko in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Java. We have over one million books available in our catalogue for you to explore.