handouts/pep-ho.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 14 Jul 2024 14:31:39 +0100
changeset 492 4ffba2f72692
parent 485 19b75e899d37
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
     1
% !TEX program = xelatex
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
\documentclass{article}
426
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
     3
\usepackage{../styles/style}
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
     4
\usepackage{../styles/langs}
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
     5
\usepackage{tikz}
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
     6
\usepackage{pgf}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
\usepackage{marvosym}
184
84dc794928de updated
pdated
parents: 183
diff changeset
     8
\usepackage{boxedminipage}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
    10
\lstset{escapeinside={/*!}{!*/}}
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
    11
\newcommand{\annotation}[1]{\hfill\footnotesize{}#1}
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
    12
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
    13
\usepackage{menukeys}
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
    14
\usepackage{emoji}
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
    15
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
%cheat sheet
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
%http://worldline.github.io/scala-cheatsheet/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
    19
% case class, apply, unapply
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    20
% see https://medium.com/@thejasbabu/scala-pattern-matching-9c9e73ba9a8a
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
    21
191
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    22
% the art of programming
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    23
% https://www.youtube.com/watch?v=QdVFvsCWXrA
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    24
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    25
% functional programming in Scala
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    26
%https://www.amazon.com/gp/product/1449311032/ref=as_li_ss_tl?ie=UTF8&tag=aleottshompag-20&linkCode=as2&camp=1789&creative=390957&creativeASIN=1449311032
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    27
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    28
% functional programming in C
191
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    29
%https://www.amazon.com/gp/product/0201419505/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=0201419505&linkCode=as2&tag=aleottshompag-20
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    30
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    31
%speeding through haskell
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    32
%https://openlibra.com/en/book/download/speeding-through-haskell
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    33
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    34
% fp books --- ocaml
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    35
% http://courses.cms.caltech.edu/cs134/cs134b/book.pdf
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    36
% http://alexott.net/en/fp/books/
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
    37
257
ba4d976ca88d updated
Christian Urban <urbanc@in.tum.de>
parents: 198
diff changeset
    38
%John Hughes’ simple words:
ba4d976ca88d updated
Christian Urban <urbanc@in.tum.de>
parents: 198
diff changeset
    39
%A combinator is a function which builds program fragments
ba4d976ca88d updated
Christian Urban <urbanc@in.tum.de>
parents: 198
diff changeset
    40
%from program fragments.
ba4d976ca88d updated
Christian Urban <urbanc@in.tum.de>
parents: 198
diff changeset
    41
ba4d976ca88d updated
Christian Urban <urbanc@in.tum.de>
parents: 198
diff changeset
    42
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
    43
%explain graph colouring program (examples from)
264
ecd989eee8bd updated
Christian Urban <urbanc@in.tum.de>
parents: 257
diff changeset
    44
%https://www.metalevel.at/prolog/optimization
ecd989eee8bd updated
Christian Urban <urbanc@in.tum.de>
parents: 257
diff changeset
    45
ecd989eee8bd updated
Christian Urban <urbanc@in.tum.de>
parents: 257
diff changeset
    46
% nice example for map and reduce using Harry potter characters
ecd989eee8bd updated
Christian Urban <urbanc@in.tum.de>
parents: 257
diff changeset
    47
% https://www.matthewgerstman.com/map-filter-reduce/
ecd989eee8bd updated
Christian Urban <urbanc@in.tum.de>
parents: 257
diff changeset
    48
333
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
    49
% interesting talk about differences in Java and Scala
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
    50
% Goto'19 conference ; about differences in type-system
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
    51
% https://www.youtube.com/watch?v=e6n-Ci8V2CM
264
ecd989eee8bd updated
Christian Urban <urbanc@in.tum.de>
parents: 257
diff changeset
    52
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    53
% Timing
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    54
%
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    55
% xs.map(x => (x, xs.count(_==x)))
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    56
%
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    57
% vs  xs.groupBy(identity)
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    58
%
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    59
% first is quadratic, while second is linear.
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    60
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    61
% contrast map with a for loop in imperative languages
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    62
%
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    63
% Let’s use a simple example of calculating sales tax on an array of
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    64
% prices.
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    65
%
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    66
%       const prices = [19.99, 4.95, 25, 3.50];
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    67
%       let new_prices = [];
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    68
%
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    69
%       for(let i=0; i < prices.length; i++) {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    70
%          new_prices.push(prices[i] * 1.06);
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    71
%       }
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    72
%
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    73
% We can achieve the same results using .map():
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    74
%
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
    75
% const prices = [19.99, 4.95, 25, 3.50];
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    76
% let new_prices = prices.map(price => price * 1.06);
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    77
%
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    78
% The syntax above is condensed so let’s walk through it a bit. The
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    79
% .map() method takes a callback, which can be thought of as a function.
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    80
% That’s what is between the parentheses. The variable price is the name
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    81
% that will be used to identify each value. Since there’s only one
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    82
% input, we can omit the usual parentheses around the parameters.
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    83
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    84
% potentially a worked example? Tetris in scala.js
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
    85
%
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    86
% https://medium.com/@michael.karen/learning-modern-javascript-with-tetris-92d532bcd057
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    87
%
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    88
% Scala videos
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    89
%    https://www.youtube.com/user/DrMarkCLewis
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
    90
334
841727e27252 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    91
841727e27252 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    92
%% https://alvinalexander.com/downloads/HelloScala-FreePreview.pdf
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
    93
%%
334
841727e27252 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    94
%% Section 10 about strings; interpolations and multiline strings
841727e27252 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
    95
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    96
% Easy installation
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    97
%https://alexarchambault.github.io/posts/2020-09-21-cs-setup.html
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    98
426
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
    99
% scala libraries
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   100
%https://index.scala-lang.org
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   101
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   102
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   103
% Learning functional programming is an opportunity to discover a new
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   104
% way to represent programs, to approach problems, and to think about
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   105
% languages. While programming with a functional language is still
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   106
% fundamentally similar to programming with any other type of language
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   107
% (examples of others being imperative or logic), it represents
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   108
% programs and algorithms through distinct forms of abstraction and
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   109
% gives you a new toolset with which to solve programming
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   110
% problems. Additionally, many of the techniques of functional
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   111
% programming are beginning to permeate new mainstream languages, so
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   112
% taking the time now to develop a thorough understanding of them is
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   113
% an investment which will pay great dividends.
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   114
b51467741af2 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 400
diff changeset
   115
334
841727e27252 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
   116
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   117
% Exact colors from NB
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   118
\usepackage[breakable]{tcolorbox}
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   119
\definecolor{incolor}{HTML}{303F9F}
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   120
\definecolor{outcolor}{HTML}{D84315}
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   121
\definecolor{cellborder}{HTML}{CFCFCF}
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   122
\definecolor{cellbackground}{HTML}{F7F7F7}
334
841727e27252 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
   123
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   124
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   125
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
\begin{document}
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   127
\fnote{\copyright{} Christian Urban, King's College London, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   129
%\begin{tcolorbox}[breakable,size=fbox,boxrule=1pt,pad at break*=1mm,colback=cellbackground,colframe=cellborder]
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   130
%  abd
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   131
%\end{tcolorbox}
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   132
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   133
\section*{A Crash-Course in Scala}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   135
\mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{c}ompiled
182
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   136
\underline{a}cademic \underline{la}nguage''}\smallskip\\
192
a112e0e2325c updated
Christian Urban <urbanc@in.tum.de>
parents: 188
diff changeset
   137
\mbox{}\hfill\textit{ --- a joke(?) found on Twitter}\bigskip
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   138
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   139
\mbox{}\hfill\textit{``Life is too short for \texttt{malloc}.''}\smallskip\\
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   140
\mbox{}\hfill\textit{ --- said Neal Ford at Oscon'13}\;\hr{https://www.youtube.com/watch?v=7aYS9PcAITQ}\bigskip\\
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   141
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   142
% In 1982, James or Jim Morris wrote:
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   143
%
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   144
% Functional languages are unnatural to use; but so are knives and
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   145
% forks, diplomatic protocols, double-entry bookkeeping, and a host of
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   146
% other things modern civilization has found useful.
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   147
%
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   148
% Real Programming in Functional Languages
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   149
% Xerox PARC technical report
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   150
% CSL·81·11 July 1,1981
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   151
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   152
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   153
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   154
\subsection*{Introduction}
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   155
178
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   156
\noindent
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   157
Scala is a programming language that combines functional and
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   158
object-oriented programming-styles. It has received quite a bit of
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   159
attention in the last ten or so years. One reason for this attention is
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   160
that, like the Java programming language, Scala compiles to the Java
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   161
Virtual Machine (JVM) and therefore Scala programs can run under MacOSX,
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   162
Linux and Windows. Because of this it has also access to
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   163
the myriads of Java libraries. Unlike Java, however, Scala often allows
186
f211d9cb856e updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   164
programmers to write very concise and elegant code.  Some therefore say
182
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   165
``Scala is the better Java''.\footnote{from
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
   166
  \url{https://www.slideshare.net/maximnovak/joy-of-scala}, though this might
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   167
be outdated as latest versions of Java are catching up somewhat}
188
937c995b047a updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   168
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   169
A number of companies---the Guardian, Duolingo, Coursera, FourSquare,
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   170
Netflix, LinkedIn, ITV, Disney to name a few---either use Scala exclusively in
188
937c995b047a updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   171
production code, or at least to some substantial degree. Scala seems
937c995b047a updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   172
also useful in job-interviews (especially in data science) according to
937c995b047a updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   173
this anecdotal report
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   174
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   175
\begin{quote}
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   176
\url{http://techcrunch.com/2016/06/14/scala-is-the-new-golden-child}
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   177
\end{quote}
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   178
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   179
\noindent
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   180
The official Scala web-page is here:
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   181
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   182
\begin{quote}
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   183
\url{http://www.scala-lang.org}\medskip
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   184
\end{quote}
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   185
395
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
   186
\noindent\alert
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   187
For PEP, make sure you are using the version 3(!) of Scala. This is
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   188
the version I am going to use in the lectures and in the coursework. This
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   189
can be any version of Scala 3.X where $X=\{3,4\}$. Also the minor
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   190
number does not matter. Note that this will be the second year I am
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   191
using this newer version of Scala -- some hiccups can still happen. Apologies
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   192
in advance!\bigskip
395
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
   193
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   194
\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   195
  I will be using the \textbf{\texttt{scala-cli}} REPL for Scala 3, rather
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   196
  than the ``plain'' Scala REPL. This is a batteries included version of
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   197
  Scala 3 and is easier to use and to install. In fact
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   198
  \texttt{scala-cli} is designated to replace
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   199
  the ``plain'' Scala REPL in future versions of Scala.
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   200
  So why not using it now?
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   201
  It can be downloaded from:
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   202
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   203
  \begin{center}
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   204
  \url{https://scala-cli.virtuslab.org}
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   205
  \end{center}
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   206
\end{tcolorbox}\medskip
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   207
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   208
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   209
\noindent
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   210
If you are interested, there are also experimental backends for Scala
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   211
for generating JavaScript code (\url{https://www.scala-js.org}), and
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   212
there is work under way to have a native Scala compiler generating
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   213
X86-code (\url{http://www.scala-native.org}). There are also some
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   214
tricks for Scala programs to run as a native
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   215
GraalVM~\hr{https://scala-cli.virtuslab.org/docs/cookbooks/native-images/}
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   216
image.  Though be warned these backends are still rather beta or even
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   217
alpha.
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   218
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   219
\subsection*{VS Code and Scala}
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   220
184
84dc794928de updated
pdated
parents: 183
diff changeset
   221
I found a convenient IDE for writing Scala programs is Microsoft's
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   222
\textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and
269
86a85865e772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 265
diff changeset
   223
obviously Windows.\footnote{\ldots{}unlike \emph{Microsoft Visual Studio}---note
191
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   224
the minuscule difference in the name---which is a heavy-duty,
269
86a85865e772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 265
diff changeset
   225
Windows-only IDE\ldots{}jeez, with all their money could they not have come
191
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   226
up with a completely different name for a complete different project?
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   227
For the pedantic, Microsoft Visual Studio is an IDE, whereas Visual
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   228
Studio Code is considered to be a \emph{source code editor}. Anybody
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   229
out there knows what the
191
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   230
difference is?} It can be downloaded for free from
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   231
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   232
\begin{quote}
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   233
\url{https://code.visualstudio.com}
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   234
\end{quote}
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   235
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   236
\noindent
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   237
and should already come pre-installed in the Department (together with
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   238
the Scala compiler). Being a project that just started in 2015, VS Code is
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   239
relatively new and therefore far from perfect. However it includes a
182
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   240
\textit{Marketplace} from which a multitude of extensions can be
184
84dc794928de updated
pdated
parents: 183
diff changeset
   241
downloaded that make editing and running Scala code a little easier (see
84dc794928de updated
pdated
parents: 183
diff changeset
   242
Figure~\ref{vscode} for my setup).
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   243
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   244
\begin{figure}[t]
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   245
\begin{boxedminipage}{\textwidth}
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   246
\begin{center}
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   247
\includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{}
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   248
\end{center}
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   249
\caption{My installation of VS Code / Codium includes the
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   250
  package  \textbf{Scala Syntax (official)} 0.5.7 from Marketplace.
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   251
  I have also bound the keys \keys{Ctrl} \keys{Ret} to the
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   252
  action ``Run-Selected-Text-In-Active-Terminal'' in order to quickly
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   253
  evaluate small code snippets in the Scala REPL. I use Codium's internal
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   254
  terminal to run \texttt{scala-cli} version 1.0.5 which
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   255
  uses Scala 3.3.1.\label{vscode}}
184
84dc794928de updated
pdated
parents: 183
diff changeset
   256
\end{boxedminipage}
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   257
\end{figure}
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   258
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   259
Actually \alert last year I switched to VS Codium as IDE for writing Scala programs. VS Codium is VS Code
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   260
minus all the telemetry data that is normally sent to Microsoft. Apart from
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   261
the telemetry (and Copilot, which you are not supposed to use anyway),
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   262
it works pretty much the same way as the original but is driven by a
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   263
dedicated community, rather than a big company. You can download VS
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   264
Codium from
441
3bfe81972674 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 426
diff changeset
   265
3bfe81972674 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 426
diff changeset
   266
\begin{quote}
3bfe81972674 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 426
diff changeset
   267
\url{https://vscodium.com}
3bfe81972674 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 426
diff changeset
   268
\end{quote}
3bfe81972674 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 426
diff changeset
   269
3bfe81972674 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 426
diff changeset
   270
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   271
What I like most about VS Code/Codium is that it provides easy access
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   272
to any Scala REPL. But if you prefer another editor for coding, it is
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   273
also painless to work with Scala completely on the command line (as
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   274
you might do with \texttt{g++} in the second part of PEP). For
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   275
the lazybones among us, there are even online editors and environments
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   276
for developing and running Scala programs: for example \textit{Scastie}
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   277
is one of them. It requires zero setup
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   278
(assuming you have a browser handy). You can access it at
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   279
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   280
\begin{quote}
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   281
  \url{https://scastie.scala-lang.org}
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   282
\end{quote}
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   283
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   284
\noindent
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   285
But you should be careful if you use them for your coursework: they
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   286
are meant to play around, not really for serious work. Therefore make
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   287
sure \texttt{scala-cli} works on your own machine ASAP!
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   288
441
3bfe81972674 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 426
diff changeset
   289
As one might expect, Scala can be used with the heavy-duty IDEs
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   290
Eclipse and IntelliJ. For example IntelliJ includes plugins for
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   291
Scala
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   292
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   293
\begin{quote}
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   294
\url{https://scalacenter.github.io/bloop/docs/ides/intellij}
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   295
\end{quote}
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   296
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   297
\noindent
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   298
\underline{\textbf{BUT}}, I do \textbf{not} recommend the usage of
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   299
either Eclipse or IntelliJ for PEP: for the small programs that we will write
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   300
in this module, these IDEs seem to make your life
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   301
harder, rather than easier. They are really meant to be used when you have a
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   302
million-lines codebase instead of our small
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   303
``toy-programs''\ldots{}for example why on earth am I required to
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   304
create a completely new project with several subdirectories when I
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   305
just want to try out 20-lines of Scala code? Your mileage may vary
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   306
though.~\texttt{;o)}
182
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   307
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   308
\subsection*{Why Functional Programming?}
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   309
186
f211d9cb856e updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   310
Before we go on, let me explain a bit more why we want to inflict upon
f211d9cb856e updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   311
you another programming language. You hopefully have mastered Java and
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   312
soon will master C++ as well, you possibly know Python already\ldots{}
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   313
the world should be your oyster, no?  Well, as usual matters are not as simple
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   314
as one might wish. We do require Scala in PEP, but actually we do not
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   315
religiously care whether you learn Scala---after all it is just a
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   316
programming language (albeit a nifty one IMHO). What we do care about
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   317
is that you learn about \textit{functional programming}. Scala is just
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   318
the vehicle for that. Still, you need to learn Scala well enough to
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   319
get good marks in PEP, but functional programming could perhaps
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   320
equally be taught with Haskell, F\#, SML, Ocaml, Kotlin, Clojure,
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   321
Scheme, Elm and many other functional programming languages.
333
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   322
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   323
%Your friendly lecturer just
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   324
%happens to like Scala and the Department agreed that it is a good idea
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   325
%to inflict Scala upon you.
182
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   326
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   327
Very likely writing programs in a functional programming language is
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   328
quite different from what you are used to in your study so far. It
183
f3767098552b updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   329
might even be totally alien to you. The reason is that functional
f3767098552b updated
Christian Urban <urbanc@in.tum.de>
parents: 182
diff changeset
   330
programming seems to go against the core principles of
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   331
\textit{imperative programming} (which is what you do in Java and
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   332
C/C++). The main idea of imperative programming is that
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   333
you have some form of \emph{state} in your program and you
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   334
continuously change this state by issuing some commands---for example
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   335
for updating a field in an array or for adding one to a variable
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   336
stored in memory and so on. The classic example for this style of
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   337
programming is a \texttt{for}-loop in say Java and C/C++.  Consider the snippet:
182
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   338
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   339
\begin{lstlisting}[language=C,numbers=none]
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   340
for (int i = 10; i < 20; i++) {
269
86a85865e772 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 265
diff changeset
   341
      //...do something with i...
184
84dc794928de updated
pdated
parents: 183
diff changeset
   342
}
182
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   343
\end{lstlisting}
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   344
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   345
\noindent Here the integer variable \texttt{i} embodies part of the
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   346
state of the program, which is first set to \texttt{10} and then
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   347
increased by one in each loop-iteration until it reaches \texttt{20}
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   348
at which point the loop exits. When this code is compiled and actually
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   349
runs, there will be some dedicated space reserved for \texttt{i} in
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   350
memory. This space of typically 32 bits contains \texttt{i}'s current
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   351
value\ldots\texttt{10} at the beginning, and then the content will be
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   352
overwritten with new content in every iteration. The main point here
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   353
is that this kind of updating, or overwriting, of memory is
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   354
25.806\ldots or \textbf{THE ROOT OF ALL EVIL}!!
186
f211d9cb856e updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   355
f211d9cb856e updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   356
\begin{center}
f211d9cb856e updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   357
\includegraphics[scale=0.25]{../pics/root-of-all-evil.png}
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   358
\end{center}
186
f211d9cb856e updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   359
182
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   360
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   361
\noindent
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   362
\ldots{}Well, it is perfectly benign if you have a sequential program
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   363
that gets run instruction by instruction...nicely one after another.
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   364
This kind of running code uses a single core of your CPU and goes as
184
84dc794928de updated
pdated
parents: 183
diff changeset
   365
fast as your CPU frequency, also called clock-speed, allows. The problem
84dc794928de updated
pdated
parents: 183
diff changeset
   366
is that this clock-speed has not much increased over the past decade and
84dc794928de updated
pdated
parents: 183
diff changeset
   367
no dramatic increases are predicted for any time soon. So you are a bit
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   368
stuck. This is unlike previous generations of developers who could rely
278
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
   369
upon the fact that approximately every 2 years their code would run
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
   370
twice as fast  because the clock-speed of their CPUs got twice as fast.
333
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   371
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   372
Unfortunately this does not happen any more nowadays. To get you out
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   373
of this dreadful situation, CPU producers pile more and more cores
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   374
into CPUs in order to make them more powerful and potentially make
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   375
software faster. The task for you as developer is to take somehow
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   376
advantage of these cores by running as much of your code as possible
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   377
in parallel on as many cores you have available (typically 4-8 or even
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   378
more in modern laptops and sometimes much more on high-end
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   379
machines---and we conveniently ignore here how many cores are on modern
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   380
GPUs, which can be hundreds or even thousands). In this situation
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   381
\textit{mutable} variables like \texttt{i} in
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   382
the for-loop above are evil, or at least a major nuisance: Because if
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   383
you want to distribute some of the loop-iterations over several cores
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   384
that are currently idle in your system, you need to be extremely
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   385
careful about who can read and overwrite the variable
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   386
\texttt{i}.\footnote{If you are of the mistaken belief that nothing
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   387
  nasty can happen to \texttt{i} inside the \texttt{for}-loop, then
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   388
  you will have to be extra careful with the C++ material.}
333
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   389
Especially the writing operation is critical because you do not want
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   390
that conflicting writes mess about with \texttt{i}. Take my word: an
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   391
untold amount of misery has arisen from this problem. The catch is
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   392
that if you try to solve this problem in languages like C/C++ or Java, and be as
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   393
defensive as possible about reads and writes to \texttt{i}, then you
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   394
need to synchronise access to it. The result is that very often your
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   395
program waits more than it runs, thereby defeating the point of trying
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   396
to run the program in parallel in the first place. If you are less
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   397
defensive, then usually all hell breaks loose by seemingly obtaining
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   398
random results. And forget the idea of being able to debug such
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   399
code. If you want to watch a 5-minute video of horror stories, feel
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   400
free to follow \ldots{}
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   401
\hr{https://www.youtube.com/watch?v=LdLUgCJkiHY} \raisebox{-0.7mm}{\emoji{rofl}} (I love the fact, he
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   402
says at 4:02 mins that he does not understand how the JVM really
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   403
works\ldots{} I always assumed I am the only idiot who does not
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   404
understand how threads work on the JVM. Apparently not.
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   405
But the point is that I am a functional programmer: I do not care -- I do not have to
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   406
understand them.)\bigskip
182
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   407
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   408
\noindent
184
84dc794928de updated
pdated
parents: 183
diff changeset
   409
The central idea of functional programming is to eliminate any state
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   410
and all mutable variables from programs---or at least from the ``interesting bits'' of the
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   411
programs. Because then it is easy to parallelise the resulting
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   412
programs: if you do not have any state, then once created, all memory
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   413
content stays unchanged and reads to such memory are absolutely safe
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   414
without the need of any synchronisation. An example is given in
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   415
Figure~\ref{mand} where in the absence of the annoying state, Scala
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   416
makes it very easy to calculate the Mandelbrot set on as many cores of
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   417
your CPU as possible. Why is it so easy in this example? Because each
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   418
pixel in the Mandelbrot set can be calculated independently and the
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   419
calculation does not need to update any variable. It is so easy in
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   420
fact that going from the sequential version of the Mandelbrot program
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   421
to the parallel version can be achieved by adding just eight
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   422
characters---in two places you have to add \texttt{.par}. Try the same
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   423
in C/C++ or Java!
182
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   424
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   425
\begin{figure}[p]
184
84dc794928de updated
pdated
parents: 183
diff changeset
   426
\begin{boxedminipage}{\textwidth}
187
4d300409f2fe updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   427
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   428
A Scala program for generating pretty pictures of the Mandelbrot set.\smallskip\\
191
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   429
(See \url{https://en.wikipedia.org/wiki/Mandelbrot_set} or\\
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   430
\phantom{(See }\url{https://www.youtube.com/watch?v=aSg2Db3jF_4}):
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   431
\begin{center}
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   432
\begin{tabular}{c}
191
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   433
\includegraphics[scale=0.11]{../pics/mand1.png}\\[-8mm]\mbox{}
184
84dc794928de updated
pdated
parents: 183
diff changeset
   434
\end{tabular}
187
4d300409f2fe updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   435
\end{center}
184
84dc794928de updated
pdated
parents: 183
diff changeset
   436
187
4d300409f2fe updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   437
\begin{center}
4d300409f2fe updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   438
\begin{tabular}{@{}p{0.45\textwidth}|p{0.45\textwidth}@{}}
191
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   439
  \bf sequential version: & \bf parallel version on 4 cores:\smallskip\\
182
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   440
191
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   441
  {\hfill\includegraphics[scale=0.11]{../pics/mand4.png}\hfill} &
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   442
  {\hfill\includegraphics[scale=0.11]{../pics/mand3.png}\hfill} \\
187
4d300409f2fe updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   443
4d300409f2fe updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   444
{\footnotesize\begin{lstlisting}[xleftmargin=-1mm]
186
f211d9cb856e updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   445
for (y <- (0 until H)) {
f211d9cb856e updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   446
  for (x <- (0 until W)) {
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   447
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   448
    val c = start +
186
f211d9cb856e updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   449
      (x * d_x + y * d_y * i)
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   450
    val iters = iterations(c, max)
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   451
    val colour =
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   452
      if (iters == max) black
186
f211d9cb856e updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   453
      else colours(iters % 16)
f211d9cb856e updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   454
191
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   455
    pixel(x, y, colour)
186
f211d9cb856e updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   456
  }
f211d9cb856e updated
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   457
  viewer.updateUI()
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   458
}
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   459
\end{lstlisting}}
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   460
&
187
4d300409f2fe updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   461
{\footnotesize\begin{lstlisting}[xleftmargin=0mm]
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 395
diff changeset
   462
for (y <- (0 until H).par) {
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 395
diff changeset
   463
  for (x <- (0 until W).par) {
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   464
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   465
    val c = start +
187
4d300409f2fe updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   466
      (x * d_x + y * d_y * i)
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   467
    val iters = iterations(c, max)
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   468
    val colour =
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   469
      if (iters == max) black
187
4d300409f2fe updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   470
      else colours(iters % 16)
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   471
191
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   472
    pixel(x, y, colour)
187
4d300409f2fe updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   473
  }
4d300409f2fe updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   474
  viewer.updateUI()
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   475
}
191
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   476
\end{lstlisting}}\\[-2mm]
187
4d300409f2fe updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   477
4d300409f2fe updated
Christian Urban <urbanc@in.tum.de>
parents: 186
diff changeset
   478
\centering\includegraphics[scale=0.5]{../pics/cpu2.png} &
188
937c995b047a updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   479
\centering\includegraphics[scale=0.5]{../pics/cpu1.png}
184
84dc794928de updated
pdated
parents: 183
diff changeset
   480
\end{tabular}
84dc794928de updated
pdated
parents: 183
diff changeset
   481
\end{center}
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   482
\caption{The code of the two ``main'' loops in my version of the mandelbrot program.
191
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   483
The parallel version differs only in \texttt{.par} being added to the
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   484
``ranges'' of the x and y coordinates. As can be seen from the CPU loads, in
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   485
the sequential version there is a lower peak for an extended period,
191
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
   486
while in the parallel version there is a short sharp burst for
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   487
essentially the same workload\ldots{}meaning you get more work done
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   488
in a shorter amount of time. This easy \emph{parallelisation}
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   489
only works reliably with immutable programs.
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   490
\label{mand}}
184
84dc794928de updated
pdated
parents: 183
diff changeset
   491
\end{boxedminipage}
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   492
\end{figure}
182
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   493
275
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   494
But remember this easy parallelisation of code requires that we have no
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   495
state in our programs\ldots{}that is \emph{no} counters like \texttt{i} in
275
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   496
\texttt{for}-loops. You might then ask, how do I write loops without
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   497
such counters? Well, teaching you that this is possible is one of the
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   498
main points of the Scala-part in PEP. I can assure you it \emph{is} possible,
275
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   499
but you have to get your head around it. Once you have mastered this, it
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   500
will be fun to have no state in your programs (a side product is that it
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   501
much easier to debug state-less code and it is also more often than not easier
275
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   502
to understand). So have fun with Scala!\footnote{If you are still not
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   503
convinced about the function programming ``thing'', there are a few more
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   504
arguments: a lot of research in programming languages happens to take
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   505
place in functional programming languages. This has resulted in
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   506
ultra-useful features such as pattern-matching, strong type-systems,
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   507
laziness, implicits, algebraic datatypes  to name a few. Imperative
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   508
languages seem to often lag behind in adopting them: I know, for
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   509
example, that Java will at some point in the future support
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   510
pattern-matching, which has been used for example in SML for at least
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   511
40(!) years. See
441
3bfe81972674 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 426
diff changeset
   512
\url{https://openjdk.org/projects/amber/design-notes/patterns/pattern-matching-for-java}.
275
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   513
Automatic garbage collection was included in Java in 1995; the
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   514
functional language LISP had this already in 1958. Generics were added
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   515
to Java 5 in 2004; the functional language SML had it since 1990.
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   516
Higher-order functions were added to C\# in 2007, to Java 8 in
275
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   517
2014; again LISP had them since 1958. Also Rust, a C-like programming
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   518
language that has been developed since 2010 and is gaining quite some
eb1b4ad23941 updated
Christian Urban <urbanc@in.tum.de>
parents: 274
diff changeset
   519
interest, borrows many ideas from functional programming from
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   520
yesteryear.}\medskip
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   521
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   522
\noindent
441
3bfe81972674 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 426
diff changeset
   523
If you need any after-work distractions, you might have fun reading
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   524
the following article about FP (functional programming) --- you
441
3bfe81972674 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 426
diff changeset
   525
might have to disable your browser cookies though if you want to read
3bfe81972674 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 426
diff changeset
   526
it for free. And spoiler alert: This is tongue-in-cheek \texttt{;o)}
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   527
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   528
\begin{quote}
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   529
  \url{https://archive.ph/vrofC}
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   530
\end{quote}
188
937c995b047a updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   531
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   532
\noindent
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   533
Relevant xkcd entries about functional programming are XXX.
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   534
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   535
\subsection*{The Very Basics}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   536
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   537
Let us get back to Scala and \texttt{scala-cli}: One advantage of
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   538
Scala over Java is that it includes an interpreter (a REPL, or
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   539
\underline{R}ead-\underline{E}val-\underline{P}rint-\underline{L}oop)
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   540
with which you can run and test small code snippets without the need
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   541
of a compiler. This helps a lot with interactively developing
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   542
programs. It is my preferred way of writing small Scala programs. Once
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   543
you installed \texttt{scala-cli}, you can start the interpreter by typing on the
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   544
command line:
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   545
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   546
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   547
$ scala-cli
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   548
Welcome to Scala 3.4.1 (21.0.2, Java OpenJDK 64-Bit Server VM).
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   549
Type in expressions for evaluation. Or try :help.
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   550
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   551
scala>
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   552
\end{lstlisting}%$
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   553
476
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   554
\noindent The precise response may vary depending on the version and
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   555
platform where you installed \texttt{scala-cli}. Make sure however that
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   556
\texttt{scala-cli} uses Scala version 3---you can find the version
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   557
number in the welcome message. Also note that at the first time
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   558
\texttt{scala-cli} runs, it might download various components, for
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   559
example the Scala compiler, Scala runtimes etc. Once
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   560
\texttt{scala-cli} is up and running, you can type at the prompt
7550c816187a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   561
expressions like \code{2 + 3}\;\keys{Ret} and the output will be
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   562
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   563
\begin{lstlisting}[numbers=none,language={}]
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   564
scala> 2 + 3
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   565
val res0: Int = 5
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   566
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   567
188
937c995b047a updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   568
\noindent The answer means that he result of the addition is of type
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   569
\code{Int} and the actual result is 5; \code{res0} is a name that
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
   570
Scala gives automatically to the result. You can reuse this name later
188
937c995b047a updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   571
on, for example
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   572
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   573
\begin{lstlisting}[numbers=none,language={}]
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   574
scala> res0 + 4
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   575
val res1: Int = 9
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   576
\end{lstlisting}
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   577
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   578
\noindent
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   579
Another classic example you can try out is
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   580
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   581
\begin{lstlisting}[numbers=none,language={}]
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   582
scala> println("hello world")
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   583
hello world
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   584
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   585
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   586
\noindent Note that in this case there is no result! The
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   587
reason is that \code{println} does not actually produce a result
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   588
(there is no \code{resX} and no type), rather it is a
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   589
function that causes the \emph{side-effect} of printing out a
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   590
string. Once you are more familiar with the functional
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   591
programming-style, you will know what the difference is
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   592
between a function that returns a result, like addition, and a
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   593
function that causes a side-effect, like \code{println}. We
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   594
shall come back to this point later, but if you are curious
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   595
now, the latter kind of functions always has \code{Unit} as
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   596
return type. It is just not printed by Scala.
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   597
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   598
You can try more examples with the \texttt{scala-cli} REPL, but feel free to
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   599
first guess what the result is (not all answers by Scala are obvious):
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   600
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   601
\begin{lstlisting}[numbers=none,language={}]
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   602
scala> 2 + 2
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   603
scala> 1 / 2
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   604
scala> 1.0 / 2
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   605
scala> 1 / 2.0
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   606
scala> 1 / 0
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   607
scala> 1.0 / 0.0
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   608
scala> true == false
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   609
scala> true && false
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   610
scala> 1 > 1.0
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   611
scala> "12345".length
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   612
scala> List(1,2,1).size
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   613
scala> Set(1,2,1).size
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   614
scala> List(1) == List(1)
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   615
scala> Array(1) == Array(1)
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 264
diff changeset
   616
scala> Array(1).sameElements(Array(1))
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   617
scala> 0.1 + 0.2 == 0.3
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   618
\end{lstlisting}
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   619
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   620
\noindent
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   621
If you think Scala's answer in the last line is braindamaged, try the
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   622
same in your own favourite language.
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   623
Also observe carefully what Scala responds in the following
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   624
three instances involving the constant \lstinline!1!---can
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   625
you explain the differences?
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   626
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   627
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   628
\begin{lstlisting}[numbers=none]
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   629
scala> 1
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   630
scala> 1L
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   631
scala> 1F
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   632
\end{lstlisting}\smallskip
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   633
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   634
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   635
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   636
\noindent
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   637
Please take the Scala REPL seriously: If you want to take advantage of my
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   638
reference implementation for the assignments, you will need to be
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   639
able to ``play around'' with it!
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   640
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   641
\subsection*{Standalone Scala Apps}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   642
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   643
If you want to write a standalone app in Scala, you can
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   644
implement a function \texttt{hello} and annotate the tag
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   645
\texttt{@main}. For example write
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   646
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   647
\begin{lstlisting}[numbers=none]
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   648
@main
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   649
def Hello() = println("hello world")
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   650
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   651
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   652
%object Hello extends App {
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   653
%    println("hello world")
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   654
%}
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   655
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   656
\noindent save it in a file, say {\tt hello-world.scala}, and
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   657
then use \texttt{scala-cli} (which compiles the
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   658
scala file and runs it):
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   659
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   660
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   661
$ scala-cli hello-world.scala
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   662
hello world
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   663
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   664
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   665
\noindent
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   666
Like Java, Scala targets the JVM and consequently
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   667
Scala programs can also be executed by the bog-standard Java
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   668
Runtime. This can be done as follows:
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   669
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   670
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   671
$ scala-cli --power package --assembly hello-world.scala
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   672
$ java -jar Hello.jar
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   673
hello world
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   674
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   675
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   676
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   677
\subsection*{Values}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   678
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   679
\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   680
  Do not use \code{var} in your code for PEP! This declares a mutable variable.
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   681
  Only use \code{val}! This is for \emph{immutable} values.
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   682
\end{tcolorbox}\medskip
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   683
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   684
\noindent
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   685
In the lectures I will try to avoid as much as possible the term
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   686
\emph{variables} familiar from other programming languages. The reason
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   687
is that Scala has \emph{values}, which can be seen as abbreviations of
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   688
potentially larger expressions. The keyword for defining values is \code{val}.
271
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   689
For example
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   690
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   691
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   692
scala> val x = 42
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   693
val x: Int = 42
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   694
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   695
scala> val y = 3 + 4
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   696
val y: Int = 7
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   697
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   698
scala> val z = x / y
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   699
val z: Int = 6
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   700
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   701
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   702
\noindent
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   703
As can be seen, we first define \code{x} and \code{y} with admittedly some silly
271
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   704
expressions, and then reuse these values in the definition of \code{z}.
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   705
All easy, right? Why the kerfuffle about values? Well, values are
271
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   706
\emph{immutable}. You cannot change their value after you defined them.
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   707
If you try to reassign \code{z} above, Scala will yell at you:
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   708
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   709
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   710
scala> z = 9
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   711
-- [E052] Type Error: -----------------------------------
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   712
1 |z = 9
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   713
  |^^^^^
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   714
  |Reassignment to val z
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   715
  | ...
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   716
1 error found
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   717
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   718
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   719
\noindent
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   720
So it would be a bit absurd to call values as variables...you cannot
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   721
change them; they cannot vary. You might think you can reassign them like
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   722
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   723
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   724
scala> val x = 42
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   725
scala> val z = x / 7
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   726
scala> val x = 70
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   727
scala> println(z)
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   728
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   729
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   730
\noindent but try to guess what Scala will print out
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   731
for \code{z}?  Will it be \code{6} or \code{10}? A final word about
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   732
values: Try to stick to the convention that names of values should be
188
937c995b047a updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   733
lower case, like \code{x}, \code{y}, \code{foo41} and so on. Upper-case
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   734
names you should reserve for what is called \emph{constructors}. And
271
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   735
forgive me when I call values as variables\ldots{}it is just something that
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   736
has been in imprinted into my developer-DNA during my early years and
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   737
is difficult to get rid of.~\texttt{;o)}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   738
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   739
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   740
\subsection*{Function Definitions}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   741
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   742
We do functional programming! So defining functions will be our main occupation.
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   743
As an example, a function named \code{f} taking a single argument of type
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   744
\code{Int} can be defined in Scala as follows:
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   745
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   746
\begin{lstlisting}[numbers=none]
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   747
def f(x: Int) : String = ...YOUR CODE...
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   748
\end{lstlisting}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   749
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   750
\noindent
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   751
This function returns the value resulting from evaluating the expression
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   752
what your code is. Since we declared
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   753
\code{String} after the colon, the result of this function will be of type
271
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   754
\code{String}. It is a good habit to always include this information
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   755
about the return type, while it is only strictly necessary to give this
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   756
type in recursive functions (later more on that). Simple examples of Scala functions are:
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   757
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   758
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   759
def incr(x: Int) : Int = x + 1
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   760
def double(x: Int) : Int = x + x
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   761
def square(x: Int) : Int = x * x
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   762
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   763
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   764
\noindent
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   765
The general scheme for functions is
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   766
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   767
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   768
def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   769
  ...BODY_OF_FUNCTION...
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   770
}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   771
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   772
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   773
\noindent
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   774
where each argument, \texttt{arg1}, \texttt{arg2} and so on, requires
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   775
its type and the result type of the
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   776
function, \code{rty}, should also be given. If the body of the function is
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   777
more complex, then it can be enclosed in braces, like above. If it
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   778
is just a simple expression, like \code{x + 1}, you can omit the
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   779
braces. Very often functions are recursive (that is call themselves),
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
   780
like the venerable factorial function:
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   781
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   782
\begin{lstlisting}[numbers=none]
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   783
def fact(n: Int) : Int =
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   784
  if (n == 0) 1 else n * fact(n - 1)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   785
\end{lstlisting}
188
937c995b047a updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   786
937c995b047a updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
   787
\noindent
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   788
In this case we have to give the return type \code{Int}. But as said, it is a good
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   789
habit to give the return type for all functions. Note we could also
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   790
have written this with braces as
271
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   791
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   792
\begin{lstlisting}[numbers=none]
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   793
def fact(n: Int) : Int = {
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   794
  if (n == 0) 1
271
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   795
  else n * fact(n - 1)
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   796
}
271
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   797
\end{lstlisting}
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   798
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   799
\noindent
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   800
but this seems a bit overkill for a small function like \code{fact}.
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   801
Notice that I did not use a \code{then}-keyword in the
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   802
\code{if}-statements and that I enclosed the condition inside
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   803
parentheses, like \code{(n == 0)}. Your eyes might hurt to not see an
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   804
\code{then} with an \code{if}, but this has been long established
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   805
syntax for \code{if}-statements. Scala, to my knowledge, was pretty
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   806
unique in that for nearly 20 years of its existence\ldots{}until Scala
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   807
3 came along. While people like me have perfectly adapted to the sight
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   808
of \code{if}s without \code{then}s, it seems the developers of Scala
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   809
caved in to the special eyesight of Gen-Python people (I am sure that is not you) and now allow to
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   810
write the same function also as
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   811
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   812
\begin{lstlisting}[numbers=none]
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   813
def fact(n: Int) : Int = {
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   814
  if n == 0 then 1
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   815
  else n * fact(n - 1)
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   816
}
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   817
\end{lstlisting}
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   818
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   819
\noindent
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   820
The main difference between both versions is that if you want to drop the \code{then}, then you need to
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   821
enclose the boolean expression within parentheses.
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   822
I accept the second version might look a bit more familiar to beginners of Scala, if
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   823
they come from other languages, like Python, Java or C++. But that we also had
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   824
to get rid in Scala 3 of the familiar \texttt{\{\}}-parentheses is
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   825
completely beyond me. So in Scala 3 the braces are optional and the
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   826
\texttt{fact}-function can even be written as
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   827
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   828
\begin{lstlisting}[numbers=none]
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   829
def fact(n: Int) : Int =
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   830
     if n == 0
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   831
     then 1
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   832
     else n * fact(n - 1)
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   833
\end{lstlisting}
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   834
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   835
\noindent on the condition that indents now become \emph{meaningful}
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   836
(as in Python).\raisebox{-0.7mm}{\emoji{face-vomiting}} All versions are now permitted in Scala 3. While you
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   837
are free to use any syntax version you want in PEP, or even mix them,
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   838
I will \textbf{not} show you any of my code in the newfangled
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   839
Pythonesque meaningful-indent-syntax. When necessary, I will always
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   840
use braces to indicate the beginning and end of a code block, and I
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   841
have not yet completely got used to the \code{if}s with
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   842
\code{then}s. Please forgive me for being still inconsistent with this\footnote{Scala adopted some very fine features of Python, for example string interpolations, but that we had to completely cave in to
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   843
  the demands of Gen-Python is a bridge too far for my completely
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   844
  insignificant opinion.
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   845
  I always assumed escaping Python's dependency hell
485
19b75e899d37 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 483
diff changeset
   846
is every software developers life goal---apparently not. \emoji{exploding-head}}
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   847
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   848
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   849
However, no matter which syntax style you adopt for \code{if}s, never
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   850
write an \code{if} without an \code{else}-branch! That has something
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   851
to do with functional programming and you will see its purpose later
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
   852
on. Coming back to function definitions: While \code{def} is the main
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   853
mechanism for defining functions, there are a few other ways for doing
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   854
this. We will see some of them in the next sections.
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   855
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   856
Before we go on, let me explain one tricky point in function
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   857
definitions, especially in larger definitions. What does a Scala
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   858
function return as result? Scala has a \code{return} keyword, but it is
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   859
used for something different than in Java (and C/C++). Therefore please
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   860
make sure no \code{return} slips into your Scala code.
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   861
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   862
So in the absence of \code{return}, what value does a Scala function
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   863
actually produce? A rule-of-thumb is whatever is in the last line of the
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   864
function is the value that will be returned. Consider the following
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   865
example:\footnote{We could have written this function in just one line,
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   866
but for the sake of argument let's keep the two intermediate values.}
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   867
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   868
\begin{lstlisting}[numbers=none]
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   869
def average(xs: List[Int]) : Int = {
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   870
  val s = xs.sum
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   871
  val n = xs.length
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   872
  s / n
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   873
}
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   874
\end{lstlisting}
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   875
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   876
\noindent In this example the expression \code{s / n} is in the last
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   877
line of the function---so this will be the result the function
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   878
calculates. The two lines before just calculate intermediate values.
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   879
This principle of the ``last-line'' comes in handy when you need to
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   880
print out values, for example, for debugging purposes. Suppose you want
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   881
rewrite the average function as
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   882
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   883
\begin{lstlisting}[numbers=none]
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   884
def average(xs: List[Int]) : Int = {
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   885
  val s = xs.sum
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   886
  val n = xs.length
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   887
  val h = xs.head
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   888
  println(s"Input $xs with first element $h")
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   889
  s / n
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   890
}
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   891
\end{lstlisting}
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   892
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   893
\noindent
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   894
Here the function still only returns the expression \code{s / n} in
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   895
the last line.  The \code{println} before just prints out some
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   896
information about the input of this function, but does not contribute
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   897
to the result of the function. Similarly, the value \code{h} is used
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   898
in the \code{println} but does not contribute to what integer is
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   899
returned by the function.
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   900
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   901
A caveat is that the idea with the ``last line'' is only a rough
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   902
rule-of-thumb. A better rule might be: the last expression that is
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   903
evaluated in the function. Consider the following version of
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   904
\code{average}:
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   905
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   906
\begin{lstlisting}[numbers=none]
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   907
def average(xs: List[Int]) : Int = {
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   908
  if (xs.length == 0) 0
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   909
  else xs.sum / xs.length
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   910
}
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   911
\end{lstlisting}
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   912
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   913
\noindent
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   914
What does this function return? Well there are two possibilities: either
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   915
the result of \code{xs.sum / xs.length} in the last line provided the
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   916
list \code{xs} is nonempty, \textbf{or} if the list is empty, then it
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   917
will return \code{0} from the \code{if}-branch (which is technically not
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 334
diff changeset
   918
the last line, but the last expression evaluated by the function in the
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   919
empty-case).
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   920
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   921
Summing up, do not use \code{return} in your Scala code! A function
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   922
returns what is evaluated by the function as the last expression. There
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   923
is always only one such last expression. Previous expressions might
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   924
calculate intermediate values, but they are not returned. If your
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   925
function is supposed to return multiple things, then one way in Scala is
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   926
to use tuples. For example returning the minimum, average and maximum
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   927
can be achieved by
271
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
   928
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   929
\begin{lstlisting}[numbers=none]
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   930
def avr_minmax(xs: List[Int]) : (Int, Int, Int) = {
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   931
  if (xs.length == 0) (0, 0, 0)
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   932
  else (xs.min, xs.sum / xs.length, xs.max)
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   933
}
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   934
\end{lstlisting}
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   935
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   936
\noindent
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   937
which still satisfies the rule-of-thumb: The result of the function is the
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   938
last expression that is run inside the function.
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   939
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   940
\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   941
  Do not use \code{return} in your code to indicate what a function
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   942
  produces as a result! It has a different meaning in Scala than in
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   943
  Java. It can change the meaning of your program, and you should
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   944
  never use it.
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   945
\end{tcolorbox}
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   946
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   947
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
   948
\subsection*{Loops, or Better the Absence Thereof}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   949
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   950
Coming from Java or C/C++, you might be surprised that Scala does
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   951
not really have loops. It has instead, what is in functional
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   952
programming called, \emph{maps}. To illustrate how they work,
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   953
let us assume you have a list of numbers from 1 to 8 and want to
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   954
build the list of corresponding squares. The list of numbers from 1 to 8
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   955
can be constructed in Scala as follows:
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   956
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   957
\begin{lstlisting}[numbers=none,language={}]
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   958
scala> (1 to 8).toList
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
   959
val res1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8)
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   960
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   961
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   962
\noindent  Like in modern versions of Java, the \code{1 to 8} generates a \code{Range}, which is then
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   963
transformed into a list by the \code{.toList}.
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   964
Generating from this list the list of
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   965
squares in an imperative programming language such as C++, you would assume
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   966
the list is given as a kind of array. You would then iterate, or loop,
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   967
an index over this array and replace each entry in the array
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   968
by its square. Right? In Scala, and in other functional
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   969
programming languages, you use maps to achieve the same.
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   970
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   971
A map essentially takes a function that describes how each element is
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   972
transformed (in this example the function is $n \rightarrow n * n$) and
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   973
a list over which this function should work. Pictorially you can think
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   974
of the idea behind maps as follows:
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   975
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   976
\begin{center}
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   977
\begin{tikzpicture}
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
   978
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   979
  \node (A0) at (1.2,0) {\texttt{List(}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   980
  \node (A1) at (2.0,0) {\texttt{1\makebox[0mm]{ ,}}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   981
  \node (A2) at (2.9,0) {\texttt{2\makebox[0mm]{ ,}}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   982
  \node (A3) at (3.8,0) {\texttt{3\makebox[0mm]{ ,}}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   983
  \node (A4) at (4.7,0) {\texttt{4\makebox[0mm]{ ,}}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   984
  \node (A5) at (5.6,0) {\texttt{5\makebox[0mm]{ ,}}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   985
  \node (A6) at (6.5,0) {\texttt{6\makebox[0mm]{ ,}}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   986
  \node (A7) at (7.4,0) {\texttt{7\makebox[0mm]{ ,}}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   987
  \node (A8) at (8.3,0) {\texttt{8)}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   988
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   989
  \node (B0) at (1.2,-3) {\texttt{List(}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   990
  \node (B1) at (2.0,-3) {\texttt{1\makebox[0mm]{ ,}}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   991
  \node (B2) at (3.0,-3) {\texttt{4\makebox[0mm]{ ,}}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   992
  \node (B3) at (4.1,-3) {\texttt{9\makebox[0mm]{ ,}}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   993
  \node (B4) at (5.2,-3) {\texttt{16\makebox[0mm]{ ,}}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   994
  \node (B5) at (6.3,-3) {\texttt{25\makebox[0mm]{ ,}}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   995
  \node (B6) at (7.4,-3) {\texttt{36\makebox[0mm]{ ,}}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   996
  \node (B7) at (8.4,-3) {\texttt{49\makebox[0mm]{ ,}}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   997
  \node (B8) at (9.4,-3) {\texttt{64\makebox[0mm]{ )}}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   998
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
   999
  \draw [->,line width=1mm] (A1.south) -- (B1.north);
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1000
  \draw [->,line width=1mm] (A2.south) -- (B2.north);
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1001
  \draw [->,line width=1mm] (A3.south) -- (B3.north);
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1002
  \draw [->,line width=1mm] (A4.south) -- (B4.north);
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1003
  \draw [->,line width=1mm] (A5.south) -- (B5.north);
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1004
  \draw [->,line width=1mm] (A6.south) -- (B6.north);
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1005
  \draw [->,line width=1mm] (A7.south) -- (B7.north);
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1006
  \draw [->,line width=1mm] (A8.south) -- (B8.north);
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1007
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1008
  \node [red] (Q0) at (-0.3,-0.3) {\large\texttt{n}};
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1009
  \node (Q1) at (-0.3,-0.4) {};
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1010
  \node (Q2) at (-0.3,-2.5) {};
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1011
  \node [red] (Q3) at (-0.3,-2.65) {\large\texttt{n\,*\,n}};
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1012
  \draw [->,red,line width=1mm] (Q1.south) -- (Q2.north);
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1013
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1014
  \node [red] at (-1.3,-1.5) {\huge{}\it\textbf{map}};
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1015
 \end{tikzpicture}
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1016
\end{center}
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1017
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1018
\noindent
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1019
On top is the ``input'' list we want to transform; on the left is the
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1020
``map'' function for how to transform each element in the input list
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1021
(the square function in this case); at the bottom is the result list of
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1022
the map. This means that a map generates a \emph{new} list, unlike a
273
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1023
for-loop in Java or C/C++ which would most likely just update the
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1024
existing list/array.
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1025
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1026
Now there are two ways for expressing such maps in Scala. The first way is
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1027
called a \emph{for-comprehension}. The keywords are \code{for} and
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1028
\code{yield}. Squaring the numbers from 1 to 8 with a for-comprehension
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1029
would look as follows:
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1030
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1031
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1032
scala> for (n <- (1 to 8).toList) yield n * n
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1033
val res2: List[Int] = List(1, 4, 9, 16, 25, 36, 49, 64)
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1034
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1035
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1036
\noindent  This for-comprehension states that from the list of numbers
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1037
we draw some elements. We use the name \code{n} to range over these
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1038
elements (whereby the name is arbitrary; we could use something more
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1039
descriptive if we wanted to). Using \code{n} we compute the result of
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1040
\code{n * n} after the \code{yield}. This way of writing a map resembles
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1041
a bit the for-loops from imperative languages, even though the ideas
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1042
behind for-loops and for-comprehensions are quite different. Also, this
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1043
is a simple example---what comes after \code{yield} can be a complex
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1044
expression enclosed in \texttt{\{...\}}. A more complicated example
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1045
might be
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1046
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1047
\begin{lstlisting}[numbers=none]
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1048
scala> for (n <- (1 to 8).toList) yield {
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1049
         val i = n + 1
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1050
         val j = n - 1
273
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1051
         i * j + 1
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1052
       }
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1053
val res3: List[Int] = List(1, 4, 9, 16, 25, 36, 49, 64)
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1054
\end{lstlisting}
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1055
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1056
Let us come back to the simple example of squaring a list of numbers from above.
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1057
As you can see in the for-comprehensions, we specified the list
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1058
where each \code{n} comes from, namely \code{(1 to 8).toList}, and how
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1059
each element needs to be transformed, the expression after the
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1060
\code{yield}. This can also be expressed in a second way in Scala by
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1061
using directly the function \code{map} as follows:
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1062
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1063
\begin{lstlisting}[numbers=none,language={}]
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1064
scala> (1 to 8).toList.map(n => n * n)
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1065
val res3 = List(1, 4, 9, 16, 25, 36, 49, 64)
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1066
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1067
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1068
\noindent In this way, the expression \code{n => n * n} stands for the
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1069
function that calculates the square (this is how the \code{n}s are
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1070
transformed by the map).  It might not be obvious, but
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1071
the for-comprehensions above are just syntactic sugar: when compiling such
273
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1072
code, Scala translates for-comprehensions into equivalent maps. This
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1073
even works when for-comprehensions get more complicated (see below).
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1074
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1075
The very charming feature of Scala is that such maps or
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1076
for-comprehensions can be written for any kind of data collection, such
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1077
as lists, sets, vectors, options and so on. For example if we instead
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1078
compute the remainders modulo 3 of this list, we can write
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1079
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1080
\begin{lstlisting}[numbers=none,language={}]
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1081
scala> (1 to 8).toList.map(n => n % 3)
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1082
val res4 = List(1, 2, 0, 1, 2, 0, 1, 2)
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1083
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1084
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1085
\noindent If we, however, transform the numbers 1 to 8 not
270
b9eaa5cdec4a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
  1086
into a list, but into a set, and then compute the remainders
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1087
modulo 3 we obtain
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1088
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1089
\begin{lstlisting}[numbers=none,language={}]
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1090
scala> (1 to 8).toSet.map(n => n % 3)
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1091
val res5 = Set(2, 1, 0)
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1092
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1093
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1094
\noindent This\footnote{This returns actually \code{HashSet(1, 2, 3)},
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1095
but this is just an implementation detail of how sets are implemented in
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1096
Scala.} is the correct result for sets, as there are only three
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1097
equivalence classes of integers modulo 3.  Since maps and
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1098
for-comprehensions are just syntactic variants of each other, the latter
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1099
can also be written as
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1100
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1101
\begin{lstlisting}[numbers=none,language={}]
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1102
scala> for (n <- (1 to 8).toSet) yield n % 3
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1103
val res5 = Set(2, 1, 0)
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1104
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1105
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1106
For-comprehensions can also be nested and the selection of
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1107
elements can be guarded (or filtered). For example if we want to pair up
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1108
the numbers 1 to 4 with the letters a to c, we can write
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1109
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1110
\begin{lstlisting}[numbers=none]
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1111
scala> for (n <- (1 to 4).toList;
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1112
            m <- ('a' to 'c').toList) yield (n, m)
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1113
val res6 = List((1,a), (1,b), (1,c), (2,a), (2,b), (2,c),
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1114
                (3,a), (3,b), (3,c), (4,a), (4,b), (4,c))
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1115
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1116
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1117
\noindent
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1118
In this example the for-comprehension ranges over two lists, and
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1119
produces a list of pairs as output. Or, if we want to find all pairs of
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1120
numbers between 1 and 3 where the sum is an even number, we can write
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1121
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1122
\begin{lstlisting}[numbers=none]
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1123
scala> for (n <- (1 to 3).toList;
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1124
            m <- (1 to 3).toList;
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1125
            if (n + m) % 2 == 0) yield (n, m)
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1126
val res7 = List((1,1), (1,3), (2,2), (3,1), (3,3))
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1127
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1128
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1129
\noindent The \code{if}-condition in this for-comprehension filters out
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1130
all pairs where the sum is not even (therefore \code{(1, 2)}, \code{(2,
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1131
1)} and \code{(3, 2)} are not in the result because their sum is odd).
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1132
278
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
  1133
To summarise, maps (or for-comprehensions) transform one collection into
273
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1134
another. For example a list of \code{Int}s into a list of squares, and
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1135
so on. There is no need for for-loops in Scala. But please do not be
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1136
tempted to write anything like
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1137
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1138
\begin{lstlisting}[numbers=none]
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1139
scala> val cs = ('a' to 'h').toList
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1140
scala> for (n <- (0 until cs.length).toList)
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1141
          yield cs(n).capitalize
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1142
val res8: List[Char] = List(A, B, C, D, E, F, G, H)
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1143
\end{lstlisting}
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1144
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1145
\noindent
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1146
This is accepted Scala-code, but utterly bad style (it is more like
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1147
Java). It can be written much clearer as:
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1148
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1149
\begin{lstlisting}[numbers=none]
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1150
scala> val cs = ('a' to 'h').toList
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1151
scala> for (c <- cs) yield c.capitalize
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1152
val res9: List[Char] = List(A, B, C, D, E, F, G, H)
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 271
diff changeset
  1153
\end{lstlisting}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1154
271
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
  1155
\subsection*{Results and Side-Effects}
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
  1156
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1157
While hopefully all this about maps looks reasonable, there is one
273
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1158
complication: In the examples above we always wanted to transform one
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1159
list into another list (e.g.~list of squares), or one set into another
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1160
set (set of numbers into set of remainders modulo 3). What happens if we
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1161
just want to print out a list of integers? In these cases the
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1162
for-comprehensions need to be modified. The reason is that \code{print},
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1163
you guessed it, does not produce any result, but only produces what is
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1164
in the functional-programming-lingo called a \emph{side-effect}\ldots it
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1165
prints something out on the screen. Printing out the list of numbers
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1166
from 1 to 5 would look as follows
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1167
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1168
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1169
scala> for (n <- (1 to 5).toList) print(n)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1170
12345
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1171
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1172
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1173
\noindent
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1174
where you need to omit the keyword \code{yield}. You can
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1175
also do more elaborate calculations before printing such as
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1176
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1177
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1178
scala> for (n <- (1 to 5).toList) {
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1179
  val square = n * n
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1180
  println(s"$n * $n = $square")
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1181
}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1182
1 * 1 = 1
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1183
2 * 2 = 4
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1184
3 * 3 = 9
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1185
4 * 4 = 16
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1186
5 * 5 = 25
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1187
\end{lstlisting}%$
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1188
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1189
\noindent In this code I use a value assignment (\code{val
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1190
square = ...} ) and also what is called in Scala a
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1191
\emph{string interpolation}, written \code{s"..."}. The latter
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1192
is for printing out formatted strings. It allows me to refer to the
270
b9eaa5cdec4a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
  1193
integer values \code{n} and \code{square} inside a string.
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1194
This is very convenient for printing out ``things''.
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1195
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1196
The corresponding map construction for functions with
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1197
side-effects is in Scala called \code{foreach}. So you
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1198
could also write
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1199
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1200
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1201
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1202
scala> (1 to 5).toList.foreach(n => print(n))
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1203
12345
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1204
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1205
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1206
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1207
\noindent or even just
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1208
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1209
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1210
scala> (1 to 5).toList.foreach(print)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1211
12345
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1212
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1213
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1214
\noindent
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1215
If you want to find out more about maps and functions with
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1216
side-effects, you can ponder about the response Scala gives if
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1217
you replace \code{foreach} by \code{map} in the expression
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1218
above. Scala will still allow \code{map} with side-effect
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1219
functions, but then reacts with a slightly interesting result.
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1220
If you understand the difference, you are pretty much on the
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1221
road of becoming a master-functional programmer.
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1222
273
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1223
\subsection*{Aggregates}
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1224
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1225
There is one more usage of for-loops in Java, C/C++ and the like:
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1226
sometimes you want to \emph{aggregate} something about a list, for
278
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
  1227
example summing up all its elements. In this case you cannot use maps,
273
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1228
because maps \emph{transform} one data collection into another data
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1229
collection. They cannot be used to generate a single integer
278
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
  1230
representing an aggregate. So how is this kind of aggregation done in
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
  1231
Scala? Let us suppose you want to sum up all elements from a list. You
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
  1232
might be tempted to write something like
273
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1233
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1234
\begin{lstlisting}[numbers=none]
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1235
var cnt = 0
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1236
for (n <- (1 to 8).toList) {
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1237
  cnt += n
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1238
}
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1239
print(cnt)
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1240
\end{lstlisting}
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1241
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1242
\noindent
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1243
and indeed this is accepted Scala code and produces the expected result,
273
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1244
namely \code{36}, \textbf{BUT} this is imperative style and not
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1245
permitted in PEP. If you submit this kind of code, you get 0 marks. The
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1246
code uses a \code{var} and therefore violates the immutability property
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1247
I ask for in your code. Sorry!
273
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1248
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1249
So how to do that same thing without using a \code{var}? Well there are
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1250
several ways. One way is to define the following recursive
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1251
\code{sum}-function:
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1252
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1253
\begin{lstlisting}[numbers=none]
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1254
def sum(xs: List[Int]) : Int =
273
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1255
  if (xs.isEmpty) 0 else xs.head + sum(xs.tail)
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1256
\end{lstlisting}
273
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1257
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1258
\noindent
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1259
You can then call \code{sum((1 to 8).toList)} and obtain the same result
278
0c2481cd8b1c updated
Christian Urban <urbanc@in.tum.de>
parents: 277
diff changeset
  1260
without a mutable variable and without a for-loop. Obviously for simple things like
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1261
sum, you could have written \code{xs.sum} in the first place. But not
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1262
all aggregate functions are pre-defined and often you have to write your
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1263
own recursive function for this.
273
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
  1264
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1265
%\subsection*{Always Produce a Result! No Exceptions!}
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1266
%
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1267
%Function should always produce a value. Exception is not thrown.
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1268
%Whenever there is a possibility of non-value result (exception, void,
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1269
%undefined, null, etc.), it should be incorporated in the result type.
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1270
%Such types include but not limited to
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1271
%
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1272
%Option[T]
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1273
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1274
%TBD
334
841727e27252 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 333
diff changeset
  1275
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 312
diff changeset
  1276
271
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
  1277
\subsection*{Higher-Order Functions}
48e12e7aee6e updated
Christian Urban <urbanc@in.tum.de>
parents: 270
diff changeset
  1278
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1279
Functions obviously play a central role in functional programming. Two simple
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1280
examples are
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1281
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1282
\begin{lstlisting}[numbers=none]
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1283
def even(x: Int) : Boolean = x % 2 == 0
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1284
def odd(x: Int) : Boolean = x % 2 == 1
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1285
\end{lstlisting}
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1286
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1287
\noindent
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1288
More interestingly, the concept of functions is really pushed to the
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1289
limit in functional programming. Functions can take other functions as
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1290
arguments and can return a function as a result. This is actually
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1291
quite important for making code generic. Assume a list of 10 elements:
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1292
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1293
\begin{lstlisting}[numbers=none]
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1294
val lst = (1 to 10).toList
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1295
\end{lstlisting}
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1296
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1297
\noindent
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1298
Say, we want to filter out all even numbers. For this we can use
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1299
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1300
\begin{lstlisting}[numbers=none]
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1301
scala> lst.filter(even)
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1302
List(2, 4, 6, 8, 10)
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1303
\end{lstlisting}
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1304
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1305
\noindent
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1306
where \code{filter} expects a function as argument specifying which
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1307
elements of the list should be kept and which should be left out. By
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1308
allowing \code{filter} to take a function as argument, we can also
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1309
easily filter out odd numbers as well.
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1310
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1311
\begin{lstlisting}[numbers=none]
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1312
scala> lst.filter(odd)
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1313
List(1, 3, 5, 7, 9)
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1314
\end{lstlisting}
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1315
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1316
\noindent
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1317
Such function arguments are quite frequently used for ``generic'' functions.
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1318
For example it is easy to count odd elements in a list or find the first
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1319
even number in a list:
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1320
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1321
\begin{lstlisting}[numbers=none]
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1322
scala> lst.count(odd)
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1323
5
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1324
scala> lst.find(even)
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1325
Some(2)
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1326
\end{lstlisting}
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1327
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1328
\noindent
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1329
Recall that the return type of \code{even} and \code{odd} are booleans.
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1330
Such function are sometimes called predicates, because they determine
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1331
what should be true for an element and what false, and then performing
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1332
some operation according to this boolean. Such predicates are quite useful.
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1333
Say you want to sort the \code{lst}-list in ascending and descending order.
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1334
For this you can write
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1335
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1336
\begin{lstlisting}[numbers=none]
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1337
lst.sortWith(_ < _)
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1338
lst.sortWith(_ > _)
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1339
\end{lstlisting}
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1340
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1341
\noindent where \code{sortWith} expects a predicate as argument. The
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1342
construction \code{_ < _} stands for a function that takes two arguments
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1343
and returns true when the first one is smaller than the second. You can
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1344
think of this as elegant shorthand notation for
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1345
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1346
\begin{lstlisting}[numbers=none]
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1347
def smaller(x: Int, y: Int) : Boolean = x < y
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1348
lst.sortWith(smaller)
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1349
\end{lstlisting}
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1350
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1351
\noindent
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1352
Say you want to find in \code{lst} the first odd number greater than 2.
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1353
For this you need to write a function that specifies exactly this
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1354
condition. To do this you can use a slight variant of the shorthand
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1355
notation above
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1356
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1357
\begin{lstlisting}[numbers=none]
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1358
scala> lst.find(n => odd(n) && n > 2)
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1359
Some(3)
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1360
\end{lstlisting}
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1361
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1362
\noindent
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1363
Here \code{n => ...} specifies a function that takes \code{n} as
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1364
argument and uses this argument in whatever comes after the double
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1365
arrow. If you want to use this mechanism for looking for an element that
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1366
is both even and odd, then of course you out of luck.
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1367
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1368
\begin{lstlisting}[numbers=none]
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1369
scala> lst.find(n => odd(n) && even(n))
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1370
None
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1371
\end{lstlisting}
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1372
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1373
While functions taking functions as arguments seems a rather useful
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1374
feature, the utility of returning a function might not be so clear.
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1375
I admit the following example is a bit contrived, but believe me
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1376
sometims functions produce other functions in a very meaningful way.
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1377
Say we want to generate functions according to strings, as in
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1378
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1379
\begin{lstlisting}[numbers=none]
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1380
def mkfn(s: String) : (Int => Boolean) =
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1381
  if (s == "even") even else odd
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1382
\end{lstlisting}
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1383
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1384
\noindent
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1385
With this we can generate the required function for \code{filter}
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1386
according to a string:
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1387
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1388
\begin{lstlisting}[numbers=none]
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1389
scala> lst.filter(mkfn("even"))
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1390
List(2, 4, 6, 8, 10)
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1391
scala> lst.filter(mkfn("foo"))
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1392
List(1, 3, 5, 7, 9)
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1393
\end{lstlisting}
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1394
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1395
\noindent
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1396
As said, this is example is a bit contrived---I was not able to think
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1397
of anything simple, but for example in the Compiler module next year I
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1398
show a compilation functions that needs to generate functions as
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1399
intermediate result. Anyway, notice the interesting type we had to
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1400
annotate to \code{mkfn}. The types in Scala are described next.
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1401
274
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
  1402
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1403
\subsection*{Types}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1404
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1405
In most functional programming languages, types play an
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1406
essential role. Scala is such a language. You have already
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1407
seen built-in types, like \code{Int}, \code{Boolean},
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1408
\code{String} and \code{BigInt}, but also user-defined ones,
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1409
like \code{Rexp} (see coursework). Unfortunately, types can be a thorny
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1410
subject, especially in Scala. For example, why do we need to
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1411
give the type to \code{toSet[Int]}, but not to \code{toList}?
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1412
The reason is the power of Scala, which sometimes means it
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1413
cannot infer all necessary typing information. At the
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1414
beginning, while getting familiar with Scala, I recommend a
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1415
``play-it-by-ear-approach'' to types. Fully understanding
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1416
type-systems, especially complicated ones like in Scala, can
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1417
take a module on their own.\footnote{Still, such a study can
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1418
be a rewarding training: If you are in the business of
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1419
designing new programming languages, you will not be able to
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1420
turn a blind eye to types. They essentially help programmers
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1421
to avoid common programming errors and help with maintaining
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1422
code.}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1423
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1424
In Scala, types are needed whenever you define an inductive
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1425
datatype and also whenever you define functions (their
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1426
arguments and their results need a type). Base types are types
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1427
that do not take any (type)arguments, for example \code{Int}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1428
and \code{String}. Compound types take one or more arguments,
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1429
which as seen earlier need to be given in angle-brackets, for
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1430
example \code{List[Int]} or \code{Set[List[String]]} or
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1431
\code{Map[Int, Int]}.
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1432
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1433
Scala provides a basic mechanism to check the type of a (closed)
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1434
expression---closed means that all parts are already known to Scala.
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1435
Then you can use the command \code{:type} and check in the REPL:
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1436
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1437
\begin{lstlisting}[ numbers=none]
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1438
scala> :type (1, List(3), Set(4,5), "hello")
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1439
(Int, List[Int], Set[Int], String)
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1440
\end{lstlisting}
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1441
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1442
\noindent
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1443
If Scala can calculate the type of the given expression, then it
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1444
will print it. Unfortunately, this way of finding out a type is almost
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1445
unusable: for `things' where the type is pretty obvious, it gives an
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1446
answer; but for `things' that are actually of interest (such as
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1447
what is the type of a pre-defined function), it gives up with
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1448
an error message.
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1449
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1450
There are a few special type-constructors that fall outside
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1451
this pattern. One is for tuples, where the type is written
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1452
with parentheses. For example
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1453
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1454
\begin{lstlisting}[ numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1455
(Int, Int, String)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1456
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1457
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1458
\noindent is for a triple (a tuple with three components---two
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1459
integers and a string). Tuples are helpful if you want to
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1460
define functions with multiple results, say the function
270
b9eaa5cdec4a updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
  1461
returning the quotient and remainder of two numbers. For this
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1462
you might define:
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1463
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1464
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1465
\begin{lstlisting}[ numbers=none]
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1466
def quo_rem(m: Int, n: Int) : (Int, Int) =
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1467
  (m / n, m % n)
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1468
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1469
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1470
\noindent Since this function returns a pair of integers, its
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1471
\emph{return type} needs to be of type \code{(Int, Int)}. Incidentally,
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1472
this is also the \emph{input type} of this function. For this notice
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1473
\code{quo_rem} takes \emph{two} arguments, namely \code{m} and \code{n},
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1474
both of which are integers. They are ``packaged'' in a pair.
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1475
Consequently the complete type of \code{quo_rem} is
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1476
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1477
\begin{lstlisting}[ numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1478
(Int, Int) => (Int, Int)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1479
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1480
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1481
\noindent
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1482
This uses another special type-constructor, written as the arrow
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1483
\code{=>}. This is sometimes also called \emph{function arrow}.  For
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1484
example, the type \code{Int => String} is for a function that takes an
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1485
integer as input argument and produces a string as result.  A function
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1486
of this type is for instance
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1487
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1488
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1489
def mk_string(n: Int) : String = n match {
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1490
  case 0 => "zero"
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1491
  case 1 => "one"
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1492
  case 2 => "two"
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1493
  case _ => "many"
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1494
}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1495
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1496
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1497
\noindent It takes an integer as input argument and returns a
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1498
string. The type of the function generated in \code{mkfn} above, is
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1499
\code{Int => Boolean}.
277
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1500
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1501
Unfortunately, unlike other functional programming languages, there is
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1502
in Scala no easy way to find out the types of existing functions, except
acaf2099406a updated
Christian Urban <urbanc@in.tum.de>
parents: 275
diff changeset
  1503
by looking into the documentation
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1504
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1505
\begin{quote}
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
  1506
\url{https://dotty.epfl.ch/api/index.html}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1507
\end{quote}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1508
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1509
The function arrow can also be iterated, as in
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1510
\code{Int => String => Boolean}. This is the type for a function
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1511
taking an integer as first argument and a string as second,
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1512
and the result of the function is a boolean. Though silly, a
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1513
function of this type would be
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1514
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1515
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1516
\begin{lstlisting}[numbers=none]
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1517
def chk_string(n: Int)(s: String) : Boolean =
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1518
  mk_string(n) == s
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1519
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1520
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1521
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1522
\noindent which checks whether the integer \code{n}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1523
corresponds to the name \code{s} given by the function
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1524
\code{mk\_string}. Notice the unusual way of specifying the
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1525
arguments of this function: the arguments are given one after
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1526
the other, instead of being in a pair (what would be the type
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1527
of this function then?). This way of specifying the arguments
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1528
can be useful, for example in situations like this
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1529
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1530
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1531
scala> List("one", "two", "three", "many").map(chk_string(2))
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1532
res4 = List(false, true, false, false)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1533
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1534
scala> List("one", "two", "three", "many").map(chk_string(3))
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1535
res5 = List(false, false, false, true)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1536
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1537
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1538
\noindent In each case we can give to \code{map} a specialised
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1539
version of \code{chk_string}---once specialised to 2 and once
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1540
to 3. This kind of ``specialising'' a function is called
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1541
\emph{partial application}---we have not yet given to this
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1542
function all arguments it needs, but only some of them.
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1543
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1544
Coming back to the type \code{Int => String => Boolean}. The
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1545
rule about such function types is that the right-most type
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1546
specifies what the function returns (a boolean in this case).
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1547
The types before that specify how many arguments the function
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1548
expects and what their type is (in this case two arguments,
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1549
one of type \code{Int} and another of type \code{String}).
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1550
Given this rule, what kind of function has type
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1551
\mbox{\code{(Int => String) => Boolean}}? Well, it returns a
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1552
boolean. More interestingly, though, it only takes a single
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1553
argument (because of the parentheses). The single argument
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1554
happens to be another function (taking an integer as input and
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1555
returning a string). Remember that \code{mk_string} is just
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1556
such a function. So how can we use it? For this define
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1557
the somewhat silly function \code{apply_3}:
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1558
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1559
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1560
def apply_3(f: Int => String): Bool = f(3) == "many"
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1561
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1562
scala> apply_3(mk_string)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1563
res6 = true
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1564
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1565
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1566
You might ask: Apart from silly functions like above, what is
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1567
the point of having functions as input arguments to other
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1568
functions?
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1569
%In Java there is indeed no need of this kind of
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1570
%feature: at least in the past it did not allow such
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1571
%constructions. I think, the point of Java 8 and successors was to lift this
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1572
%restriction.
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1573
Well, in all functional programming languages,
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1574
including Scala, it is really essential to allow functions as
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1575
input argument. Above you have already seen \code{map} and
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1576
\code{foreach} which need this feature. Consider the functions
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1577
\code{print} and \code{println}, which both print out strings,
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1578
but the latter adds a line break. You can call \code{foreach}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1579
with either of them and thus changing how, for example, five
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1580
numbers are printed.
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1581
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1582
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1583
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1584
scala> (1 to 5).toList.foreach(print)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1585
12345
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1586
scala> (1 to 5).toList.foreach(println)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1587
1
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1588
2
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1589
3
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1590
4
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1591
5
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1592
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1593
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1594
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1595
\noindent This is actually one of the main design principles
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1596
in functional programming. You have generic functions like
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1597
\code{map} and \code{foreach} that can traverse data containers,
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1598
like lists or sets. They then take a function to specify what
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1599
should be done with each element during the traversal. This
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1600
requires that the generic traversal functions can cope with
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1601
any kind of function (not just functions that, for example,
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1602
take as input an integer and produce a string like above).
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1603
This means we cannot fix the type of the generic traversal
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1604
functions, but have to keep them
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
  1605
\emph{polymorphic}.\footnote{Another interesting topic about
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1606
types, but we omit it here for the sake of brevity.}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1607
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1608
There is one more type constructor that is rather special. It is
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1609
called \code{Unit}. Recall that \code{Boolean} has two values, namely
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1610
\code{true} and \code{false}. This can be used, for example, to test
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1611
something and decide whether the test succeeds or not. In contrast the
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1612
type \code{Unit} has only a single value, written \code{()}. This
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1613
seems like a completely useless type and return value for a function,
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1614
but is actually quite useful. It indicates when the function does not
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1615
return any result. The purpose of these functions is to cause
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1616
something being written on the screen or written into a file, for
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1617
example. This is what is called they cause a \emph{side-effect}, for
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1618
example new content displayed on the screen or some new data in a
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1619
file. Scala uses the \code{Unit} type to indicate that a function does
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1620
not have a result, but potentially causes a side-effect. Typical
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1621
examples are the printing functions, like \code{print}.
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1622
301
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1623
c3b33c709696 updated
Christian Urban <urbanc@in.tum.de>
parents: 278
diff changeset
  1624
%%\subsection*{User-Defined Types}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1625
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1626
% \subsection*{Cool Stuff}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1627
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1628
% The first wow-moment I had with Scala was when I came across
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1629
% the following code-snippet for reading a web-page.
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1630
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1631
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1632
% \begin{lstlisting}[ numbers=none]
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1633
% import io.Source
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1634
% val url = """http://www.inf.kcl.ac.uk/staff/urbanc/"""
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1635
% Source.fromURL(url)("ISO-8859-1").take(10000).mkString
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1636
% \end{lstlisting}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1637
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1638
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1639
% \noindent These three lines return a string containing the
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1640
% HTML-code of my webpage. It actually already does something
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1641
% more sophisticated, namely only returns the first 10000
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1642
% characters of a webpage in case it is too large. Why is that
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1643
% code-snippet of any interest? Well, try implementing
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1644
% reading-from-a-webpage in Java. I also like the possibility of
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1645
% triple-quoting strings, which I have only seen in Scala so
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1646
% far. The idea behind this is that in such a string all
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1647
% characters are interpreted literally---there are no escaped
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1648
% characters, like \verb|\n| for newlines.
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1649
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1650
% My second wow-moment I had with a feature of Scala that other
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1651
% functional programming languages do not have. This feature is
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1652
% about implicit type conversions. If you have regular
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1653
% expressions and want to use them for language processing you
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1654
% often want to recognise keywords in a language, for example
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1655
% \code{for},{} \code{if},{} \code{yield} and so on. But the
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1656
% basic regular expression \code{CHAR} can only recognise a
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1657
% single character. In order to recognise a whole string, like
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1658
% \code{for}, you have to put many of those together using
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1659
% \code{SEQ}:
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1660
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1661
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1662
% \begin{lstlisting}[numbers=none]
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1663
% SEQ(CHAR('f'), SEQ(CHAR('o'), CHAR('r')))
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1664
% \end{lstlisting}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1665
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1666
% \noindent This gets quickly unreadable when the strings and
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1667
% regular expressions get more complicated. In other functional
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1668
% programming languages, you can explicitly write a conversion
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1669
% function that takes a string, say \dq{\pcode{for}}, and
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1670
% generates the regular expression above. But then your code is
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1671
% littered with such conversion functions.
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1672
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1673
% In Scala you can do better by ``hiding'' the conversion
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1674
% functions. The keyword for doing this is \code{implicit} and
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1675
% it needs a built-in library called
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1676
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1677
% \begin{lstlisting}[numbers=none]
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1678
% scala.language.implicitConversions
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1679
% \end{lstlisting}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1680
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1681
% \noindent
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1682
% Consider the code
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1683
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1684
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1685
% \begin{lstlisting}[language=Scala]
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1686
% import scala.language.implicitConversions
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1687
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1688
% def charlist2rexp(s: List[Char]) : Rexp = s match {
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1689
%   case Nil => EMPTY
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1690
%   case c::Nil => CHAR(c)
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1691
%   case c::s => SEQ(CHAR(c), charlist2rexp(s))
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1692
% }
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1693
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1694
% implicit def string2rexp(s: String) : Rexp =
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1695
%   charlist2rexp(s.toList)
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1696
% \end{lstlisting}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1697
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1698
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1699
% \noindent where the first seven lines implement a function
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1700
% that given a list of characters generates the corresponding
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1701
% regular expression. In Lines 9 and 10, this function is used
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1702
% for transforming a string into a regular expression. Since the
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1703
% \code{string2rexp}-function is declared as \code{implicit},
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1704
% the effect will be that whenever Scala expects a regular
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1705
% expression, but I only give it a string, it will automatically
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1706
% insert a call to the \code{string2rexp}-function. I can now
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1707
% write for example
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1708
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1709
% \begin{lstlisting}[numbers=none]
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1710
% scala> ALT("ab", "ac")
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1711
% res9 = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c)))
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1712
% \end{lstlisting}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1713
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1714
% \noindent Recall that \code{ALT} expects two regular
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1715
% expressions as arguments, but I only supply two strings. The
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1716
% implicit conversion function will transform the string into a
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1717
% regular expression.
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1718
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1719
% Using implicit definitions, Scala allows me to introduce
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1720
% some further syntactic sugar for regular expressions:
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1721
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1722
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1723
% \begin{lstlisting}[ numbers=none]
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1724
% implicit def RexpOps(r: Rexp) = new {
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1725
%   def | (s: Rexp) = ALT(r, s)
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1726
%   def ~ (s: Rexp) = SEQ(r, s)
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1727
%   def % = STAR(r)
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1728
% }
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1729
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1730
% implicit def stringOps(s: String) = new {
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1731
%   def | (r: Rexp) = ALT(s, r)
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1732
%   def | (r: String) = ALT(s, r)
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1733
%   def ~ (r: Rexp) = SEQ(s, r)
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1734
%   def ~ (r: String) = SEQ(s, r)
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1735
%   def % = STAR(s)
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1736
% }
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1737
% \end{lstlisting}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1738
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1739
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1740
% \noindent This might seem a bit overly complicated, but its effect is
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1741
% that I can now write regular expressions such as $ab + ac$
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1742
% simply as
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1743
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1744
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1745
% \begin{lstlisting}[numbers=none]
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1746
% scala> "ab" | "ac"
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1747
% res10 = ALT(SEQ(CHAR(a),CHAR(b)),SEQ(CHAR(a),CHAR(c)))
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1748
% \end{lstlisting}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1749
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1750
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1751
% \noindent I leave you to figure out what the other
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1752
% syntactic sugar in the code above stands for.
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1753
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1754
% One more useful feature of Scala is the ability to define
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1755
% functions with varying argument lists. This is a feature that
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1756
% is already present in old languages, like C, but seems to have
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1757
% been forgotten in the meantime---Java does not have it. In the
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1758
% context of regular expressions this feature comes in handy:
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1759
% Say you are fed up with writing many alternatives as
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1760
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1761
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1762
% \begin{lstlisting}[numbers=none]
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1763
% ALT(..., ALT(..., ALT(..., ...)))
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1764
% \end{lstlisting}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1765
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1766
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1767
% \noindent To make it difficult, you do not know how deep such
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1768
% alternatives are nested. So you need something flexible that
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1769
% can take as many alternatives as needed. In Scala one can
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1770
% achieve this by adding a \code{*} to the type of an argument.
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1771
% Consider the code
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1772
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1773
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1774
% \begin{lstlisting}[language=Scala]
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1775
% def Alts(rs: List[Rexp]) : Rexp = rs match {
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1776
%   case Nil => NULL
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1777
%   case r::Nil => r
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1778
%   case r::rs => ALT(r, Alts(rs))
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1779
% }
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1780
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1781
% def ALTS(rs: Rexp*) = Alts(rs.toList)
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1782
% \end{lstlisting}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1783
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1784
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1785
% \noindent The function in Lines 1 to 5 takes a list of regular
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1786
% expressions and converts it into an appropriate alternative
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1787
% regular expression. In Line 7 there is a wrapper for this
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1788
% function which uses the feature of varying argument lists. The
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1789
% effect of this code  is that I can write the regular
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1790
% expression for keywords as
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1791
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1792
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1793
% \begin{lstlisting}[numbers=none]
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1794
% ALTS("for", "def", "yield", "implicit", "if", "match", "case")
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1795
% \end{lstlisting}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1796
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1797
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1798
% \noindent Again I leave it to you to find out how much this
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1799
% simplifies the regular expression in comparison with if I had
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1800
% to write this by hand using only the ``plain'' regular
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1801
% expressions from the inductive datatype.
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
  1802
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1803
%\bigskip\noindent
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1804
%\textit{More TBD.}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1805
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1806
%\subsection*{Coursework}
181
31ba76ce016d updated
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
  1807
395
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1808
\begin{figure}[p]
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1809
\begin{boxedminipage}{\textwidth}
395
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1810
\textbf{Scala Syntax for Java Developers}\bigskip
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1811
395
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1812
\noindent
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1813
Scala compiles to the JVM, like the Java language. Because of this,
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1814
it can re-use many libraries. Here are a few hints how some Java code
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1815
tranlsates to Scala code:\bigskip
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1816
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1817
\noindent
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1818
Variable declaration:
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1819
\begin{lstlisting}[language=Java]
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1820
Drink coke = getCoke();/*!\annotation{Java}!*/
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1821
\end{lstlisting}
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1822
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1823
\begin{lstlisting}[language=Scala]
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1824
val coke : Drink = getCoke()/*!\annotation{Scala}!*/
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1825
\end{lstlisting}
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1826
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1827
\noindent
395
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1828
or even
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1829
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1830
\begin{lstlisting}[language=Scala]
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1831
val coke = getCoke()/*!\annotation{Scala}!*/
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1832
\end{lstlisting}\bigskip
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1833
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1834
\noindent
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1835
Unit means void:
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1836
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1837
\begin{lstlisting}[language=Java]
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1838
public void output(String s) {/*!\annotation{Java}!*/
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1839
  System.out.println(s);
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1840
}
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1841
\end{lstlisting}
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1842
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1843
\begin{lstlisting}[language=Scala]
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1844
def output(s: String): Unit = println(s)/*!\annotation{Scala}!*/
395
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1845
\end{lstlisting}\bigskip
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1846
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1847
\noindent
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1848
Compound types, say the type for list of Strings:
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1849
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1850
\begin{lstlisting}[language=Java]
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1851
List<String>/*!\annotation{Java}!*/
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1852
\end{lstlisting}
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1853
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1854
\begin{lstlisting}[language=Scala]
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1855
List[String]/*!\annotation{Scala}!*/
395
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1856
\end{lstlisting}\bigskip
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1857
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1858
\noindent
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1859
String interpolations
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1860
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1861
\begin{lstlisting}[language=Java]
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1862
System.out.println("Hello, "+ first + " "+ last + "!");
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1863
/*!\annotation{Java}!*/
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1864
\end{lstlisting}
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1865
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1866
\begin{lstlisting}[language=Scala]
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1867
println(s"Hello, $first $last!")/*!\annotation{Scala}!*/
395
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1868
\end{lstlisting}\bigskip
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1869
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1870
\noindent
395
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1871
Java provides some syntactic sugar when constructing anonymous functions:
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1872
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1873
\begin{lstlisting}[language=Java]
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1874
list.foreach(item -> System.out.println("* " + item));
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1875
/*!\annotation{Java}!*/
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1876
\end{lstlisting}
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1877
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1878
\noindent
441
3bfe81972674 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 426
diff changeset
  1879
In Scala, we use the \code{=>} symbol for the same:
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1880
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1881
\begin{lstlisting}[language=Scala]
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1882
list.foreach(item => println(s"* $item"))/*!\annotation{Scala}!*/
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1883
\end{lstlisting}%$
395
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1884
\end{boxedminipage}
017f621f5835 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
  1885
\end{figure}
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1886
352
97bcf8efe4e0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 343
diff changeset
  1887
%%new / vs case classes
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
  1888
195
fc3ac7b70a06 updated
Christian Urban <urbanc@in.tum.de>
parents: 193
diff changeset
  1889
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1890
\subsection*{More Info}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1891
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1892
There is much more to Scala than I can possibly describe in
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1893
this short document and teach in the lectures. Fortunately there are a
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1894
number of free books
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1895
about Scala and of course lots of help online. For example
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1896
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1897
\begin{itemize}
400
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 395
diff changeset
  1898
%%\item \url{http://www.scala-lang.org/docu/files/ScalaByExample.pdf}
e48ea8300b2d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 395
diff changeset
  1899
%%\item \url{http://www.scala-lang.org/docu/files/ScalaTutorial.pdf}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1900
\item \url{https://www.youtube.com/user/ShadowofCatron}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1901
\item \url{http://docs.scala-lang.org/tutorials}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1902
\item \url{https://www.scala-exercises.org}
188
937c995b047a updated
Christian Urban <urbanc@in.tum.de>
parents: 187
diff changeset
  1903
\item \url{https://twitter.github.io/scala_school}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1904
\end{itemize}
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1905
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1906
\noindent There is also an online course at Coursera on Functional
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1907
Programming Principles in Scala by Martin Odersky, the main
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1908
developer of the Scala language. And a document that explains
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1909
Scala for Java programmers
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1910
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1911
\begin{itemize}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1912
\item \small\url{http://docs.scala-lang.org/tutorials/scala-for-java-programmers.html}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1913
\end{itemize}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1914
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1915
While I am quite enthusiastic about Scala, I am also happy to
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1916
admit that it has more than its fair share of faults.
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1917
%The
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1918
%problem seen earlier of having to give an explicit type to
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1919
%\code{toSet}, but not \code{toList} is one of them. There are
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1920
%also many ``deep'' ideas about types in Scala, which even to
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1921
%me as seasoned functional programmer are puzzling.
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1922
For example, whilst
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1923
implicits are great, they can also be a source of great
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1924
headaches, for example consider the code:
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1925
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1926
\begin{lstlisting}[numbers=none]
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1927
scala>  List (1, 2, 3) contains "your mom"
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1928
res1: Boolean = false
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1929
\end{lstlisting}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1930
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1931
\noindent Rather than returning \code{false}, this code should
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1932
throw a typing-error. There are also many limitations Scala
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1933
inherited from the JVM that can be really annoying. For
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1934
example a fixed stack size. One can work around this
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1935
particular limitation, but why does one have to?
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1936
More such `puzzles' can be found at
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1937
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1938
\begin{center}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1939
  \url{http://scalapuzzlers.com} and
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1940
  \url{http://latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1941
\end{center}
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1942
191
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
  1943
Even if Scala has been a success in several high-profile companies,
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
  1944
there is also a company (Yammer) that first used Scala in their
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
  1945
production code, but then moved away from it. Allegedly they did not
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
  1946
like the steep learning curve of Scala and also that new versions of
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
  1947
Scala often introduced incompatibilities in old code. Also the Java
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
  1948
language is lately developing at lightening speed (in comparison to
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
  1949
the past) taking on many features of Scala and other languages, and it
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
  1950
seems it even introduces new features on its own. So there is
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
  1951
seemingly even more incentive to stick with the old stuff you
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
  1952
know. Still, the goal of this part of PEP is to bend your mind about
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
  1953
what programming is\ldots{}namely functional programming. I promise
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
  1954
you, this will be useful no matter with which programming language you
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
  1955
will work.
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1956
333
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1957
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1958
Scala is deep: After many years, I still continue to learn new technique
470
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1959
for writing more elegant code.
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1960
%Unfortunately, I have not yet managed to
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1961
%switch over my code to Scala 3.0 due to time constraints.
86a456f8cb92 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 442
diff changeset
  1962
Scala 3 seems
441
3bfe81972674 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 426
diff changeset
  1963
to iron out a number of snags from Scala 2, but why on earth are they
442
c86e7dd198bf updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 441
diff changeset
  1964
introducing Python-esque indentation and why on earth are they
441
3bfe81972674 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 426
diff changeset
  1965
re-introducing the \texttt{then}-keyword in Scala 3, when I just about got
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1966
comfortable without it?
333
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1967
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1968
%So all in all, Scala might not be a great teaching language,
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1969
%but I hope this is mitigated by the fact that I never require
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1970
%you to write any Scala code. You only need to be able to read
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1971
%it. In the coursework you can use any programming language you
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1972
%like. If you want to use Scala for this, then be my guest; if
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1973
%you do not want, stick with the language you are most familiar
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 143
diff changeset
  1974
%with.
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1975
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1976
191
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
  1977
\subsection*{Conclusion}
f78b18c4c886 updated
Christian Urban <urbanc@in.tum.de>
parents: 190
diff changeset
  1978
483
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
  1979
I hope you liked the short journey through the Scala language---but
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
  1980
remember we like you to take on board the functional programming point
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
  1981
of view, rather than just learning another language: Immutable
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
  1982
functions are easier to trust, because they the same output on the
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
  1983
same input. For the same reason they are easier to test and debug.
1a51207780e6 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 481
diff changeset
  1984
There is an interesting blog article about Scala by a convert:
198
d59c7995bcb2 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1985
d59c7995bcb2 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1986
\begin{center}
d59c7995bcb2 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1987
\url{https://www.skedulo.com/tech-blog/technology-scala-programming/}
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  1988
\end{center}
198
d59c7995bcb2 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1989
d59c7995bcb2 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1990
\noindent
d59c7995bcb2 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1991
He makes pretty much the same arguments about functional programming and
d59c7995bcb2 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1992
immutability (one section is teasingly called \textit{``Where Did all
d59c7995bcb2 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1993
the Bugs Go?''}). If you happen to moan about all the idiotic features
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 470
diff changeset
  1994
of Scala (3), well, I guess this is part of the package according to this
198
d59c7995bcb2 updated handout
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
  1995
quote:\bigskip
197
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1996
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1997
%\begin{itemize}
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1998
%\item no exceptions....there two kinds, one ``global'' exceptions, like
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  1999
%out of memory (not much can be done about this by the ``individual''
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  2000
%programmer); and ``local one'' open a file that might not exists - in
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  2001
%the latter you do not want to use exceptions, but Options
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
  2002
%\end{itemize}
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2003
182
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  2004
\begin{flushright}\it
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  2005
There are only two kinds of languages: the ones people complain
182
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  2006
about\\ and the ones nobody uses.\smallskip\\
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  2007
\mbox{}\hfill\small{}---Bjarne Stroustrup (the inventor of C++)
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  2008
\end{flushright}
d3d912d7e17f updated
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
  2009
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2010
\end{document}
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2011
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  2012
%%% Local Variables:
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2013
%%% mode: latex
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2014
%%% TeX-master: t
492
4ffba2f72692 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 485
diff changeset
  2015
%%% End: