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