| 
123
 | 
     1  | 
\documentclass{article}
 | 
| 
 | 
     2  | 
\usepackage{../style}
 | 
| 
 | 
     3  | 
\usepackage{../langs}
 | 
| 
 | 
     4  | 
\usepackage{marvosym}
 | 
| 
 | 
     5  | 
  | 
| 
 | 
     6  | 
%cheat sheet
  | 
| 
 | 
     7  | 
%http://worldline.github.io/scala-cheatsheet/
  | 
| 
 | 
     8  | 
  | 
| 
181
 | 
     9  | 
% case class, apply, unapply
  | 
| 
170
 | 
    10  | 
% see https://medium.com/@thejasbabu/scala-pattern-matching-9c9e73ba9a8a
  | 
| 
 | 
    11  | 
  | 
| 
123
 | 
    12  | 
\begin{document}
 | 
| 
180
 | 
    13  | 
\fnote{\copyright{} Christian Urban, King's College London, 2017, 2018}
 | 
| 
123
 | 
    14  | 
  | 
| 
125
 | 
    15  | 
\section*{A Crash-Course in Scala}
 | 
| 
123
 | 
    16  | 
  | 
| 
182
 | 
    17  | 
\mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{c}ompiled 
 | 
| 
 | 
    18  | 
\underline{a}cademic \underline{la}nguage''}\smallskip\\
 | 
| 
183
 | 
    19  | 
\mbox{}\hfill\textit{ --- a joke on Twitter}\bigskip
 | 
| 
178
 | 
    20  | 
 
  | 
| 
 | 
    21  | 
\noindent
  | 
| 
170
 | 
    22  | 
Scala is a programming language that combines functional and
  | 
| 
 | 
    23  | 
object-oriented programming-styles. It has received quite a bit of
  | 
| 
181
 | 
    24  | 
attention in the last five or so years. One reason for this attention is
  | 
| 
 | 
    25  | 
that, like the Java programming language, Scala compiles to the Java
  | 
| 
 | 
    26  | 
Virtual Machine (JVM) and therefore Scala programs can run under MacOSX,
  | 
| 
182
 | 
    27  | 
Linux and Windows.\footnote{There are also experimental backends of
 | 
| 
 | 
    28  | 
Scala for producing code under Android (\url{http://scala-android.org});
 | 
| 
183
 | 
    29  | 
for generating JavaScript code to build browser applications
  | 
| 
 | 
    30  | 
\url{(https://www.scala-js.org)}; and there is work under way to
 | 
| 
181
 | 
    31  | 
have a native Scala compiler generating X86-code
  | 
| 
182
 | 
    32  | 
(\url{http://www.scala-native.org}).} Because of this it has also access to
 | 
| 
181
 | 
    33  | 
the myriads of Java libraries. Unlike Java, however, Scala often allows
  | 
| 
 | 
    34  | 
programmers to write very concise and elegant code.  Some therefore say:
  | 
| 
182
 | 
    35  | 
``Scala is the better Java''.\footnote{from
 | 
| 
 | 
    36  | 
\url{https://www.slideshare.net/maximnovak/joy-of-scala}} A number
 | 
| 
181
 | 
    37  | 
of companies---the Guardian, Twitter, Coursera, FourSquare, LinkedIn,
  | 
| 
 | 
    38  | 
Netflix to name a few---either use Scala exclusively in production code,
  | 
| 
183
 | 
    39  | 
or at least to some substantial degree. Scala seems also useful in
  | 
| 
181
 | 
    40  | 
job-interviews (especially in Data Science) according to this anecdotal
  | 
| 
 | 
    41  | 
report
  | 
| 
170
 | 
    42  | 
  | 
| 
181
 | 
    43  | 
\begin{quote}
 | 
| 
 | 
    44  | 
\url{http://techcrunch.com/2016/06/14/scala-is-the-new-golden-child}
 | 
| 
170
 | 
    45  | 
\end{quote}
 | 
| 
 | 
    46  | 
  | 
| 
 | 
    47  | 
\noindent
  | 
| 
 | 
    48  | 
The official Scala compiler can be downloaded from
  | 
| 
 | 
    49  | 
  | 
| 
 | 
    50  | 
\begin{quote}
 | 
| 
 | 
    51  | 
\url{http://www.scala-lang.org}
 | 
| 
 | 
    52  | 
\end{quote}
 | 
| 
 | 
    53  | 
  | 
| 
 | 
    54  | 
\noindent
  | 
| 
181
 | 
    55  | 
I found a convenient IDE for Scala programming is Microsoft's
  | 
| 
 | 
    56  | 
\textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and
 | 
| 
 | 
    57  | 
obviously Windows. It can be downloaded for free from
  | 
| 
 | 
    58  | 
  | 
| 
 | 
    59  | 
\begin{quote}
 | 
| 
 | 
    60  | 
\url{https://code.visualstudio.com}
 | 
| 
 | 
    61  | 
\end{quote}
 | 
| 
 | 
    62  | 
  | 
| 
 | 
    63  | 
\noindent
  | 
| 
 | 
    64  | 
and should already come pre-installed in the Department (together with
  | 
| 
182
 | 
    65  | 
the Scala compiler). VS Code is far from perfect, however it includes a
  | 
| 
 | 
    66  | 
\textit{Marketplace} from which a multitude of extensions can be
 | 
| 
 | 
    67  | 
downloaded that make editing and running Scala code easier (see
  | 
| 
 | 
    68  | 
Figure~\ref{vscode} for my own setup).
 | 
| 
181
 | 
    69  | 
  | 
| 
 | 
    70  | 
\begin{figure}[t]
 | 
| 
 | 
    71  | 
\begin{center}  
 | 
| 
 | 
    72  | 
\includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{}
 | 
| 
 | 
    73  | 
\end{center}
 | 
| 
182
 | 
    74  | 
\caption{My personal installation of VS Code includes the following
 | 
| 
 | 
    75  | 
packages from Marketplace: Scala Syntax (official), Code Runner, Code
  | 
| 
 | 
    76  | 
Spell Checker, Rewrap and Subtle Match Brackets. I have also bound keys
  | 
| 
 | 
    77  | 
\keys{\^{}} \keys{Ret} to the action
 | 
| 
 | 
    78  | 
``Run-Selected-Text-In-Active-Terminal'' in order to quickly evaluate
  | 
| 
 | 
    79  | 
small code snippets in the Scala REPL.\label{vscode}}
 | 
| 
181
 | 
    80  | 
\end{figure}  
 | 
| 
 | 
    81  | 
  | 
| 
 | 
    82  | 
What I like most about VS Code is that it provides an easy access to the
  | 
| 
 | 
    83  | 
Scala REPL. But if you prefer your own editor for coding, it
  | 
| 
182
 | 
    84  | 
is also painless to work with Scala completely on the command line (like you
  | 
| 
181
 | 
    85  | 
might have done with \texttt{g++} in the earlier part of PEP). For the
 | 
| 
 | 
    86  | 
lazybones among us, there is even an online editor and environment for
  | 
| 
 | 
    87  | 
developing and running Scala programs called \textit{ScalaFiddle}, which
 | 
| 
182
 | 
    88  | 
requires zero setup (assuming you have a browser handy). You can access
  | 
| 
 | 
    89  | 
it from:
  | 
| 
181
 | 
    90  | 
 
  | 
| 
 | 
    91  | 
\begin{quote}
 | 
| 
 | 
    92  | 
\url{https://scalafiddle.io}
 | 
| 
 | 
    93  | 
\end{quote}
 | 
| 
 | 
    94  | 
  
  | 
| 
 | 
    95  | 
  | 
| 
183
 | 
    96  | 
Scala can be used with the heavy-duty IDEs Eclipse and IntelliJ.
  | 
| 
182
 | 
    97  | 
A ready-made Scala bundle for Eclipse is available from
  | 
| 
170
 | 
    98  | 
  | 
| 
 | 
    99  | 
\begin{quote}
 | 
| 
 | 
   100  | 
\url{http://scala-ide.org/download/sdk.html}
 | 
| 
 | 
   101  | 
\end{quote}
 | 
| 
 | 
   102  | 
  | 
| 
 | 
   103  | 
\noindent
  | 
| 
181
 | 
   104  | 
Also IntelliJ includes plugins for Scala. \textbf{BUT}, I do not
 | 
| 
182
 | 
   105  | 
recommend the usage of either Eclipse or IntelliJ for PEP: these IDEs
  | 
| 
 | 
   106  | 
seem to make your life harder, rather than easier, for the small
  | 
| 
 | 
   107  | 
programs we will write in this module. They are really meant to be used
  | 
| 
 | 
   108  | 
when you have a million-lines codebase, rather than our
  | 
| 
 | 
   109  | 
``toy-programs''\ldots{}for example why on earth am I required to create a
 | 
| 
 | 
   110  | 
completely new project with several subdirectories when I just want to
  | 
| 
 | 
   111  | 
try out 20-lines of Scala code? ;o)
  | 
| 
 | 
   112  | 
  | 
| 
 | 
   113  | 
\subsection*{Why Functional Programming?}
 | 
| 
 | 
   114  | 
  | 
| 
 | 
   115  | 
Before we go on, let me explain a bit more why we want to inflict
  | 
| 
 | 
   116  | 
upon you another programming language. You hopefully have mastered Java and
  | 
| 
 | 
   117  | 
C++\ldots{}the world should be your oyster, no? Well, it is not that
 | 
| 
 | 
   118  | 
easy. We require Scala in PEP, but actually we do not deeply care
  | 
| 
 | 
   119  | 
whether you learn Scala---after all it is just a programming language
  | 
| 
 | 
   120  | 
(albeit a nifty one IMHO). What we do care about is that you learn about
  | 
| 
 | 
   121  | 
\textit{functional programming}. Scala is just the vehicle for that.
 | 
| 
 | 
   122  | 
  | 
| 
 | 
   123  | 
Very likely writing programs in a functional programming language is
  | 
| 
183
 | 
   124  | 
quite different from what you are  used to in your study so far. It
  | 
| 
 | 
   125  | 
might even be totally alien to you. The reason is that functional
  | 
| 
 | 
   126  | 
programming seems to go against the core principles of
  | 
| 
 | 
   127  | 
\textit{imperative programming} (which is what you do in Java and C++
 | 
| 
 | 
   128  | 
for example). The main idea of imperative programming  is that you have
  | 
| 
 | 
   129  | 
some form of ``state'' in your program and you continuously change this
  | 
| 
 | 
   130  | 
state by issuing some commands (e.g.~updating a field in an array,
  | 
| 
 | 
   131  | 
adding one to a variable and so on). The classic example for this style
  | 
| 
 | 
   132  | 
of programming is a \texttt{for}-loop, say
 | 
| 
182
 | 
   133  | 
  | 
| 
 | 
   134  | 
\begin{lstlisting}[language=C,numbers=none]
 | 
| 
 | 
   135  | 
  for (int i = 10; i < 20; i++) { 
 | 
| 
 | 
   136  | 
      ...Do something interesting with i...
  | 
| 
 | 
   137  | 
   }
  | 
| 
 | 
   138  | 
\end{lstlisting}
 | 
| 
 | 
   139  | 
  | 
| 
 | 
   140  | 
\noindent Here the variable \texttt{i} embodies the state, which is
 | 
| 
 | 
   141  | 
first set to \texttt{10} and then increased by one in each
 | 
| 
183
 | 
   142  | 
loop-iteration until it reaches \texttt{20} when the loop is exited.
 | 
| 
 | 
   143  | 
When this code is compiled and actually runs, there will be some
  | 
| 
 | 
   144  | 
dedicated space reserved in memory which contains the value of
  | 
| 
 | 
   145  | 
\texttt{i}\ldots\texttt{10} at the beginning, and then the content will
 | 
| 
 | 
   146  | 
be updated, or replaced, by some new content in every iteration.  The
  | 
| 
 | 
   147  | 
main point here is that this kind of updating, or manipulating, memory 
  | 
| 
 | 
   148  | 
is \textbf{PURE EVIL}!!
 | 
| 
182
 | 
   149  | 
  | 
| 
 | 
   150  | 
\noindent
  | 
| 
 | 
   151  | 
\ldots{}Well, it is perfectly benign if you have a sequential program
 | 
| 
 | 
   152  | 
that gets run instruction by instruction...nicely one after another.
  | 
| 
 | 
   153  | 
This kind of running code uses a single core of your CPU and goes as
  | 
| 
 | 
   154  | 
fast as your CPU frequency (or clock-speed) allows. Unfortunately, this
  | 
| 
183
 | 
   155  | 
clock-speed has not much increased over the past few years and no
  | 
| 
 | 
   156  | 
dramatic increases are predicted any time soon. So you are a bit stuck,
  | 
| 
 | 
   157  | 
unlike previous generations of developers who could rely upon the fact
  | 
| 
 | 
   158  | 
that every 2 years or so their code run twice as fast (in ideal
  | 
| 
 | 
   159  | 
circumstances) because the clock-speed of their CPUs got twice as fast.
  | 
| 
 | 
   160  | 
This unfortunately does not happen any more nowadays. To get you out of
  | 
| 
 | 
   161  | 
this embarrassing situation, CPU producers pile more and more cores into
  | 
| 
 | 
   162  | 
CPUs in order to make them more powerful and potentially make software
  | 
| 
182
 | 
   163  | 
faster. The task for you as developer is to take somehow advantage of
  | 
| 
 | 
   164  | 
these cores by running as much of your code as possible in parallel on
  | 
| 
 | 
   165  | 
as many core you have available (typically 4 in modern laptops and
  | 
| 
183
 | 
   166  | 
sometimes much more on high-end machines). In this situation,
  | 
| 
 | 
   167  | 
\textit{mutable} variables like \texttt{i} above are evil, or at least a
 | 
| 
 | 
   168  | 
major nuisance. Because if you want to distribute some of the
  | 
| 
 | 
   169  | 
loop-iterations over the cores that are currently idle in your system,
  | 
| 
 | 
   170  | 
you need to be extremely careful about who can read and write (update)
  | 
| 
 | 
   171  | 
the variable \texttt{i}.\footnote{If you are of the belief that nothing
 | 
| 
 | 
   172  | 
nasty can happen to \texttt{i} inside the \texttt{for}-loop, then you
 | 
| 
 | 
   173  | 
need to go back over the C++ material.} Especially the writing operation
  | 
| 
 | 
   174  | 
is critical because you do not want that conflicting writes mess about
  | 
| 
 | 
   175  | 
with \texttt{i}. An untold amount of misery has arisen from this
 | 
| 
 | 
   176  | 
problem. The catch is that if you try to solve this problem in C++ or
  | 
| 
 | 
   177  | 
Java, and be as defensive as possible about reads and writes to
  | 
| 
 | 
   178  | 
\texttt{i}, then you need to synchronise access to it and as a result
 | 
| 
 | 
   179  | 
your program more often than not waits more than it runs, thereby
  | 
| 
 | 
   180  | 
defeating the point of trying to run the program in parallel in the
  | 
| 
 | 
   181  | 
first place. If you are less defensive, then usually all hell breaks
  | 
| 
 | 
   182  | 
loose by seemingly obtaining random results. And forget the idea of
  | 
| 
 | 
   183  | 
being able to debug such code.
  | 
| 
182
 | 
   184  | 
  | 
| 
 | 
   185  | 
The idea of functional programming is to eliminate any state from
  | 
| 
183
 | 
   186  | 
programs. Because then it is easy to parallelize the resulting programs:
  | 
| 
 | 
   187  | 
if you do not have any state, then once created all memory content stays
  | 
| 
 | 
   188  | 
unchanged and reads to such memory are absolutely safe without the need
  | 
| 
 | 
   189  | 
of any synchronisations. An example is given in Figure~\ref{mand} where
 | 
| 
 | 
   190  | 
in the absence of annoying state, Scala makes it easy to calculate the
  | 
| 
 | 
   191  | 
Mandelbrot set on as many cores of your CPU as possible. Why is it so
  | 
| 
 | 
   192  | 
easy in this example? Because each pixel in the Mandelbrot set can be
  | 
| 
 | 
   193  | 
calculated independently and the calculation does not need to update any
  | 
| 
 | 
   194  | 
variable. It is so easy in fact, that going from the sequential version
  | 
| 
 | 
   195  | 
of the program to the parallel version can be done by adding just eight
  | 
| 
 | 
   196  | 
characters. What is not to be liked about that (try the same in C++)?
  | 
| 
182
 | 
   197  | 
  | 
| 
 | 
   198  | 
\begin{figure}[p]
 | 
| 
183
 | 
   199  | 
  \includegraphics[scale=0.15]{../pics/mand1.png}
 | 
| 
182
 | 
   200  | 
  | 
| 
183
 | 
   201  | 
  \includegraphics[scale=0.15]{../pics/mand4.png}
 | 
| 
 | 
   202  | 
  \includegraphics[scale=0.15]{../pics/mand3.png}
 | 
| 
 | 
   203  | 
\caption{\label{mand}}
 | 
| 
182
 | 
   204  | 
\end{figure}  
 | 
| 
 | 
   205  | 
  | 
| 
183
 | 
   206  | 
But remember that this easy parallelisation of code requires that we
  | 
| 
 | 
   207  | 
have no state in our program\ldots{} that is no counters like\texttt{i}
 | 
| 
 | 
   208  | 
in \texttt{for}-loops. You might then ask, how do I write loops without
 | 
| 
 | 
   209  | 
such counters? Well, teaching you that this is possible is the main
  | 
| 
 | 
   210  | 
point of the Scala-part in PEP. I can assure you it is possible, but you
  | 
| 
 | 
   211  | 
have to get your head around it. Once you mastered this, it will be fun
  | 
| 
 | 
   212  | 
to have no state in your programs (a side product is that it much easier
  | 
| 
 | 
   213  | 
to debug state-less code; and the memory we might waste by not allowing
  | 
| 
 | 
   214  | 
in-place updates is taken care of by the memory garbage collector of
  | 
| 
 | 
   215  | 
Java and Scala).
  | 
| 
182
 | 
   216  | 
  | 
| 
 | 
   217  | 
  | 
| 
170
 | 
   218  | 
  | 
| 
123
 | 
   219  | 
\subsection*{The Very Basics}
 | 
| 
 | 
   220  | 
  | 
| 
 | 
   221  | 
One advantage of Scala over Java is that it includes an interpreter (a
  | 
| 
 | 
   222  | 
REPL, or
  | 
| 
 | 
   223  | 
\underline{R}ead-\underline{E}val-\underline{P}rint-\underline{L}oop)
 | 
| 
181
 | 
   224  | 
with which you can run and test small code snippets without the need
  | 
| 
123
 | 
   225  | 
of a compiler. This helps a lot with interactively developing
  | 
| 
181
 | 
   226  | 
programs. This is really my preferred way of writing small Scala
  | 
| 
123
 | 
   227  | 
programs. Once you installed Scala, you can start the interpreter by
  | 
| 
 | 
   228  | 
typing on the command line:
  | 
| 
 | 
   229  | 
  | 
| 
 | 
   230  | 
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
 | 
| 
 | 
   231  | 
$ scala
  | 
| 
181
 | 
   232  | 
Welcome to Scala 2.12.6 (Java HotSpot(TM) 64-Bit Server VM, Java 9).
  | 
| 
123
 | 
   233  | 
Type in expressions for evaluation. Or try :help.
  | 
| 
 | 
   234  | 
  | 
| 
 | 
   235  | 
scala>
  | 
| 
 | 
   236  | 
\end{lstlisting}%$
 | 
| 
 | 
   237  | 
  | 
| 
 | 
   238  | 
\noindent The precise response may vary depending
  | 
| 
 | 
   239  | 
on the version and platform where you installed Scala. At the Scala
  | 
| 
 | 
   240  | 
prompt you can type things like \code{2 + 3}\;\keys{Ret} and
 | 
| 
 | 
   241  | 
the output will be
  | 
| 
 | 
   242  | 
  | 
| 
 | 
   243  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   244  | 
scala> 2 + 3
  | 
| 
 | 
   245  | 
res0: Int = 5
  | 
| 
 | 
   246  | 
\end{lstlisting}
 | 
| 
 | 
   247  | 
  | 
| 
124
 | 
   248  | 
\noindent indicating that the result of the addition is of type
  | 
| 
 | 
   249  | 
\code{Int} and the actual result is 5; \code{res0} is a name that
 | 
| 
125
 | 
   250  | 
Scala gives automatically to the result. You can reuse this name later
  | 
| 
181
 | 
   251  | 
on. 
  | 
| 
 | 
   252  | 
  | 
| 
 | 
   253  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   254  | 
scala> res0 + 4
  | 
| 
 | 
   255  | 
res1: Int = 9
  | 
| 
 | 
   256  | 
\end{lstlisting}
 | 
| 
 | 
   257  | 
  | 
| 
 | 
   258  | 
\noindent
  | 
| 
 | 
   259  | 
Another classic example you can try out is
  | 
| 
123
 | 
   260  | 
  | 
| 
 | 
   261  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   262  | 
scala> print("hello world")
 | 
| 
 | 
   263  | 
hello world
  | 
| 
 | 
   264  | 
\end{lstlisting}
 | 
| 
 | 
   265  | 
  | 
| 
 | 
   266  | 
\noindent Note that in this case there is no result. The
  | 
| 
 | 
   267  | 
reason is that \code{print} does not actually produce a result
 | 
| 
124
 | 
   268  | 
(there is no \code{resX} and no type), rather it is a
 | 
| 
123
 | 
   269  | 
function that causes the \emph{side-effect} of printing out a
 | 
| 
 | 
   270  | 
string. Once you are more familiar with the functional
  | 
| 
 | 
   271  | 
programming-style, you will know what the difference is
  | 
| 
 | 
   272  | 
between a function that returns a result, like addition, and a
  | 
| 
 | 
   273  | 
function that causes a side-effect, like \code{print}. We
 | 
| 
 | 
   274  | 
shall come back to this point later, but if you are curious
  | 
| 
 | 
   275  | 
now, the latter kind of functions always has \code{Unit} as
 | 
| 
124
 | 
   276  | 
return type. It is just not printed.
  | 
| 
123
 | 
   277  | 
  | 
| 
181
 | 
   278  | 
You can try more examples with the Scala REPL, but feel free to
  | 
| 
 | 
   279  | 
first guess what the result is (not all answers by Scala are obvious):
  | 
| 
123
 | 
   280  | 
  | 
| 
 | 
   281  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   282  | 
scala> 2 + 2
  | 
| 
 | 
   283  | 
scala> 1 / 2
  | 
| 
 | 
   284  | 
scala> 1.0 / 2
  | 
| 
 | 
   285  | 
scala> 1 / 2.0
  | 
| 
 | 
   286  | 
scala> 1 / 0
  | 
| 
 | 
   287  | 
scala> 1.0 / 0.0
  | 
| 
 | 
   288  | 
scala> true == false
  | 
| 
 | 
   289  | 
scala> true && false
  | 
| 
 | 
   290  | 
scala> 1 > 1.0
  | 
| 
 | 
   291  | 
scala> "12345".length
  | 
| 
181
 | 
   292  | 
scala> List(1,2,1).size
  | 
| 
 | 
   293  | 
scala> Set(1,2,1).size
  | 
| 
 | 
   294  | 
\end{lstlisting}\smallskip
 | 
| 
123
 | 
   295  | 
  | 
| 
181
 | 
   296  | 
\noindent
  | 
| 
 | 
   297  | 
Please take the Scala REPL seriously: If you want to take advantage of my
  | 
| 
 | 
   298  | 
reference implementation for the assignments, you will need to be
  | 
| 
 | 
   299  | 
able to ``play around'' with it!
  | 
| 
 | 
   300  | 
  | 
| 
 | 
   301  | 
\subsection*{Standalone Scala Apps}
 | 
| 
123
 | 
   302  | 
  | 
| 
 | 
   303  | 
If you want to write a stand-alone app in Scala, you can
  | 
| 
 | 
   304  | 
implement an object that is an instance of \code{App}, say
 | 
| 
 | 
   305  | 
  | 
| 
 | 
   306  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   307  | 
object Hello extends App {
 | 
| 
 | 
   308  | 
    println("hello world")
 | 
| 
 | 
   309  | 
}
  | 
| 
 | 
   310  | 
\end{lstlisting}
 | 
| 
 | 
   311  | 
  | 
| 
124
 | 
   312  | 
\noindent save it in a file, for example {\tt hello-world.scala}, and
 | 
| 
181
 | 
   313  | 
then run the compiler (\texttt{scalac}) and followed by the runtime
 | 
| 
 | 
   314  | 
environment (\texttt{scala}):
 | 
| 
123
 | 
   315  | 
  | 
| 
 | 
   316  | 
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
 | 
| 
 | 
   317  | 
$ scalac hello-world.scala
  | 
| 
 | 
   318  | 
$ scala Hello
  | 
| 
 | 
   319  | 
hello world
  | 
| 
 | 
   320  | 
\end{lstlisting}
 | 
| 
 | 
   321  | 
  | 
| 
124
 | 
   322  | 
\noindent
  | 
| 
123
 | 
   323  | 
Like Java, Scala targets the JVM and consequently
  | 
| 
 | 
   324  | 
Scala programs can also be executed by the bog-standard Java
  | 
| 
 | 
   325  | 
Runtime. This only requires the inclusion of {\tt
 | 
| 
 | 
   326  | 
scala-library.jar}, which on my computer can be done as
  | 
| 
 | 
   327  | 
follows:
  | 
| 
 | 
   328  | 
  | 
| 
 | 
   329  | 
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
 | 
| 
 | 
   330  | 
$ scalac hello-world.scala
  | 
| 
 | 
   331  | 
$ java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello
  | 
| 
 | 
   332  | 
hello world
  | 
| 
 | 
   333  | 
\end{lstlisting}
 | 
| 
 | 
   334  | 
  | 
| 
 | 
   335  | 
\noindent You might need to adapt the path to where you have
  | 
| 
 | 
   336  | 
installed Scala.
  | 
| 
 | 
   337  | 
  | 
| 
 | 
   338  | 
\subsection*{Values}
 | 
| 
 | 
   339  | 
  | 
| 
124
 | 
   340  | 
In the lectures I will try to avoid as much as possible the term
  | 
| 
 | 
   341  | 
\emph{variables} familiar from other programming languages. The reason
 | 
| 
 | 
   342  | 
is that Scala has \emph{values}, which can be seen as abbreviations of
 | 
| 
 | 
   343  | 
larger expressions. For example
  | 
| 
123
 | 
   344  | 
  | 
| 
 | 
   345  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   346  | 
scala> val x = 42
  | 
| 
 | 
   347  | 
x: Int = 42
  | 
| 
 | 
   348  | 
  | 
| 
 | 
   349  | 
scala> val y = 3 + 4
  | 
| 
 | 
   350  | 
y: Int = 7
  | 
| 
 | 
   351  | 
  | 
| 
 | 
   352  | 
scala> val z = x / y
  | 
| 
 | 
   353  | 
z: Int = 6
  | 
| 
 | 
   354  | 
\end{lstlisting}
 | 
| 
 | 
   355  | 
  | 
| 
 | 
   356  | 
\noindent
  | 
| 
181
 | 
   357  | 
Why the kerfuffle about values? Well, values are \emph{immutable}. You 
 | 
| 
 | 
   358  | 
cannot change their value after you defined them. If you try to reassign
  | 
| 
124
 | 
   359  | 
\code{z} above, Scala will yell at you:
 | 
| 
123
 | 
   360  | 
  | 
| 
 | 
   361  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   362  | 
scala> z = 9
  | 
| 
 | 
   363  | 
error: reassignment to val
  | 
| 
 | 
   364  | 
       z = 9
  | 
| 
 | 
   365  | 
         ^
  | 
| 
 | 
   366  | 
\end{lstlisting}
 | 
| 
 | 
   367  | 
  | 
| 
 | 
   368  | 
\noindent
  | 
| 
 | 
   369  | 
So it would be a bit absurd to call values as variables...you cannot
  | 
| 
181
 | 
   370  | 
change them; they cannot vary. You might think you can re-assign them like
  | 
| 
123
 | 
   371  | 
  | 
| 
 | 
   372  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   373  | 
scala> val x = 42
  | 
| 
 | 
   374  | 
scala> val z = x / 7
  | 
| 
 | 
   375  | 
scala> val x = 70
  | 
| 
 | 
   376  | 
scala> println(z) 
  | 
| 
 | 
   377  | 
\end{lstlisting}
 | 
| 
 | 
   378  | 
  | 
| 
124
 | 
   379  | 
\noindent but try to guess what Scala will print out 
  | 
| 
123
 | 
   380  | 
for \code{z}?  Will it be \code{6} or \code{10}? A final word about
 | 
| 
 | 
   381  | 
values: Try to stick to the convention that names of values should be
  | 
| 
 | 
   382  | 
lower case, like \code{x}, \code{y}, \code{foo41} and so on.
 | 
| 
 | 
   383  | 
  | 
| 
 | 
   384  | 
  | 
| 
 | 
   385  | 
\subsection*{Function Definitions}
 | 
| 
 | 
   386  | 
  | 
| 
181
 | 
   387  | 
We do functional programming! So defining functions will be our main occupation.
  | 
| 
182
 | 
   388  | 
As an example, a function named \code{f} taking a single argument of type 
 | 
| 
181
 | 
   389  | 
\code{Int} can be defined in Scala as follows:
 | 
| 
123
 | 
   390  | 
  | 
| 
 | 
   391  | 
\begin{lstlisting}[numbers=none]
 | 
| 
181
 | 
   392  | 
def f(x: Int) : String = ...EXPR...
  | 
| 
123
 | 
   393  | 
\end{lstlisting} 
 | 
| 
 | 
   394  | 
  | 
| 
 | 
   395  | 
\noindent
  | 
| 
124
 | 
   396  | 
This function returns the value resulting from evaluating the expression
  | 
| 
123
 | 
   397  | 
\code{EXPR} (whatever is substituted for this). The result will be
 | 
| 
181
 | 
   398  | 
of type \code{String}. It is a good habit to include this information
 | 
| 
124
 | 
   399  | 
about the return type always. Simple examples of Scala functions are:
  | 
| 
123
 | 
   400  | 
  | 
| 
 | 
   401  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   402  | 
def incr(x: Int) : Int = x + 1
  | 
| 
 | 
   403  | 
def double(x: Int) : Int = x + x
  | 
| 
 | 
   404  | 
def square(x: Int) : Int = x * x
  | 
| 
 | 
   405  | 
\end{lstlisting}
 | 
| 
 | 
   406  | 
  | 
| 
 | 
   407  | 
\noindent
  | 
| 
 | 
   408  | 
The general scheme for a function is
  | 
| 
 | 
   409  | 
  | 
| 
 | 
   410  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   411  | 
def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
 | 
| 
 | 
   412  | 
  BODY
  | 
| 
 | 
   413  | 
}
  | 
| 
 | 
   414  | 
\end{lstlisting}
 | 
| 
 | 
   415  | 
  | 
| 
 | 
   416  | 
\noindent
  | 
| 
 | 
   417  | 
where each argument requires its type and the result type of the
  | 
| 
124
 | 
   418  | 
function, \code{rty}, should be given. If the body of the function is
 | 
| 
 | 
   419  | 
more complex, then it can be enclosed in braces, like above. If it it
  | 
| 
 | 
   420  | 
is just a simple expression, like \code{x + 1}, you can omit the
 | 
| 
 | 
   421  | 
braces. Very often functions are recursive (call themselves) like
  | 
| 
 | 
   422  | 
the venerable factorial function.
  | 
| 
123
 | 
   423  | 
  | 
| 
 | 
   424  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   425  | 
def fact(n: Int): Int = 
  | 
| 
 | 
   426  | 
  if (n == 0) 1 else n * fact(n - 1)
  | 
| 
 | 
   427  | 
\end{lstlisting}
 | 
| 
 | 
   428  | 
  
  | 
| 
 | 
   429  | 
\subsection*{Loops, or better the Absence thereof}
 | 
| 
 | 
   430  | 
  | 
| 
 | 
   431  | 
Coming from Java or C++, you might be surprised that Scala does
  | 
| 
 | 
   432  | 
not really have loops. It has instead, what is in functional
  | 
| 
 | 
   433  | 
programming called, \emph{maps}. To illustrate how they work,
 | 
| 
 | 
   434  | 
let us assume you have a list of numbers from 1 to 8 and want to
  | 
| 
 | 
   435  | 
build the list of squares. The list of numbers from 1 to 8 
  | 
| 
 | 
   436  | 
can be constructed in Scala as follows:
  | 
| 
 | 
   437  | 
  | 
| 
 | 
   438  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   439  | 
scala> (1 to 8).toList
  | 
| 
 | 
   440  | 
res1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8)
  | 
| 
 | 
   441  | 
\end{lstlisting}
 | 
| 
 | 
   442  | 
  | 
| 
 | 
   443  | 
\noindent Generating from this list, the list of squares in a
  | 
| 
 | 
   444  | 
programming language such as Java, you would assume the list
  | 
| 
 | 
   445  | 
is given as a kind of array. You would then iterate, or loop,
  | 
| 
 | 
   446  | 
an index over this array and replace each entry in the array
  | 
| 
 | 
   447  | 
by the square. Right? In Scala, and in other functional
  | 
| 
 | 
   448  | 
programming languages, you use maps to achieve the same. 
  | 
| 
 | 
   449  | 
  | 
| 
 | 
   450  | 
A map essentially takes a function that describes how each
  | 
| 
 | 
   451  | 
element is transformed (for example squared) and a list over
  | 
| 
 | 
   452  | 
which this function should work. There are two forms to
  | 
| 
 | 
   453  | 
express such maps in Scala. The first way is called a
  | 
| 
 | 
   454  | 
\emph{for-comprehension}. Squaring the numbers from 1 to 8
 | 
| 
 | 
   455  | 
would look as follows:
  | 
| 
 | 
   456  | 
  | 
| 
 | 
   457  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   458  | 
scala> for (n <- (1 to 8).toList) yield n * n
  | 
| 
 | 
   459  | 
res2: List[Int] = List(1, 4, 9, 16, 25, 36, 49, 64)
  | 
| 
 | 
   460  | 
\end{lstlisting}
 | 
| 
 | 
   461  | 
  | 
| 
 | 
   462  | 
\noindent The important keywords are \code{for} and
 | 
| 
 | 
   463  | 
\code{yield}. This for-comprehension roughly states that from
 | 
| 
 | 
   464  | 
the list of numbers we draw \code{n}s and compute the result
 | 
| 
 | 
   465  | 
of \code{n * n}. As you can see, we specified the list where
 | 
| 
 | 
   466  | 
each \code{n} comes from, namely \code{(1 to 8).toList}, and
 | 
| 
 | 
   467  | 
how each element needs to be transformed. This can also be
  | 
| 
 | 
   468  | 
expressed in a second way in Scala by using directly
  | 
| 
 | 
   469  | 
\code{map}s as follows:
 | 
| 
 | 
   470  | 
  | 
| 
 | 
   471  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   472  | 
scala> (1 to 8).toList.map(n => n * n)
  | 
| 
 | 
   473  | 
res3 = List(1, 4, 9, 16, 25, 36, 49, 64)
  | 
| 
 | 
   474  | 
\end{lstlisting}
 | 
| 
 | 
   475  | 
  | 
| 
 | 
   476  | 
\noindent In this way, the expression \code{n => n * n} stands
 | 
| 
 | 
   477  | 
for the function that calculates the square (this is how the
  | 
| 
 | 
   478  | 
\code{n}s are transformed). This expression for functions
 | 
| 
 | 
   479  | 
might remind you of your lessons about the lambda-calculus
  | 
| 
 | 
   480  | 
where this would have been written as $\lambda n.\,n * n$. It
  | 
| 
 | 
   481  | 
might not be obvious, but for-comprehensions are just
  | 
| 
 | 
   482  | 
syntactic sugar: when compiling, Scala translates
  | 
| 
 | 
   483  | 
for-comprehensions into equivalent maps. This even works
  | 
| 
 | 
   484  | 
when for-comprehensions get more complicated (see below).
  | 
| 
 | 
   485  | 
  | 
| 
 | 
   486  | 
The very charming feature of Scala is that such maps or
  | 
| 
 | 
   487  | 
for-comprehensions can be written for any kind of data
  | 
| 
 | 
   488  | 
collection, such as lists, sets, vectors, options and so on.
  | 
| 
 | 
   489  | 
For example if we instead compute the reminders modulo 3 of
  | 
| 
 | 
   490  | 
this list, we can write
  | 
| 
 | 
   491  | 
  | 
| 
 | 
   492  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   493  | 
scala> (1 to 8).toList.map(n => n % 3)
  | 
| 
 | 
   494  | 
res4 = List(1, 2, 0, 1, 2, 0, 1, 2)
  | 
| 
 | 
   495  | 
\end{lstlisting}
 | 
| 
 | 
   496  | 
  | 
| 
 | 
   497  | 
\noindent If we, however, transform the numbers 1 to 8 not
  | 
| 
 | 
   498  | 
into a list, but into a set, and then compute the reminders
  | 
| 
 | 
   499  | 
modulo 3 we obtain
  | 
| 
 | 
   500  | 
  | 
| 
 | 
   501  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   502  | 
scala> (1 to 8).toSet[Int].map(n => n % 3)
  | 
| 
 | 
   503  | 
res5 = Set(2, 1, 0)
  | 
| 
 | 
   504  | 
\end{lstlisting}
 | 
| 
 | 
   505  | 
  | 
| 
 | 
   506  | 
\noindent This is the correct result for sets, as there are
  | 
| 
 | 
   507  | 
only three equivalence classes of integers modulo 3. Note that
  | 
| 
 | 
   508  | 
in this example we need to ``help'' Scala to transform the
  | 
| 
 | 
   509  | 
numbers into a set of integers by explicitly annotating the
  | 
| 
 | 
   510  | 
type \code{Int}. Since maps and for-comprehensions are
 | 
| 
 | 
   511  | 
just syntactic variants of each other, the latter can also be
  | 
| 
 | 
   512  | 
written as
  | 
| 
 | 
   513  | 
  | 
| 
 | 
   514  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   515  | 
scala> for (n <- (1 to 8).toSet[Int]) yield n % 3
  | 
| 
 | 
   516  | 
res5 = Set(2, 1, 0)
  | 
| 
 | 
   517  | 
\end{lstlisting}
 | 
| 
 | 
   518  | 
  | 
| 
 | 
   519  | 
For-comprehensions can also be nested and the selection of 
  | 
| 
 | 
   520  | 
elements can be guarded. For example if we want to pair up
  | 
| 
 | 
   521  | 
the numbers 1 to 4 with the letters a to c, we can write
  | 
| 
 | 
   522  | 
  | 
| 
 | 
   523  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   524  | 
scala> for (n <- (1 to 4).toList; 
  | 
| 
 | 
   525  | 
            m <- ('a' to 'c').toList) yield (n, m)
 | 
| 
 | 
   526  | 
res6 = List((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), 
  | 
| 
 | 
   527  | 
            (3,a), (3,b), (3,c), (4,a), (4,b), (4,c))
  | 
| 
 | 
   528  | 
\end{lstlisting}
 | 
| 
 | 
   529  | 
  | 
| 
 | 
   530  | 
\noindent 
  | 
| 
 | 
   531  | 
Or if we want to find all pairs of numbers between 1 and 3
  | 
| 
 | 
   532  | 
where the sum is an even number, we can write
  | 
| 
 | 
   533  | 
  | 
| 
 | 
   534  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   535  | 
scala> for (n <- (1 to 3).toList; 
  | 
| 
 | 
   536  | 
            m <- (1 to 3).toList;
  | 
| 
 | 
   537  | 
            if (n + m) % 2 == 0) yield (n, m)
  | 
| 
 | 
   538  | 
res7 = List((1,1), (1,3), (2,2), (3,1), (3,3))
  | 
| 
 | 
   539  | 
\end{lstlisting}
 | 
| 
 | 
   540  | 
  | 
| 
 | 
   541  | 
\noindent The \code{if}-condition in the for-comprehension
 | 
| 
 | 
   542  | 
filters out all pairs where the sum is not even.
  | 
| 
 | 
   543  | 
  | 
| 
 | 
   544  | 
While hopefully this all looks reasonable, there is one
  | 
| 
 | 
   545  | 
complication: In the examples above we always wanted to
  | 
| 
 | 
   546  | 
transform one list into another list (e.g.~list of squares),
  | 
| 
 | 
   547  | 
or one set into another set (set of numbers into set of
  | 
| 
 | 
   548  | 
reminders modulo 3). What happens if we just want to print out
  | 
| 
 | 
   549  | 
a list of integers? Then actually the for-comprehension
  | 
| 
 | 
   550  | 
needs to be modified. The reason is that \code{print}, you
 | 
| 
 | 
   551  | 
guessed it, does not produce any result, but only produces
  | 
| 
 | 
   552  | 
what is in the functional-programming-lingo called a
  | 
| 
 | 
   553  | 
side-effect. Printing out the list of numbers from 1 to 5
  | 
| 
 | 
   554  | 
would look as follows
  | 
| 
 | 
   555  | 
  | 
| 
 | 
   556  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   557  | 
scala> for (n <- (1 to 5).toList) print(n)
  | 
| 
 | 
   558  | 
12345
  | 
| 
 | 
   559  | 
\end{lstlisting}
 | 
| 
 | 
   560  | 
  | 
| 
 | 
   561  | 
\noindent
  | 
| 
 | 
   562  | 
where you need to omit the keyword \code{yield}. You can
 | 
| 
 | 
   563  | 
also do more elaborate calculations such as
  | 
| 
 | 
   564  | 
  | 
| 
 | 
   565  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   566  | 
scala> for (n <- (1 to 5).toList) {
 | 
| 
 | 
   567  | 
  val square_n = n * n
  | 
| 
 | 
   568  | 
  println(s"$n * $n = $square_n") 
  | 
| 
 | 
   569  | 
}
  | 
| 
 | 
   570  | 
1 * 1 = 1
  | 
| 
 | 
   571  | 
2 * 2 = 4
  | 
| 
 | 
   572  | 
3 * 3 = 9
  | 
| 
 | 
   573  | 
4 * 4 = 16
  | 
| 
 | 
   574  | 
5 * 5 = 25
  | 
| 
 | 
   575  | 
\end{lstlisting}%$
 | 
| 
 | 
   576  | 
  | 
| 
 | 
   577  | 
\noindent In this code I use a variable assignment (\code{val
 | 
| 
 | 
   578  | 
square_n = ...} ) and also what is called in Scala a
  | 
| 
 | 
   579  | 
\emph{string interpolation}, written \code{s"..."}. The latter
 | 
| 
 | 
   580  | 
is for printing out an equation. It allows me to refer to the
  | 
| 
 | 
   581  | 
integer values \code{n} and \code{square\_n} inside a string.
 | 
| 
 | 
   582  | 
This is very convenient for printing out ``things''. 
  | 
| 
 | 
   583  | 
  | 
| 
 | 
   584  | 
The corresponding map construction for functions with 
  | 
| 
 | 
   585  | 
side-effects is in Scala called \code{foreach}. So you 
 | 
| 
 | 
   586  | 
could also write
  | 
| 
 | 
   587  | 
  | 
| 
 | 
   588  | 
  | 
| 
 | 
   589  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   590  | 
scala> (1 to 5).toList.foreach(n => print(n))
  | 
| 
 | 
   591  | 
12345
  | 
| 
 | 
   592  | 
\end{lstlisting}
 | 
| 
 | 
   593  | 
  | 
| 
 | 
   594  | 
  | 
| 
 | 
   595  | 
\noindent or even just
  | 
| 
 | 
   596  | 
  | 
| 
 | 
   597  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   598  | 
scala> (1 to 5).toList.foreach(print)
  | 
| 
 | 
   599  | 
12345
  | 
| 
 | 
   600  | 
\end{lstlisting}
 | 
| 
 | 
   601  | 
  | 
| 
 | 
   602  | 
\noindent Again I hope this reminds you a bit of your
  | 
| 
 | 
   603  | 
lambda-calculus lessons, where an explanation is given why
  | 
| 
 | 
   604  | 
both forms produce the same result.
  | 
| 
 | 
   605  | 
  | 
| 
 | 
   606  | 
  | 
| 
 | 
   607  | 
If you want to find out more about maps and functions with
  | 
| 
 | 
   608  | 
side-effects, you can ponder about the response Scala gives if
  | 
| 
 | 
   609  | 
you replace \code{foreach} by \code{map} in the expression
 | 
| 
 | 
   610  | 
above. Scala will still allow \code{map} with side-effect
 | 
| 
 | 
   611  | 
functions, but then reacts with a slightly interesting result.
  | 
| 
 | 
   612  | 
  | 
| 
 | 
   613  | 
\subsection*{Types}
 | 
| 
 | 
   614  | 
  | 
| 
 | 
   615  | 
In most functional programming languages, types play an
  | 
| 
 | 
   616  | 
important role. Scala is such a language. You have already
  | 
| 
 | 
   617  | 
seen built-in types, like \code{Int}, \code{Boolean},
 | 
| 
 | 
   618  | 
\code{String} and \code{BigInt}, but also user-defined ones,
 | 
| 
 | 
   619  | 
like \code{Rexp}. Unfortunately, types can be a thorny
 | 
| 
 | 
   620  | 
subject, especially in Scala. For example, why do we need to
  | 
| 
 | 
   621  | 
give the type to \code{toSet[Int]}, but not to \code{toList}?
 | 
| 
 | 
   622  | 
The reason is the power of Scala, which sometimes means it
  | 
| 
 | 
   623  | 
cannot infer all necessary typing information. At the
  | 
| 
 | 
   624  | 
beginning while getting familiar with Scala, I recommend a
  | 
| 
 | 
   625  | 
``play-it-by-ear-approach'' to types. Fully understanding
  | 
| 
 | 
   626  | 
type-systems, especially complicated ones like in Scala, can
  | 
| 
 | 
   627  | 
take a module on their own.\footnote{Still, such a study can
 | 
| 
 | 
   628  | 
be a rewarding training: If you are in the business of
  | 
| 
 | 
   629  | 
designing new programming languages, you will not be able to
  | 
| 
 | 
   630  | 
turn a blind eye to types. They essentially help programmers
  | 
| 
 | 
   631  | 
to avoid common programming errors and help with maintaining
  | 
| 
 | 
   632  | 
code.}
  | 
| 
 | 
   633  | 
  | 
| 
 | 
   634  | 
In Scala, types are needed whenever you define an inductive
  | 
| 
 | 
   635  | 
datatype and also whenever you define functions (their
  | 
| 
 | 
   636  | 
arguments and their results need a type). Base types are types
  | 
| 
 | 
   637  | 
that do not take any (type)arguments, for example \code{Int}
 | 
| 
 | 
   638  | 
and \code{String}. Compound types take one or more arguments,
 | 
| 
 | 
   639  | 
which as seen earlier need to be given in angle-brackets, for
  | 
| 
 | 
   640  | 
example \code{List[Int]} or \code{Set[List[String]]} or 
 | 
| 
 | 
   641  | 
\code{Map[Int, Int]}.
 | 
| 
 | 
   642  | 
  | 
| 
 | 
   643  | 
There are a few special type-constructors that fall outside
  | 
| 
 | 
   644  | 
this pattern. One is for tuples, where the type is written
  | 
| 
 | 
   645  | 
with parentheses. For example 
  | 
| 
 | 
   646  | 
  | 
| 
 | 
   647  | 
\begin{lstlisting}[ numbers=none]
 | 
| 
 | 
   648  | 
(Int, Int, String)
  | 
| 
 | 
   649  | 
\end{lstlisting}
 | 
| 
 | 
   650  | 
  | 
| 
 | 
   651  | 
\noindent is for a triple (a tuple with three components---two
  | 
| 
 | 
   652  | 
integers and a string). Tuples are helpful if you want to
  | 
| 
 | 
   653  | 
define functions with multiple results, say the function
  | 
| 
 | 
   654  | 
returning the quotient and reminder of two numbers. For this
  | 
| 
 | 
   655  | 
you might define:
  | 
| 
 | 
   656  | 
  | 
| 
 | 
   657  | 
  | 
| 
 | 
   658  | 
\begin{lstlisting}[ numbers=none]
 | 
| 
 | 
   659  | 
def quo_rem(m: Int, n: Int) : (Int, Int) = (m / n, m % n)
  | 
| 
 | 
   660  | 
\end{lstlisting}
 | 
| 
 | 
   661  | 
  | 
| 
 | 
   662  | 
  | 
| 
 | 
   663  | 
\noindent Since this function returns a pair of integers, its
  | 
| 
 | 
   664  | 
return type needs to be of type \code{(Int, Int)}.
 | 
| 
 | 
   665  | 
Incidentally, this is also the input type of this function.
  | 
| 
 | 
   666  | 
Notice this function takes \emph{two} arguments, namely
 | 
| 
 | 
   667  | 
\code{m} and \code{n}, both of which are integers. They are
 | 
| 
 | 
   668  | 
``packaged'' in a pair. Consequently the complete type of
  | 
| 
 | 
   669  | 
\code{quo_rem} is
 | 
| 
 | 
   670  | 
  | 
| 
 | 
   671  | 
\begin{lstlisting}[ numbers=none]
 | 
| 
 | 
   672  | 
(Int, Int) => (Int, Int)
  | 
| 
 | 
   673  | 
\end{lstlisting}
 | 
| 
 | 
   674  | 
  | 
| 
 | 
   675  | 
Another special type-constructor is for functions, written as
  | 
| 
 | 
   676  | 
the arrow \code{=>}. For example, the type \code{Int =>
 | 
| 
 | 
   677  | 
String} is for a function that takes an integer as input
  | 
| 
 | 
   678  | 
argument and produces a string as result. A function of this
  | 
| 
 | 
   679  | 
type is for instance
  | 
| 
 | 
   680  | 
  | 
| 
 | 
   681  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   682  | 
def mk_string(n: Int) : String = n match {
 | 
| 
 | 
   683  | 
  case 0 => "zero"
  | 
| 
 | 
   684  | 
  case 1 => "one"
  | 
| 
 | 
   685  | 
  case 2 => "two"
  | 
| 
 | 
   686  | 
  case _ => "many" 
  | 
| 
 | 
   687  | 
} 
  | 
| 
 | 
   688  | 
\end{lstlisting}
 | 
| 
 | 
   689  | 
  | 
| 
 | 
   690  | 
\noindent It takes an integer as input argument and returns a
  | 
| 
 | 
   691  | 
string. Unlike other functional programming languages, there
  | 
| 
 | 
   692  | 
is in Scala no easy way to find out the types of existing
  | 
| 
 | 
   693  | 
functions, except by looking into the documentation
  | 
| 
 | 
   694  | 
  | 
| 
 | 
   695  | 
\begin{quote}
 | 
| 
 | 
   696  | 
\url{http://www.scala-lang.org/api/current/}
 | 
| 
 | 
   697  | 
\end{quote}
 | 
| 
 | 
   698  | 
  | 
| 
 | 
   699  | 
The function arrow can also be iterated, as in 
  | 
| 
 | 
   700  | 
\code{Int => String => Boolean}. This is the type for a function
 | 
| 
 | 
   701  | 
taking an integer as first argument and a string as second,
  | 
| 
 | 
   702  | 
and the result of the function is a boolean. Though silly, a
  | 
| 
 | 
   703  | 
function of this type would be
  | 
| 
 | 
   704  | 
  | 
| 
 | 
   705  | 
  | 
| 
 | 
   706  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   707  | 
def chk_string(n: Int)(s: String) : Boolean = 
  | 
| 
 | 
   708  | 
  mk_string(n) == s
  | 
| 
 | 
   709  | 
\end{lstlisting}
 | 
| 
 | 
   710  | 
  | 
| 
 | 
   711  | 
  | 
| 
 | 
   712  | 
\noindent which checks whether the integer \code{n}
 | 
| 
 | 
   713  | 
corresponds to the name \code{s} given by the function
 | 
| 
 | 
   714  | 
\code{mk\_string}. Notice the unusual way of specifying the
 | 
| 
 | 
   715  | 
arguments of this function: the arguments are given one after
  | 
| 
 | 
   716  | 
the other, instead of being in a pair (what would be the type
  | 
| 
 | 
   717  | 
of this function then?). This way of specifying the arguments
  | 
| 
 | 
   718  | 
can be useful, for example in situations like this
  | 
| 
 | 
   719  | 
  | 
| 
 | 
   720  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   721  | 
scala> List("one", "two", "three", "many").map(chk_string(2))
 | 
| 
 | 
   722  | 
res4 = List(false, true, false, false)
  | 
| 
 | 
   723  | 
  | 
| 
 | 
   724  | 
scala> List("one", "two", "three", "many").map(chk_string(3))
 | 
| 
 | 
   725  | 
res5 = List(false, false, false, true)
  | 
| 
 | 
   726  | 
\end{lstlisting}
 | 
| 
 | 
   727  | 
  | 
| 
 | 
   728  | 
\noindent In each case we can give to \code{map} a specialised
 | 
| 
 | 
   729  | 
version of \code{chk_string}---once specialised to 2 and once
 | 
| 
 | 
   730  | 
to 3. This kind of ``specialising'' a function is called
  | 
| 
 | 
   731  | 
\emph{partial application}---we have not yet given to this
 | 
| 
 | 
   732  | 
function all arguments it needs, but only some of them.
  | 
| 
 | 
   733  | 
  | 
| 
 | 
   734  | 
Coming back to the type \code{Int => String => Boolean}. The
 | 
| 
 | 
   735  | 
rule about such function types is that the right-most type
  | 
| 
 | 
   736  | 
specifies what the function returns (a boolean in this case).
  | 
| 
 | 
   737  | 
The types before that specify how many arguments the function
  | 
| 
 | 
   738  | 
expects and what their type is (in this case two arguments,
  | 
| 
 | 
   739  | 
one of type \code{Int} and another of type \code{String}).
 | 
| 
 | 
   740  | 
Given this rule, what kind of function has type
  | 
| 
 | 
   741  | 
\mbox{\code{(Int => String) => Boolean}}? Well, it returns a
 | 
| 
 | 
   742  | 
boolean. More interestingly, though, it only takes a single
  | 
| 
 | 
   743  | 
argument (because of the parentheses). The single argument
  | 
| 
 | 
   744  | 
happens to be another function (taking an integer as input and
  | 
| 
 | 
   745  | 
returning a string). Remember that \code{mk_string} is just 
 | 
| 
 | 
   746  | 
such a function. So how can we use it? For this define
  | 
| 
 | 
   747  | 
the somewhat silly function \code{apply_3}:
 | 
| 
 | 
   748  | 
  | 
| 
 | 
   749  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   750  | 
def apply_3(f: Int => String): Bool = f(3) == "many"
  | 
| 
 | 
   751  | 
  | 
| 
 | 
   752  | 
scala> apply_3(mk_string)
  | 
| 
 | 
   753  | 
res6 = true
  | 
| 
 | 
   754  | 
\end{lstlisting}
 | 
| 
 | 
   755  | 
  | 
| 
 | 
   756  | 
You might ask: Apart from silly functions like above, what is
  | 
| 
 | 
   757  | 
the point of having functions as input arguments to other
  | 
| 
 | 
   758  | 
functions? In Java there is indeed no need of this kind of
  | 
| 
 | 
   759  | 
feature: at least in the past it did not allow such
  | 
| 
 | 
   760  | 
constructions. I think, the point of Java 8 is to lift this
  | 
| 
 | 
   761  | 
restriction. But in all functional programming languages,
  | 
| 
 | 
   762  | 
including Scala, it is really essential to allow functions as
  | 
| 
 | 
   763  | 
input argument. Above you already seen \code{map} and
 | 
| 
 | 
   764  | 
\code{foreach} which need this. Consider the functions
 | 
| 
 | 
   765  | 
\code{print} and \code{println}, which both print out strings,
 | 
| 
 | 
   766  | 
but the latter adds a line break. You can call \code{foreach}
 | 
| 
 | 
   767  | 
with either of them and thus changing how, for example, five
  | 
| 
 | 
   768  | 
numbers are printed.
  | 
| 
 | 
   769  | 
  | 
| 
 | 
   770  | 
  | 
| 
 | 
   771  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
   772  | 
scala> (1 to 5).toList.foreach(print)
  | 
| 
 | 
   773  | 
12345
  | 
| 
 | 
   774  | 
scala> (1 to 5).toList.foreach(println)
  | 
| 
 | 
   775  | 
1
  | 
| 
 | 
   776  | 
2
  | 
| 
 | 
   777  | 
3
  | 
| 
 | 
   778  | 
4
  | 
| 
 | 
   779  | 
5
  | 
| 
 | 
   780  | 
\end{lstlisting}
 | 
| 
 | 
   781  | 
  | 
| 
 | 
   782  | 
  | 
| 
 | 
   783  | 
\noindent This is actually one of the main design principles
  | 
| 
 | 
   784  | 
in functional programming. You have generic functions like
  | 
| 
 | 
   785  | 
\code{map} and \code{foreach} that can traverse data containers,
 | 
| 
 | 
   786  | 
like lists or sets. They then take a function to specify what
  | 
| 
 | 
   787  | 
should be done with each element during the traversal. This
  | 
| 
 | 
   788  | 
requires that the generic traversal functions can cope with
  | 
| 
 | 
   789  | 
any kind of function (not just functions that, for example,
  | 
| 
 | 
   790  | 
take as input an integer and produce a string like above).
  | 
| 
 | 
   791  | 
This means we cannot fix the type of the generic traversal
  | 
| 
 | 
   792  | 
functions, but have to keep them
  | 
| 
181
 | 
   793  | 
\emph{polymorphic}.\footnote{Another interesting topic about
 | 
| 
123
 | 
   794  | 
types, but we omit it here for the sake of brevity.} 
  | 
| 
 | 
   795  | 
  | 
| 
 | 
   796  | 
There is one more type constructor that is rather special. It
  | 
| 
 | 
   797  | 
is called \code{Unit}. Recall that \code{Boolean} has two
 | 
| 
 | 
   798  | 
values, namely \code{true} and \code{false}. This can be used,
 | 
| 
 | 
   799  | 
for example, to test something and decide whether the test
  | 
| 
 | 
   800  | 
succeeds or not. In contrast the type \code{Unit} has only a
 | 
| 
 | 
   801  | 
single value, written \code{()}. This seems like a completely
 | 
| 
 | 
   802  | 
useless type and return value for a function, but is actually
  | 
| 
 | 
   803  | 
quite useful. It indicates when the function does not return
  | 
| 
 | 
   804  | 
any result. The purpose of these functions is to cause
  | 
| 
 | 
   805  | 
something being written on the screen or written into a file,
  | 
| 
 | 
   806  | 
for example. This is what is called they cause some effect on 
  | 
| 
 | 
   807  | 
the side, namely a new content displayed on the screen or some
  | 
| 
 | 
   808  | 
new data in a file. Scala uses the \code{Unit} type to indicate
 | 
| 
 | 
   809  | 
that a function does not have a result, but potentially causes
  | 
| 
 | 
   810  | 
some side-effect. Typical examples are the printing functions, 
  | 
| 
 | 
   811  | 
like \code{print}.
 | 
| 
 | 
   812  | 
  | 
| 
 | 
   813  | 
  | 
| 
143
 | 
   814  | 
% \subsection*{Cool Stuff}
 | 
| 
123
 | 
   815  | 
  | 
| 
143
 | 
   816  | 
% The first wow-moment I had with Scala was when I came across
  | 
| 
 | 
   817  | 
% the following code-snippet for reading a web-page. 
  | 
| 
123
 | 
   818  | 
  | 
| 
 | 
   819  | 
  | 
| 
143
 | 
   820  | 
% \begin{lstlisting}[ numbers=none]
 | 
| 
 | 
   821  | 
% import io.Source
  | 
| 
 | 
   822  | 
% val url = """http://www.inf.kcl.ac.uk/staff/urbanc/"""
  | 
| 
 | 
   823  | 
% Source.fromURL(url)("ISO-8859-1").take(10000).mkString
 | 
| 
 | 
   824  | 
% \end{lstlisting}
 | 
| 
123
 | 
   825  | 
  | 
| 
 | 
   826  | 
  | 
| 
143
 | 
   827  | 
% \noindent These three lines return a string containing the
  | 
| 
 | 
   828  | 
% HTML-code of my webpage. It actually already does something
  | 
| 
 | 
   829  | 
% more sophisticated, namely only returns the first 10000
  | 
| 
 | 
   830  | 
% characters of a webpage in case it is too large. Why is that
  | 
| 
 | 
   831  | 
% code-snippet of any interest? Well, try implementing
  | 
| 
 | 
   832  | 
% reading-from-a-webpage in Java. I also like the possibility of
  | 
| 
 | 
   833  | 
% triple-quoting strings, which I have only seen in Scala so
  | 
| 
 | 
   834  | 
% far. The idea behind this is that in such a string all
  | 
| 
 | 
   835  | 
% characters are interpreted literally---there are no escaped
  | 
| 
 | 
   836  | 
% characters, like \verb|\n| for newlines.
  | 
| 
123
 | 
   837  | 
  | 
| 
143
 | 
   838  | 
% My second wow-moment I had with a feature of Scala that other
  | 
| 
 | 
   839  | 
% functional programming languages do not have. This feature is
  | 
| 
 | 
   840  | 
% about implicit type conversions. If you have regular
  | 
| 
 | 
   841  | 
% expressions and want to use them for language processing you
  | 
| 
 | 
   842  | 
% often want to recognise keywords in a language, for example
  | 
| 
 | 
   843  | 
% \code{for},{} \code{if},{} \code{yield} and so on. But the
 | 
| 
 | 
   844  | 
% basic regular expression \code{CHAR} can only recognise a
 | 
| 
 | 
   845  | 
% single character. In order to recognise a whole string, like
  | 
| 
 | 
   846  | 
% \code{for}, you have to put many of those together using
 | 
| 
 | 
   847  | 
% \code{SEQ}:
 | 
| 
123
 | 
   848  | 
  | 
| 
 | 
   849  | 
  | 
| 
143
 | 
   850  | 
% \begin{lstlisting}[numbers=none]
 | 
| 
 | 
   851  | 
% SEQ(CHAR('f'), SEQ(CHAR('o'), CHAR('r')))
 | 
| 
 | 
   852  | 
% \end{lstlisting}
 | 
| 
123
 | 
   853  | 
  | 
| 
143
 | 
   854  | 
% \noindent This gets quickly unreadable when the strings and
  | 
| 
 | 
   855  | 
% regular expressions get more complicated. In other functional
  | 
| 
 | 
   856  | 
% programming languages, you can explicitly write a conversion
  | 
| 
 | 
   857  | 
% function that takes a string, say \dq{\pcode{for}}, and
 | 
| 
 | 
   858  | 
% generates the regular expression above. But then your code is
  | 
| 
 | 
   859  | 
% littered with such conversion functions.
  | 
| 
123
 | 
   860  | 
  | 
| 
143
 | 
   861  | 
% In Scala you can do better by ``hiding'' the conversion
  | 
| 
 | 
   862  | 
% functions. The keyword for doing this is \code{implicit} and
 | 
| 
 | 
   863  | 
% it needs a built-in library called 
  | 
| 
123
 | 
   864  | 
  | 
| 
143
 | 
   865  | 
% \begin{lstlisting}[numbers=none]
 | 
| 
 | 
   866  | 
% scala.language.implicitConversions
  | 
| 
 | 
   867  | 
% \end{lstlisting}
 | 
| 
123
 | 
   868  | 
  | 
| 
143
 | 
   869  | 
% \noindent
  | 
| 
 | 
   870  | 
% Consider the code
  | 
| 
123
 | 
   871  | 
  | 
| 
 | 
   872  | 
  | 
| 
143
 | 
   873  | 
% \begin{lstlisting}[language=Scala]
 | 
| 
 | 
   874  | 
% import scala.language.implicitConversions
  | 
| 
123
 | 
   875  | 
  | 
| 
143
 | 
   876  | 
% def charlist2rexp(s: List[Char]) : Rexp = s match {
 | 
| 
 | 
   877  | 
%   case Nil => EMPTY
  | 
| 
 | 
   878  | 
%   case c::Nil => CHAR(c)
  | 
| 
 | 
   879  | 
%   case c::s => SEQ(CHAR(c), charlist2rexp(s))
  | 
| 
 | 
   880  | 
% }
  | 
| 
123
 | 
   881  | 
  | 
| 
143
 | 
   882  | 
% implicit def string2rexp(s: String) : Rexp = 
  | 
| 
 | 
   883  | 
%   charlist2rexp(s.toList)
  | 
| 
 | 
   884  | 
% \end{lstlisting}
 | 
| 
123
 | 
   885  | 
  | 
| 
 | 
   886  | 
  | 
| 
143
 | 
   887  | 
% \noindent where the first seven lines implement a function
  | 
| 
 | 
   888  | 
% that given a list of characters generates the corresponding
  | 
| 
 | 
   889  | 
% regular expression. In Lines 9 and 10, this function is used
  | 
| 
 | 
   890  | 
% for transforming a string into a regular expression. Since the
  | 
| 
 | 
   891  | 
% \code{string2rexp}-function is declared as \code{implicit},
 | 
| 
 | 
   892  | 
% the effect will be that whenever Scala expects a regular
  | 
| 
 | 
   893  | 
% expression, but I only give it a string, it will automatically
  | 
| 
 | 
   894  | 
% insert a call to the \code{string2rexp}-function. I can now
 | 
| 
 | 
   895  | 
% write for example
  | 
| 
123
 | 
   896  | 
  | 
| 
143
 | 
   897  | 
% \begin{lstlisting}[numbers=none]
 | 
| 
 | 
   898  | 
% scala> ALT("ab", "ac")
 | 
| 
 | 
   899  | 
% res9 = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c)))
  | 
| 
 | 
   900  | 
% \end{lstlisting}
 | 
| 
123
 | 
   901  | 
  | 
| 
143
 | 
   902  | 
% \noindent Recall that \code{ALT} expects two regular
 | 
| 
 | 
   903  | 
% expressions as arguments, but I only supply two strings. The
  | 
| 
 | 
   904  | 
% implicit conversion function will transform the string into a
  | 
| 
 | 
   905  | 
% regular expression.
  | 
| 
123
 | 
   906  | 
  | 
| 
143
 | 
   907  | 
% Using implicit definitions, Scala allows me to introduce
  | 
| 
 | 
   908  | 
% some further syntactic sugar for regular expressions:
  | 
| 
123
 | 
   909  | 
  | 
| 
 | 
   910  | 
  | 
| 
143
 | 
   911  | 
% \begin{lstlisting}[ numbers=none]
 | 
| 
 | 
   912  | 
% implicit def RexpOps(r: Rexp) = new {
 | 
| 
 | 
   913  | 
%   def | (s: Rexp) = ALT(r, s)
  | 
| 
 | 
   914  | 
%   def ~ (s: Rexp) = SEQ(r, s)
  | 
| 
 | 
   915  | 
%   def % = STAR(r)
  | 
| 
 | 
   916  | 
% }
  | 
| 
123
 | 
   917  | 
  | 
| 
143
 | 
   918  | 
% implicit def stringOps(s: String) = new {
 | 
| 
 | 
   919  | 
%   def | (r: Rexp) = ALT(s, r)
  | 
| 
 | 
   920  | 
%   def | (r: String) = ALT(s, r)
  | 
| 
 | 
   921  | 
%   def ~ (r: Rexp) = SEQ(s, r)
  | 
| 
 | 
   922  | 
%   def ~ (r: String) = SEQ(s, r)
  | 
| 
 | 
   923  | 
%   def % = STAR(s)
  | 
| 
 | 
   924  | 
% }
  | 
| 
 | 
   925  | 
% \end{lstlisting}
 | 
| 
123
 | 
   926  | 
  | 
| 
 | 
   927  | 
 
  | 
| 
143
 | 
   928  | 
% \noindent This might seem a bit overly complicated, but its effect is
  | 
| 
 | 
   929  | 
% that I can now write regular expressions such as $ab + ac$ 
  | 
| 
 | 
   930  | 
% simply as
  | 
| 
123
 | 
   931  | 
  | 
| 
 | 
   932  | 
  | 
| 
143
 | 
   933  | 
% \begin{lstlisting}[numbers=none]
 | 
| 
 | 
   934  | 
% scala> "ab" | "ac"
  | 
| 
 | 
   935  | 
% res10 = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c)))
  | 
| 
 | 
   936  | 
% \end{lstlisting}
 | 
| 
123
 | 
   937  | 
  | 
| 
 | 
   938  | 
 
  | 
| 
143
 | 
   939  | 
% \noindent I leave you to figure out what the other
  | 
| 
 | 
   940  | 
% syntactic sugar in the code above stands for.
  | 
| 
123
 | 
   941  | 
 
  | 
| 
143
 | 
   942  | 
% One more useful feature of Scala is the ability to define
  | 
| 
 | 
   943  | 
% functions with varying argument lists. This is a feature that
  | 
| 
 | 
   944  | 
% is already present in old languages, like C, but seems to have
  | 
| 
 | 
   945  | 
% been forgotten in the meantime---Java does not have it. In the
  | 
| 
 | 
   946  | 
% context of regular expressions this feature comes in handy:
  | 
| 
 | 
   947  | 
% Say you are fed up with writing many alternatives as
  | 
| 
123
 | 
   948  | 
  | 
| 
 | 
   949  | 
  | 
| 
143
 | 
   950  | 
% \begin{lstlisting}[numbers=none]
 | 
| 
 | 
   951  | 
% ALT(..., ALT(..., ALT(..., ...)))
  | 
| 
 | 
   952  | 
% \end{lstlisting}
 | 
| 
123
 | 
   953  | 
  | 
| 
 | 
   954  | 
  | 
| 
143
 | 
   955  | 
% \noindent To make it difficult, you do not know how deep such
  | 
| 
 | 
   956  | 
% alternatives are nested. So you need something flexible that
  | 
| 
 | 
   957  | 
% can take as many alternatives as needed. In Scala one can
  | 
| 
 | 
   958  | 
% achieve this by adding a \code{*} to the type of an argument.
 | 
| 
 | 
   959  | 
% Consider the code
  | 
| 
123
 | 
   960  | 
  | 
| 
 | 
   961  | 
  | 
| 
143
 | 
   962  | 
% \begin{lstlisting}[language=Scala]
 | 
| 
 | 
   963  | 
% def Alts(rs: List[Rexp]) : Rexp = rs match {
 | 
| 
 | 
   964  | 
%   case Nil => NULL
  | 
| 
 | 
   965  | 
%   case r::Nil => r
  | 
| 
 | 
   966  | 
%   case r::rs => ALT(r, Alts(rs))
  | 
| 
 | 
   967  | 
% }
  | 
| 
123
 | 
   968  | 
  | 
| 
143
 | 
   969  | 
% def ALTS(rs: Rexp*) = Alts(rs.toList)
  | 
| 
 | 
   970  | 
% \end{lstlisting}
 | 
| 
123
 | 
   971  | 
  | 
| 
 | 
   972  | 
  | 
| 
143
 | 
   973  | 
% \noindent The function in Lines 1 to 5 takes a list of regular
  | 
| 
 | 
   974  | 
% expressions and converts it into an appropriate alternative
  | 
| 
 | 
   975  | 
% regular expression. In Line 7 there is a wrapper for this
  | 
| 
 | 
   976  | 
% function which uses the feature of varying argument lists. The
  | 
| 
 | 
   977  | 
% effect of this code  is that I can write the regular
  | 
| 
 | 
   978  | 
% expression for keywords as
  | 
| 
123
 | 
   979  | 
  | 
| 
 | 
   980  | 
  | 
| 
143
 | 
   981  | 
% \begin{lstlisting}[numbers=none]
 | 
| 
 | 
   982  | 
% ALTS("for", "def", "yield", "implicit", "if", "match", "case")
 | 
| 
 | 
   983  | 
% \end{lstlisting}
 | 
| 
123
 | 
   984  | 
  | 
| 
 | 
   985  | 
  | 
| 
143
 | 
   986  | 
% \noindent Again I leave it to you to find out how much this
  | 
| 
 | 
   987  | 
% simplifies the regular expression in comparison with if I had
  | 
| 
 | 
   988  | 
% to write this by hand using only the ``plain'' regular
  | 
| 
 | 
   989  | 
% expressions from the inductive datatype.
  | 
| 
 | 
   990  | 
  | 
| 
 | 
   991  | 
\bigskip\noindent
  | 
| 
 | 
   992  | 
\textit{More TBD.}
 | 
| 
123
 | 
   993  | 
  | 
| 
181
 | 
   994  | 
\subsection*{Coursework}
 | 
| 
 | 
   995  | 
  | 
| 
123
 | 
   996  | 
\subsection*{More Info}
 | 
| 
 | 
   997  | 
  | 
| 
 | 
   998  | 
There is much more to Scala than I can possibly describe in
  | 
| 
 | 
   999  | 
this document. Fortunately there are a number of free books
  | 
| 
 | 
  1000  | 
about Scala and of course lots of help online. For example
  | 
| 
 | 
  1001  | 
  | 
| 
 | 
  1002  | 
\begin{itemize}
 | 
| 
 | 
  1003  | 
\item \url{http://www.scala-lang.org/docu/files/ScalaByExample.pdf}
 | 
| 
 | 
  1004  | 
\item \url{http://www.scala-lang.org/docu/files/ScalaTutorial.pdf}
 | 
| 
 | 
  1005  | 
\item \url{https://www.youtube.com/user/ShadowofCatron}
 | 
| 
 | 
  1006  | 
\item \url{http://docs.scala-lang.org/tutorials}
 | 
| 
 | 
  1007  | 
\item \url{https://www.scala-exercises.org}
 | 
| 
 | 
  1008  | 
\end{itemize}
 | 
| 
 | 
  1009  | 
  | 
| 
 | 
  1010  | 
\noindent There is also a course at Coursera on Functional
  | 
| 
 | 
  1011  | 
Programming Principles in Scala by Martin Odersky, the main
  | 
| 
 | 
  1012  | 
developer of the Scala language. And a document that explains
  | 
| 
 | 
  1013  | 
Scala for Java programmers
  | 
| 
 | 
  1014  | 
  | 
| 
 | 
  1015  | 
\begin{itemize}
 | 
| 
 | 
  1016  | 
\item \small\url{http://docs.scala-lang.org/tutorials/scala-for-java-programmers.html}
 | 
| 
 | 
  1017  | 
\end{itemize}
 | 
| 
 | 
  1018  | 
  | 
| 
 | 
  1019  | 
While I am quite enthusiastic about Scala, I am also happy to
  | 
| 
 | 
  1020  | 
admit that it has more than its fair share of faults. The
  | 
| 
 | 
  1021  | 
problem seen earlier of having to give an explicit type to
  | 
| 
 | 
  1022  | 
\code{toSet}, but not \code{toList} is one of them. There are
 | 
| 
 | 
  1023  | 
also many ``deep'' ideas about types in Scala, which even to
  | 
| 
 | 
  1024  | 
me as seasoned functional programmer are puzzling. Whilst
  | 
| 
 | 
  1025  | 
implicits are great, they can also be a source of great
  | 
| 
 | 
  1026  | 
headaches, for example consider the code:
  | 
| 
 | 
  1027  | 
  | 
| 
 | 
  1028  | 
\begin{lstlisting}[numbers=none]
 | 
| 
 | 
  1029  | 
scala>  List (1, 2, 3) contains "your mom"
  | 
| 
 | 
  1030  | 
res1: Boolean = false
  | 
| 
 | 
  1031  | 
\end{lstlisting}
 | 
| 
 | 
  1032  | 
  | 
| 
 | 
  1033  | 
\noindent Rather than returning \code{false}, this code should
 | 
| 
 | 
  1034  | 
throw a typing-error. There are also many limitations Scala
  | 
| 
 | 
  1035  | 
inherited from the JVM that can be really annoying. For
  | 
| 
 | 
  1036  | 
example a fixed stack size. One can work around this
  | 
| 
 | 
  1037  | 
particular limitation, but why does one have to?
  | 
| 
 | 
  1038  | 
More such `puzzles' can be found at
  | 
| 
 | 
  1039  | 
  | 
| 
 | 
  1040  | 
\begin{center}
 | 
| 
 | 
  1041  | 
  \url{http://scalapuzzlers.com} and
 | 
| 
 | 
  1042  | 
  \url{http://latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/}
 | 
| 
 | 
  1043  | 
\end{center}
 | 
| 
 | 
  1044  | 
  | 
| 
 | 
  1045  | 
Even if Scala has been a success in several high-profile
  | 
| 
 | 
  1046  | 
companies, there is also a company (Yammer) that first used
  | 
| 
 | 
  1047  | 
Scala in their production code, but then moved away from it.
  | 
| 
 | 
  1048  | 
Allegedly they did not like the steep learning curve of Scala
  | 
| 
 | 
  1049  | 
and also that new versions of Scala often introduced
  | 
| 
 | 
  1050  | 
incompatibilities in old code. In the past two months
  | 
| 
 | 
  1051  | 
there have also been two forks of the Scala compiler.
  | 
| 
 | 
  1052  | 
It needs to be seen what the future brings for Scala.
  | 
| 
 | 
  1053  | 
  | 
| 
152
 | 
  1054  | 
%So all in all, Scala might not be a great teaching language,
  | 
| 
 | 
  1055  | 
%but I hope this is mitigated by the fact that I never require
  | 
| 
 | 
  1056  | 
%you to write any Scala code. You only need to be able to read
  | 
| 
 | 
  1057  | 
%it. In the coursework you can use any programming language you
  | 
| 
 | 
  1058  | 
%like. If you want to use Scala for this, then be my guest; if
  | 
| 
 | 
  1059  | 
%you do not want, stick with the language you are most familiar
  | 
| 
 | 
  1060  | 
%with.
  | 
| 
123
 | 
  1061  | 
  | 
| 
 | 
  1062  | 
  | 
| 
 | 
  1063  | 
  | 
| 
182
 | 
  1064  | 
\begin{flushright}\it
 | 
| 
 | 
  1065  | 
There are only two kinds of languages: the ones people complain 
  | 
| 
 | 
  1066  | 
about\\ and the ones nobody uses.\smallskip\\
  | 
| 
 | 
  1067  | 
\mbox{}\hfill\small{}---Bjarne Stroustrup (the inventor of C++)
 | 
| 
 | 
  1068  | 
\end{flushright}
 | 
| 
 | 
  1069  | 
  | 
| 
123
 | 
  1070  | 
\end{document}
 | 
| 
 | 
  1071  | 
  | 
| 
 | 
  1072  | 
%%% Local Variables: 
  | 
| 
 | 
  1073  | 
%%% mode: latex
  | 
| 
 | 
  1074  | 
%%% TeX-master: t
  | 
| 
 | 
  1075  | 
%%% End: 
  |