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


Chapter 6 Common Collections

Collections are data structures that hold one or more values of a given type (e.g. map). It is possible to use the Java collections from Scala, however these do not allow the higher-order features present in Scala (e.g. map, filter).

The root of iterable collections is the Iterable structure, this provides common methods to iterate over collections. The chapter discusses the List collection in detail, but the methods are equally applicable to the other iterable collections. The chapter continues with some simple List examples (e.g. showing the content of the List). The head and tail methods can access the first and other items in the List. Examples are given of map (apply function to each item in List), and reduce (apply function to List items and return one value e.g. sum) methods.

The chapter continues with a look at what a List can contain (e.g. simple types, tuples, other Lists). IsEmpty can be used to determine if a List is empty, although Nil is often more efficient. Many rich methods exist for List arithmetic, a table of these is provided.

Methods exist to easily convert between the different types of collection (e.g. toList, toString). It’s also possible to use Java collections, via an appropriate import. The chapter ends with a look at pattern matching with collections, via the case statement.

This chapter provides a helpful overview of the common methods that can be applied to Scala collections. Although the List collection was used extensively, the methods are equally applicable to the other iterable collections.

Chapter 7 More Collections

This chapter discusses some of the more unusual collections, with an emphasis on mutable collections. Modifying a collection will create a new collection. The List, Set, and Map collections are immutable - but also have corresponding mutable collections (e.g. collection.mutable.Set). It is possible to swap from a mutable to an immutable collection via the appropriate conversion method (e.g. toList). Mutable collections are often built iteratively from within loops.

The chapter proceeds with a look at the Array, Sequences, and Streams collections. Useful examples are provided of each. The chapter ends with a look at monadic collections, these contain only one element. This chapter provided a helpful overview of some of the more unusual types of collections.


Chapter 8 Classes

This chapter opens with an overview of what classes are, followed by a brief introduction to object-oriented programming (OOP) i.e. encapsulation, inheritance, and polymorphism. The chapter then looks at defining classes. Classes have fields of values or variables and methods, and possibly nested classes. Classes can have parameters, and these can have default values.

Next, abstract classes are discussed. These can’t be instantiated, they need to be extended (via inheritance), and the abstract methods/fields overridden, and the extended class instantiated. The chapter continues with a look at method overloading, here methods have the same name but differ in their parameter signatures. Next, the Apply method is discussed, this is called by default. Packaging of classes is discussed briefly, its aim is to prevent name clash.

Next, accessibility of classes is discussed (i.e. protected: current and subclasses can access methods/fields, and private: only current class can access methods/fields). This is followed by a similar section related to the finer grained access of the class members.

The chapter ends with a look at final classes – whose members cannot be overridden, and sealed classes – which restrict the subclass to being located in same file as parent class.

This chapter provides a helpful overview of OOP, with simple Scala examples. If you already know OOP, this chapter is then mostly about aligning your existing knowledge with Scala’s syntax.

Chapter 9 Objects, Case Classes, and Traits

This chapter discusses 3 new components (objects, case classes, traits), that provide additional class functionality.

The chapter opens with a look at objects. In other languages these are typically known as static or global classes. Objects can extend other classes (but you can’t extend an object). They are most suitable as pure functions. Objects can act as a companion to a class of same name – providing additional functionality via its static methods. There’s a useful object function example provided that removes markup from HTML.

Next, case classes are examined. These automatically generate methods based on the class’s parameters. They are especially useful as data transfer objects. They are really just a convenience, since you could write the code yourself.

Next, traits are examined. These are classes that enable multiple inheritance. You can’t instantiate a trait, they are used to extend classes, case classes, objects and other traits. It’s possible to restrict traits to be used only with certain classes, also they can be used in class instantiation to provide build-in dependency injection.

The chapter ends with an example Scala project, built using the intelliJ IDE. Additionally, the Simple Build Tool (sbt) is used both to manage dependencies and as a build tool.

This chapter provides some useful additional class features. The author encourages you to create command-line Scala applications to replace common Unix functions – as a fun way to learn Scala.

Chapter 10 Advanced Typing

The features described in the previous chapters, together with the associated exercises, should allow you to start writing Scala applications. This chapter contains additional, advanced type functionality.

The chapter opens with a look at tuples and function value classes, these are both shortcuts to classes you could create yourself. Next, implicit parameters are discussed, these can either be provided from default parameter values or from a default namespace. It’s noted that excessive usage can decrease the readability of code.

The chapter continues with a look at implicit classes, these can provide implicit conversion between types. They are a good way to add useful methods to existing classes, and can make code more expressive. But again can impact code readability.

The chapter ends with a look at various advanced features of types, whose purpose is to write stricter, safer, more stable and better documented code. Type aliases create a new name for an existing type or class – this can be a useful way to refer to existing types with a local specific name. Bounded types can be restricted to a specific class or base type. Type variance is the opposite of bounded types, detailing how a type parameter may adapt to meet base type or subtype.

This chapter provides a useful overview of some of the more advanced type functionality. The trade-off between using the functionality and its impact on code readability/maintenance was noted.




This book aims to help developers learn the Scala programming language, and succeeds admirably - provided you are already familiar with programming concepts using another language, especially an object-oriented language. I found myself constantly aligning my existing programming knowledge with Scala’s syntax.

The book is well written, concise in its explanations, with plenty of helpful examples to follow along with. The summaries and exercises at the end of each chapter are useful. The answers to the chapter exercises can be found at http://bit.ly/Learning-Scala-materials.

While the book concentrates on how to use the Scala language, there is little on Scala’s associated tools (e.g. Spark). Additionally, it might have been useful to include a section on where to find further information (books, websites, blogs etc). However, these are minor concerns.

Overall, this is a very useful, concise, introduction to the Scala language for existing developers. Highly recommended.


To keep up with our coverage of books for programmers, follow @bookwatchiprog on Twitter or subscribe to I Programmer's Books RSS feed for each day's new addition to Book Watch and for new reviews.


JavaScript Application Design

Author: Nicolas Bevacqua
Publisher: Manning
Pages: 344 
ISBN: 9781617291951
Print: 1617291951
Audience: Intermediate JavaScript programmers
Rating: 4
Reviewer: Ian Elliot

Big JavaScript applications are possible and important and this book aims to give you the techniques and tools you [ ... ]

Python 201

Author: Michael Driscoll
Publisher: Leanpub
Pages: 296
ISBN: 978-0996062831
Kindle: B01LMUAYSO
Audience: Python programmers ready to go second level
Rating:  4
Reviewer: Sue Gee

What can you expect from a book on "Intermediate Python"?

More Reviews

Last Updated ( Saturday, 12 August 2017 )

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.