Skip to main content

2018 | Buch

A Beginner's Guide to Scala, Object Orientation and Functional Programming

insite
SUCHEN

Über dieses Buch

Scala is now an established programming language developed by Martin Oderskey and his team at the EPFL. The name Scala is derived from Sca(lable) La(nguage). Scala is a multi-paradigm language, incorporating object oriented approaches with functional programming.

Although some familiarity with standard computing concepts is assumed (such as the idea of compiling a program and executing this compiled from etc.) and with basic procedural language concepts (such as variables and allocation of values to these variables) the early chapters of the book do not assume any familiarity with object orientation nor with functional programming These chapters also step through other concepts with which the reader may not be familiar (such as list processing).

From this background, the book provides a practical introduction to both object and functional approaches using Scala. These concepts are introduced through practical experience taking the reader beyond the level of the language syntax to the philosophy and practice of object oriented development and functional programming.

Students and those actively involved in the software industry will find this comprehensive introduction to Scala invaluable.

Inhaltsverzeichnis

Frontmatter
Chapter 1. Introduction

This book is intended as an introduction to Scala for computer science students or those actively involved in the software industry. It assumes some familiarity with standard computing concepts, such as the idea of compiling a program and executing this compiled form, and with the basics of procedural language concepts such as variables and allocation of values to variables. However, the early chapters of the book do not assume any familiarity with Object Orientation nor functional programming. They also step through other concepts with which the reader may not be familiar (such as list processing). From this background, it provides a practical introduction to object and functional technology using Scala, one of the newest and most interesting programming languages available.

John Hunt
Chapter 2. Elements of Object Orientation

This chapter introduces the core concepts in object orientation. It concisely defines the terminology used and attempts to clarify issues associated with hierarchies. It also discusses some of the perceived strengths and weaknesses of the Object-Oriented approach. It then offers some guidance on the approach to take in learning about objects.

John Hunt
Chapter 3. Why Object Orientation?

The previous chapter introduced the basic concepts behind Object Orientation, the terminology and explored some of the motivation. This chapter looks at how Object Orientation addresses some of the issues that have been raised with procedural languages. To do this it looks at how a small extract of a program might be written in a language such as C, considers the problems faced by the C developer and then looks at how the same functionality might be achieved in an Object Oriented language such as Scala, Java or C#. Again do not worry too much about the syntax you will be presented with, it will be scala but it should not detract from the legibility of the examples.

John Hunt
Chapter 4. Constructing an Object-Oriented System

This chapter takes you through the design of a simple Object-Oriented system without considering implementation issues or the details of any particular language. Instead, this chapter illustrates how to use Object Orientation concepts to construct a software system. We first describe the application and then consider where to start looking for objects, what the objects should do and how they should do it. We conclude by discussing issues such as class inheritance, and answer questions such as “where is the structure of the program?”.

John Hunt
Chapter 5. Functional Programming

Previous chapters have focussed on the Object-Oriented side of the scala language. However, Scala is a Hybrid Object Oriented (OO) and Functional Programming (FP) language. In this chapter, we will now look at Functional Programming and its advantages and disadvantages.

John Hunt
Chapter 6. Scala Background

I first encountered the scala language in 2010. I was working on a graduate training programme for an international banking organisation when I was asked to give an hour talk to the graduates on scala. At that point, I had heard the name mentioned but had no idea what it was. I therefore did some reading, installed the ide being used and tried out some examples—and was hooked.

John Hunt
Chapter 7. A Little Scala

In the last chapter, you learned a little about the history of scala and the scala development environment. In this chapter, you encounter a little of the scala language, what happens when you compile and run a scala program, the scala runtime (Virtual Machine) and the scala ide.

John Hunt
Chapter 8. Scala Building Blocks

This chapter presents an introduction to the Scala programming language. As such, it is not intended to be a comprehensive guide. It introduces the basic elements of the Scala language, including Apps, discusses the concept of classes and instances and how they are defined, presents methods and method definitions and considers constructors and their role.

John Hunt
Chapter 9. Scala Classes

This chapter considers the constructs in Scala used to define classes.

John Hunt
Chapter 10. Scala Methods

This chapter presents how methods and associated behaviour is defined in Scala.

John Hunt
Chapter 11. Packages and Encapsulation

This chapter discusses the encapsulation and packaging features of Scala. The concept of packages is discussed, along with some concrete examples. It then illustrates how the encapsulation facilities can allow quite fine-grained control over the visibility of the elements of your programs.

John Hunt
Chapter 12. Building a Class

In this chapter, we will work through the creation of a simple class to represent a Company. As we are using IntelliJ, we will step through using this IDE to create our class.

John Hunt
Chapter 13. Classes, Inheritance and Abstraction

Inheritance is one of the most powerful features of Object Orientation. It is the difference between an encapsulated language that provides an object-based model and an object-oriented language.

John Hunt
Chapter 14. Objects and Instances

This chapter will discuss the difference between objects in Scala and Instance of a class. This is important as many other Object-Oriented languages use these terms interchangeably. However, in Scala they are significantly different concepts, defined with different language constructs and used in different ways.

John Hunt
Chapter 15. Value Classes

This chapter introduces another type of class in Scala, Value Classes. A Value Class is a type where the actual value being represented by the type class is hold directly by a variable, rather than needing to access that value via a reference (an address in memory). Examples of Value Types include Booelan, Int and Double which can have the values true, false, 32, 45.7, etc. Such values can be held directly by a variable, rather than accessed via a reference. This can be more efficient for simple types like Int.

John Hunt
Chapter 16. Scala Constructs

This chapter presents more of the Scala language. It considers the representation and use of numbers, strings and characters. It also discusses assignments, literals and variables. Finally, it considers messages, message types and their precedence.

John Hunt
Chapter 17. Control and Iteration

This chapter introduces control and iteration in Scala. In Scala, as in many other languages, the mainstay of the control and iteration processes is the if and switch statements and the for and while loops.

John Hunt
Chapter 18. Traits

In the previous chapters there have already been several references to these things called traits. For example, we have encountered the app trait numerous times. However, we have avoided the question “what are traits?”

John Hunt
Chapter 19. Further Traits

This chapter looks at some of the more advanced features associated with the use of traits. The chapter looks at the way in which Traits can be dynamically wrapped around an instance providing a form of aspect-oriented programming (AOP) known as stackable traits. We then look at the role Traits can play in developing reusable behaviour that simplifies the development of new types. Universal Traits used with Value Types are then discussed. The chapter concludes by considering the way in which traits can be used to define a restricted set of values for a given type.

John Hunt
Chapter 20. Arrays

This chapter discusses how arrays are represented in Scala. Arrays are (logically) a continuous set of slots that can hold values or references to instances of a specific type.

John Hunt
Chapter 21. Tuples

In this chapter we introduce Scala tuples, which are useful (and very simple construct) for grouping instances together.

John Hunt
Chapter 22. Functional Programming in Scala

This chapter examines the functional programming features of Scala and looks at how they can be used while the next chapter will look at concepts such as Currying and Partially Applied functions.

John Hunt
Chapter 23. Higher-Order Functions

Functions in Scala are part of the type system. That is, they are part of the system of entities that comprise the types that variables and parameters can reference. Thus just as a parameter can be of type Int, or Boolean, or Person a parameter to a method or a function can be another functions. A function that takes a function as a parameter is referred to as a higher-order function. This chapter discusses higher-order functions and how they can be used and provides some examples taken from the collection classes in Scala.

John Hunt
Chapter 24. Partially Applied Functions and Currying

This chapter looks at two ways in which functions (and in fact methods) in Scala can comprise components of reuse within a software system. These two approaches are partial application of functions and Currying. The two approaches represent variations on a theme. In both cases they allow a function with one or more parameters to have one or more of those parameters bound to a specific value to create a new function with one or more fewer variables.

John Hunt
Chapter 25. Scala Collections Framework

The collections framework is one of the main categories within the set of Scala libraries that you will work with. It provides types (Traits, Objects and Classes) that support various types of data structures (such as lists and maps) and ways to process elements within those structures. This chapter introduces the Scala collections framework.

John Hunt
Chapter 26. Immutable Lists and Maps

This chapter focuses on the List and Map types from the scala.collection.immutable package.

John Hunt
Chapter 27. Immutable and Mutable Collection Packages

This chapter discusses the contents of the Scala collection framework packages for immutable and mutable collections.

John Hunt
Chapter 28. Type Parameterisation

This chapter introduces and discusses type parameterisation. We have already seen examples of type parameterisation in the collection types where a List can be parameterised to hold only Strings, Person objects or integers. In this chapter, we will look at how you can create your own parameterised types and the options available for controlling the types that can be used with those parameterisations.

John Hunt
Chapter 29. Further Language Constructs

This chapter introduces a number of new concepts. The first is the use of implicit. Implicit facilities are a range of language facilities that provide implicit conversions between one type and another (typically in order to access some functionality or to provide compatibility between types). This chapter then introduces Scala annotations (a form of meta data for types), Enumerations and lazy evaluation.

John Hunt
Chapter 30. Exception Handling

This chapter considers exception handling and how it is implemented in Scala. You are introduced to the object model of exception handling, to the throwing and catching of exceptions, and how to define new exceptions and exception-specific constructs.

John Hunt
Chapter 31. Akka Actors

This chapter introduces the concept of an Actor as an approach to the development of concurrent programs. In particular we focus on the Scala Akka implementation of the Actor model based on release 2.5.x (the current release at the time of writing). Note that older versions of Scala also had their own scala.actor implementation of the Actor model—however this is now deprecated and the recommendation is to use the Akka implementation.

John Hunt
Chapter 32. Further Akka Actors

The previous chapter introduced the basic Akka Actor concepts and constructs. This chapter takes these concepts further looking at how a result can be returned by (obtained from) an Actor. It also looks at Actor hierarchies and Actor supervision.

John Hunt
Chapter 33. Scala and JDBC Database Access

In this chapter we will primarily look at how scala can use a database connectivity API referred to as JDBC. Strictly speaking this is available to scala when run on a JVM due to its relationship to the byte code environment of the JVM. Officially JDBC is not an acronym; however, to all intents and purposes it stands for Java DataBase Connectivity.

John Hunt
Chapter 34. Scala Style Database Access

The last chapter introduced how Scala can use JDBC to access a database. However, there are a number of different Scala-based approaches being developed to explore more Scala-like ways of interacting with a database than JDBC. Although JDBC works very well it is far more Java like than Scala like and actually provides a very low level of abstraction (witness the many Object Relational Mapping, or ORM, tools within the Java world such as Hibernate).

John Hunt
Chapter 35. Slick: Functional Relational Mapping for Scala

In this chapter, we will look at Slick. Slick (or in full Scala Language-integrated Connection Kit) is a library that provides access to relational databases from a Scala application. It is heavily based on the functional programming paradigm and as such is much more aligned with the Scala way or programming than the rather procedural approach of JDBC.

John Hunt
Chapter 36. Testing

This chapter considers the different types of tests that you might want to perform with the systems you develop in Scala. It also introduces test-driven development.

John Hunt
Chapter 37. Scala Testing

This chapter examines the various facilities available within Scala to perform a range of tests.

John Hunt
Chapter 38. Play Framework

The Play framework is a lightweight, stateless, asynchronous framework for building Web applications and services.

John Hunt
Chapter 39. RESTful Services

This chapter looks at RESTful Web services as implemented using the Play framework.

John Hunt
Chapter 40. Scalaz

If you start working with Scala you will probably quickly come across the library Scalaz. It is a very widely used set of extensions to the core language. In many cases when people talk about developing systems in Scala they actually mean a combination of Scala and Scalaz.

John Hunt
Chapter 41. GUIs in Scala Swing

This chapter describes how to create rich client graphical displays (desktop application) using the Scala Swing windowing and graphical types. Since Scala 2.11, the Scala wing package I is no longer considered to be a part of Scala’s standard library API and thus some tools may require an additional dependency to be set up to pick it up. This is not the case with IntelliJ’s IDE but is the case with the Simple Build Tool (SBT).

John Hunt
Chapter 42. User Input in Scala Swing

The last chapter looked at various different types of user interface component available within Scala Swing. This chapter now looks at how user input, via those components, can be handled.

John Hunt
Chapter 43. Scala Build Tools

There are many ways in which a Scala application can be built. These include the REPL loop and automated compilation within an IDE such as the Eclipse-based Scala IDE. However, neither of these is suitable for centrally building large applications as might be found within many commercial organisations. The most popular build environments for Scala are Maven and SBT. In this chapter, we will briefly examine both so that you have a flavour of both tools and the potential benefits and drawbacks of each.

John Hunt
Chapter 44. Scala & Java Interoperability

In this chapter, we will look at the interoperation of Java and Scala. Both Java and Scala are JVM byte code languages. That is, they both compile to the byte code language that is understood by the JVM. The byte code language of the JVM was originally designed to be the compiled form of Java and was what the Java Virtual Machine executed. However, things have evolved such that today the JVM is a virtual environment for executing byte code languages. In fact, there are now several languages that can be compiled to JVM byte codes including Java, Groovy, Clojure, Jruby, Jython, JavaScript, Ada, Pascal as well as Scala. A common term used for these languages is that they are all byte code languages.

John Hunt
Metadaten
Titel
A Beginner's Guide to Scala, Object Orientation and Functional Programming
verfasst von
Dr. John Hunt
Copyright-Jahr
2018
Electronic ISBN
978-3-319-75771-1
Print ISBN
978-3-319-75770-4
DOI
https://doi.org/10.1007/978-3-319-75771-1