12 \begin{document} |
12 \begin{document} |
13 \fnote{\copyright{} Christian Urban, King's College London, 2017, 2018} |
13 \fnote{\copyright{} Christian Urban, King's College London, 2017, 2018} |
14 |
14 |
15 \section*{A Crash-Course in Scala} |
15 \section*{A Crash-Course in Scala} |
16 |
16 |
17 \mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{C}ompiled |
17 \mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{c}ompiled |
18 \underline{A}cademic \underline{LA}nguage''}\smallskip\\ |
18 \underline{a}cademic \underline{la}nguage''}\smallskip\\ |
19 \mbox{}\hfill\textit{ --- a joke read on Twitter}\bigskip |
19 \mbox{}\hfill\textit{ --- a joke read on Twitter}\bigskip |
20 |
20 |
21 \noindent |
21 \noindent |
22 Scala is a programming language that combines functional and |
22 Scala is a programming language that combines functional and |
23 object-oriented programming-styles. It has received quite a bit of |
23 object-oriented programming-styles. It has received quite a bit of |
24 attention in the last five or so years. One reason for this attention is |
24 attention in the last five or so years. One reason for this attention is |
25 that, like the Java programming language, Scala compiles to the Java |
25 that, like the Java programming language, Scala compiles to the Java |
26 Virtual Machine (JVM) and therefore Scala programs can run under MacOSX, |
26 Virtual Machine (JVM) and therefore Scala programs can run under MacOSX, |
27 Linux and Windows.\footnote{There are also experimental backends for |
27 Linux and Windows.\footnote{There are also experimental backends of |
28 producing code under Android (\url{http://scala-android.org}); and also |
28 Scala for producing code under Android (\url{http://scala-android.org}); |
29 for generating JavaScript code to build browser applications |
29 and also for generating JavaScript code to build browser applications |
30 \url{(https://www.scala-js.org)}. Moreover there is work under way to |
30 \url{(https://www.scala-js.org)}. Moreover there is work under way to |
31 have a native Scala compiler generating X86-code |
31 have a native Scala compiler generating X86-code |
32 (\url{http://www.scala-native.org}).} It therefore has also access to |
32 (\url{http://www.scala-native.org}).} Because of this it has also access to |
33 the myriads of Java libraries. Unlike Java, however, Scala often allows |
33 the myriads of Java libraries. Unlike Java, however, Scala often allows |
34 programmers to write very concise and elegant code. Some therefore say: |
34 programmers to write very concise and elegant code. Some therefore say: |
35 ``Scala is the better Java''.\footnote{form |
35 ``Scala is the better Java''.\footnote{from |
36 \url{https://www.slideshare.net/maximnovak/joy-of-scala}} Also a number |
36 \url{https://www.slideshare.net/maximnovak/joy-of-scala}} A number |
37 of companies---the Guardian, Twitter, Coursera, FourSquare, LinkedIn, |
37 of companies---the Guardian, Twitter, Coursera, FourSquare, LinkedIn, |
38 Netflix to name a few---either use Scala exclusively in production code, |
38 Netflix to name a few---either use Scala exclusively in production code, |
39 or at least to some substantial degree. Scala seems also to be useful in |
39 or at least to some substantial degree. Scala seems useful as well in |
40 job-interviews (especially in Data Science) according to this anecdotal |
40 job-interviews (especially in Data Science) according to this anecdotal |
41 report |
41 report |
42 |
42 |
43 \begin{quote} |
43 \begin{quote} |
44 \url{http://techcrunch.com/2016/06/14/scala-is-the-new-golden-child} |
44 \url{http://techcrunch.com/2016/06/14/scala-is-the-new-golden-child} |
60 \url{https://code.visualstudio.com} |
60 \url{https://code.visualstudio.com} |
61 \end{quote} |
61 \end{quote} |
62 |
62 |
63 \noindent |
63 \noindent |
64 and should already come pre-installed in the Department (together with |
64 and should already come pre-installed in the Department (together with |
65 the Scala compiler). VS Code includes a \textit{Marketplace} from which |
65 the Scala compiler). VS Code is far from perfect, however it includes a |
66 a multitude of extensions can be downloaded that make editing and |
66 \textit{Marketplace} from which a multitude of extensions can be |
67 running Scala code easier (see Figure~\ref{vscode}). |
67 downloaded that make editing and running Scala code easier (see |
|
68 Figure~\ref{vscode} for my own setup). |
68 |
69 |
69 \begin{figure}[t] |
70 \begin{figure}[t] |
70 \begin{center} |
71 \begin{center} |
71 \includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{} |
72 \includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{} |
72 \end{center} |
73 \end{center} |
73 \caption{My personal installation of VS Code includes the following packages |
74 \caption{My personal installation of VS Code includes the following |
74 from Marketplace: Scala Syntax (official), Code Runner, Code Spell Checker, |
75 packages from Marketplace: Scala Syntax (official), Code Runner, Code |
75 Rewrap and Subtle Match Brackets. I have also bound keys \keys{\^{}} \keys{Ret} |
76 Spell Checker, Rewrap and Subtle Match Brackets. I have also bound keys |
76 to the action ``Run-Selected-Text-In-Active-Terminal'' in order to |
77 \keys{\^{}} \keys{Ret} to the action |
77 quickly evaluate small code snippets in the Scala REPL.\label{vscode}} |
78 ``Run-Selected-Text-In-Active-Terminal'' in order to quickly evaluate |
|
79 small code snippets in the Scala REPL.\label{vscode}} |
78 \end{figure} |
80 \end{figure} |
79 |
81 |
80 What I like most about VS Code is that it provides an easy access to the |
82 What I like most about VS Code is that it provides an easy access to the |
81 Scala REPL. But if you prefer your own editor for coding, it |
83 Scala REPL. But if you prefer your own editor for coding, it |
82 is also easy to work with Scala completely on the command line (like you |
84 is also painless to work with Scala completely on the command line (like you |
83 might have done with \texttt{g++} in the earlier part of PEP). For the |
85 might have done with \texttt{g++} in the earlier part of PEP). For the |
84 lazybones among us, there is even an online editor and environment for |
86 lazybones among us, there is even an online editor and environment for |
85 developing and running Scala programs called \textit{ScalaFiddle}, which |
87 developing and running Scala programs called \textit{ScalaFiddle}, which |
86 requires zero setup (assuming you have a browser handy) |
88 requires zero setup (assuming you have a browser handy). You can access |
|
89 it from: |
87 |
90 |
88 \begin{quote} |
91 \begin{quote} |
89 \url{https://scalafiddle.io} |
92 \url{https://scalafiddle.io} |
90 \end{quote} |
93 \end{quote} |
91 |
94 |
92 |
95 |
93 Scala can also be used with the heavy-duty IDEs Eclipse and IntelliJ. |
96 Scala can be used with the heavy-duty IDEs Eclipse and IntelliJ too. |
94 A ready-made Scala bundle for Eclipse is available at |
97 A ready-made Scala bundle for Eclipse is available from |
95 |
98 |
96 \begin{quote} |
99 \begin{quote} |
97 \url{http://scala-ide.org/download/sdk.html} |
100 \url{http://scala-ide.org/download/sdk.html} |
98 \end{quote} |
101 \end{quote} |
99 |
102 |
100 \noindent |
103 \noindent |
101 Also IntelliJ includes plugins for Scala. \textbf{BUT}, I do not |
104 Also IntelliJ includes plugins for Scala. \textbf{BUT}, I do not |
102 recommend the usage of Eclipse or IntelliJ for PEP: these IDEs seem to |
105 recommend the usage of either Eclipse or IntelliJ for PEP: these IDEs |
103 make your life harder, rather than easier, for the small programs we |
106 seem to make your life harder, rather than easier, for the small |
104 will write in this module. They are really meant to be used when you |
107 programs we will write in this module. They are really meant to be used |
105 have a million-lines codebase, rather than our ``toy-programs''\ldots{}why |
108 when you have a million-lines codebase, rather than our |
106 on earth am I required to create a completely new project with |
109 ``toy-programs''\ldots{}for example why on earth am I required to create a |
107 several subdirectories when I just want to try out 20-lines of Scala |
110 completely new project with several subdirectories when I just want to |
108 code? ;o) |
111 try out 20-lines of Scala code? ;o) |
|
112 |
|
113 \subsection*{Why Functional Programming?} |
|
114 |
|
115 Before we go on, let me explain a bit more why we want to inflict |
|
116 upon you another programming language. You hopefully have mastered Java and |
|
117 C++\ldots{}the world should be your oyster, no? Well, it is not that |
|
118 easy. We require Scala in PEP, but actually we do not deeply care |
|
119 whether you learn Scala---after all it is just a programming language |
|
120 (albeit a nifty one IMHO). What we do care about is that you learn about |
|
121 \textit{functional programming}. Scala is just the vehicle for that. |
|
122 |
|
123 Very likely writing programs in a functional programming language is |
|
124 quite different from what you are used to in your study so far. It might |
|
125 even be totally alien to you. The reason is that functional programming |
|
126 seems to go against the core principles of \textit{imperative |
|
127 programming} (which is what you do in Java and C++ for example). The |
|
128 main idea of imperative programming is that you have some form of |
|
129 ``state'' in your program and you continuously change this state by |
|
130 issuing some commands. The classic example for this style of programming |
|
131 is a \texttt{for}-loop, say |
|
132 |
|
133 \begin{lstlisting}[language=C,numbers=none] |
|
134 for (int i = 10; i < 20; i++) { |
|
135 ...Do something interesting with i... |
|
136 } |
|
137 \end{lstlisting} |
|
138 |
|
139 \noindent Here the variable \texttt{i} embodies the state, which is |
|
140 first set to \texttt{10} and then increased by one in each |
|
141 loop-iteration until it reaches \texttt{20} when the loop is exited, and |
|
142 something else happens in the program. When this code actually runs |
|
143 there will be some memory cell reserved containing the value of |
|
144 \texttt{i}, say \texttt{10} at the beginning, and the content is then |
|
145 updated, or replaced, by some new content in every iteration. The main |
|
146 point is that this kind of updating memory cells is \textbf{PURE EVIL}!! |
|
147 |
|
148 \noindent |
|
149 \ldots{}Well, it is perfectly benign if you have a sequential program |
|
150 that gets run instruction by instruction...nicely one after another. |
|
151 This kind of running code uses a single core of your CPU and goes as |
|
152 fast as your CPU frequency (or clock-speed) allows. Unfortunately, this |
|
153 clock-speed has not much increased in the past few years and no dramatic |
|
154 increases are predicted any time soon. So you are a bit stuck, unlike |
|
155 previous generations of developers who could rely upon the fact that |
|
156 every 2 years or so their code run twice as fast (in ideal |
|
157 circumstances) because the clock-speed their CPUs got twice as fast. |
|
158 This does not happen any more unfortunately. To get you out of this |
|
159 embarrassing situation, CPU producers pile more and more cores into CPUs |
|
160 in order to make them more powerful and potentially make software |
|
161 faster. The task for you as developer is to take somehow advantage of |
|
162 these cores by running as much of your code as possible in parallel on |
|
163 as many core you have available (typically 4 in modern laptops and |
|
164 sometimes much more on high-end machines). In this situation variables |
|
165 like \texttt{i} are evil, or at least a major nuisance. Because if you |
|
166 want to distribute some of the loop-iterations from the for-loop above |
|
167 over some of the cores that are currently idle in your system, you need |
|
168 to be extremely careful about who can read and write (update) the |
|
169 variable \texttt{i}.\footnote{If you are of the belief that nothing |
|
170 nasty can happen to \texttt{i} inside the loop, then you need to go back |
|
171 over the C++ material.} Especially the writing operation is critical |
|
172 because you do not want that it gets unintentionally overwritten. Untold |
|
173 number of problems have arisen from this problem. The catch is that if |
|
174 you try to be as defensive as possible about reads and writes to |
|
175 \texttt{i}, then you synchronise access to it and as a result your |
|
176 program waits more than it runs, thereby defeating the point of trying |
|
177 to run the program in parallel in the first place. If you are less |
|
178 defensive, then usually all hell breaks loose by seemingly obtaining |
|
179 random results. |
|
180 |
|
181 The idea of functional programming is to eliminate any state from |
|
182 programs. Because of this it is easy to parallelize your program, |
|
183 because if you do not have any state, then once created all |
|
184 memory content stays unchanged and reads (and writes) to memory are |
|
185 safe without the need of any synchronisations. An example is given |
|
186 in Figure~\ref{mand} where Scala makes it easy to calculate the |
|
187 Mandelbrot set on as many cores of your CPU as possible. Why is it |
|
188 so easy? Because each pixel in the Mandelbrot set can be calculated |
|
189 independently. Going from the sequential version of the |
|
190 program to the parallel version takes exactly the addition of 8 |
|
191 characters. What is not to be liked about that? |
|
192 |
|
193 \begin{figure}[p] |
|
194 |
|
195 \caption{\label{Bla}} |
|
196 \end{figure} |
|
197 |
|
198 But remember that this easy parallelisation requires that we have no |
|
199 state in our program\ldots{} no counters like\texttt{i} seen in the |
|
200 \texttt{for}-loop. You might then ask, how do I write loops without such |
|
201 counters? Well, teaching you that this is possible is the point of the |
|
202 functional programming language Scala in PEP. I can assure you it is |
|
203 possible and actually fun to have no state in your programs (a side |
|
204 product is that it makes it easier to debug them; and the memory |
|
205 we might waste by not allowing in-place updates is taken care of by the |
|
206 memory garbage collector of Java and Scala). |
|
207 |
|
208 |
109 |
209 |
110 \subsection*{The Very Basics} |
210 \subsection*{The Very Basics} |
111 |
211 |
112 One advantage of Scala over Java is that it includes an interpreter (a |
212 One advantage of Scala over Java is that it includes an interpreter (a |
113 REPL, or |
213 REPL, or |