Learning Scala
Learning Scala
Article Index
Learning Scala
Part II: Object-Oriented Scala

Author: Jason Swartz
Publisher: O'Reilly Media
Pages: 256
ISBN: 978-1449367930
Print: 1449367933
Kindle: B00QW1RQ94
Audience: Developers wanting to learn Scala
Rating: 4.7
Reviewer: Ian Stirk

This book aims to help developers learn the Scala programming language, how does it fare?

 

The audience for this book is primarily developers that have worked with object-oriented languages e.g. Java, Ruby, and Python, that want to learn Scala.

Scala marries object-oriented features with functional programming, so any experience with these will help in understanding this book. Scala is very popular with big data systems, being used increasingly with interactive processing (e.g. Spark).

The book is relatively small, having around 220 working pages, consisting of 2 sections: Core Scala (7 chapters) and Object-Oriented Scala (3 chapters).

Below is a chapter-by-chapter exploration of the topics covered.

 

Chapter 1 Getting Started with the Scalable Language

This chapter opens with a brief history of Scala, its name is derived from SCAlable LAnguage, and it was created in 2003 as a high performance concurrent ready environment, for functional and object-orient processing on the Java Virtual Machine.

The chapter continues with a look at where to download Scala and its associated prerequisite - Java. Scala can be installed either manually or via packages, the latter is easier.

Next, the Scala REPL environment is discussed. REPL represents Read-Eval-Print Loop, and describes how code can be entered into the REPL environment where it is read, evaluated and the results printed, this process can then be repeated (i.e. the looping part of REPL). The environment keeps your variables and values available for the session, for reuse.

This chapter provided a gentle introduction to Scala, showing where to download it and how to install it. The REPL environment is introduced together with some simple example code. REPL is used for all the examples in this book.

The chapter teaches concepts concisely, assisted with related code examples and outputs. A useful summary, together with chapter-related exercises, are provided. The chapter is well written and practical. These traits apply to each chapter of the book.

Chapter 2 Working with Data: Literals, Values, Variables, and Types

This chapter opens with a look at values, these hold immutable data, and variables which hold mutable data. Values are preferred over variables since they tend to be more stable and are responsible for fewer errors. A short section on the naming rules for values and variables is given.

The chapter continues with a look at types. There’s a useful table of core numeric types, giving their description and minimum/maximum value. There’s a useful diagram showing the hierarchy of Scala’s core types, all derived from the Any type, with AnyVal as the parent for value types, and AnyRef as the parent for reference types. A tuple is briefly described as an ordered container for 2 or more values (like a relational row).

This chapter provides a helpful overview of the various types used in Scala, together with example usage. Variables, values and types are the base components upon which the language is built.

 

 

Chapter 3 Expressions and Conditionals

The chapter looks at the use of expressions, these are units of code that return a value. They are at the heart of functional programming, where new data is returned, instead of modifying existing data. It’s possible for statements not to return any data, here the return type is named Unit.

The chapter continues with a look at language structures that control decision making. First, If..Else expression blocks are discussed. There is no elseif - this can be implemented via if..else{if..else}. The match structure has similar functionality as the switch structure in other languages. Wildcards can be used as match-all patterns, this is important since an uncaught match will cause an error. Pattern guards add an “if expression” to the pattern, applying conditional logic to the match expressions.

The chapter ends with a look at loops, which allow a task to be repeated until a condition is met. The for-loop is the primary loop structure, it can contain the “yield” keyword – this returns the entire loop values in a collection. An iterator guard, like match expressions, provides filtering. Additionally, looping can be provided via While and Do/While structures, however these cannot yield values.

This is another helpful chapter, describing, with examples, the various Scala language structures for both conditional logic and looping.

 

Chapter 4 Functions

Functions are a core feature of functional programming. If written and used correctly, they provide no side-effects, are more stable and less prone to errors that other features.

The chapter opens with a look at procedures, which do not return a value, and parameter-less functions. Next, recursive and nested functions are discussed, all with examples. Functions can be called with named parameters, enabling the functions to be called with parameters that are out of sequence. Parameters can have default values. Vararg parameters allow zero or more arguments to be processed (e.g. the printf function can process a variable number of input parameters).

The chapter next discusses type parameters, these provide a large degree of flexibility - you can define a function with a generic root type (e.g. Any), and the caller can call the function with an appropriate type.

The chapter ends with a look at writing functions for improved readability and maintenance. Ideally, functions should be readable, short, well-named, and obvious. Larger/complex functions should be broken down into simpler functions. Comment should be added to improve readability.

This chapter provides a practical overview of the basic use of functions in Scala. There’s a very helpful overview of pure functions, these perform calculations using only input parameters, return the same value for the same input, and aren’t affected by data outside the function.

Chapter 5 First-Class Functions

The chapter opens with a definition of a First-Class function, these are functions that can be used everywhere, just like data.

The type of a function is the signature of the function’s input types, then a right arrow (i.e. =>), followed by the function’s output types. For example: (Int, String) => Int, represents a type of function that accepts an integer and a string, and returns an integer. An underscore can be used to represent a wildcard operator.

Higher-Order functions are discussed next, these accept functions as parameters. They’re often used in declarative programming (where you specify what to do, not how it is done), where the Higher-Order function does the implementation. There’s a useful example related to using a safeString.

The next section discusses function literals, these are nameless functions, popularly known as anonymous functions. It is possible to replace named parameters with wildcard operators. Partially Applied Functions can reuse a function invocation and retain some of the parameter values.

The chapter ends with a look at Partial Functions, these restrict the values of the parameters that can be used (e.g. ensuring only a positive value can be passed to a square root function). These can be useful for collections and pattern matching.

This chapter provides a useful overview of how Scala uses First-Class functions. There are some helpful step-by-step walkthroughs, together with annotated REPL examples.

Banner
 



Last Updated ( Saturday, 12 August 2017 )
 
 

   
Banner
Banner
RSS feed of book reviews only
I Programmer Book Reviews
RSS feed of all content
I Programmer Book Reviews
Copyright © 2017 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.