|     62  |     63  | 
|     63 \noindent |     64 \noindent | 
|     64 and should already come pre-installed in the Department (together with |     65 and should already come pre-installed in the Department (together with | 
|     65 the Scala compiler). VS Code is far from perfect, however it includes a |     66 the Scala compiler). VS Code is far from perfect, however it includes a | 
|     66 \textit{Marketplace} from which a multitude of extensions can be |     67 \textit{Marketplace} from which a multitude of extensions can be | 
|     67 downloaded that make editing and running Scala code easier (see |     68 downloaded that make editing and running Scala code a little easier (see | 
|     68 Figure~\ref{vscode} for my own setup). |     69 Figure~\ref{vscode} for my setup). | 
|     69  |     70  | 
|     70 \begin{figure}[t] |     71 \begin{figure}[t] | 
|         |     72 \begin{boxedminipage}{\textwidth}   | 
|     71 \begin{center}   |     73 \begin{center}   | 
|     72 \includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{} |     74 \includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{} | 
|     73 \end{center} |     75 \end{center} | 
|     74 \caption{My personal installation of VS Code includes the following |     76 \caption{My personal installation of VS Code includes the following | 
|     75 packages from Marketplace: Scala Syntax (official), Code Runner, Code |     77 packages from Marketplace: Scala Syntax (official), Code Runner, Code | 
|     76 Spell Checker, Rewrap and Subtle Match Brackets. I have also bound keys |     78 Spell Checker, Rewrap and Subtle Match Brackets. I have also bound keys | 
|     77 \keys{\^{}} \keys{Ret} to the action |     79 the \keys{Ctrl} \keys{Ret} to the action | 
|     78 ``Run-Selected-Text-In-Active-Terminal'' in order to quickly evaluate |     80 ``Run-Selected-Text-In-Active-Terminal'' in order to quickly evaluate | 
|     79 small code snippets in the Scala REPL.\label{vscode}} |     81 small code snippets in the Scala REPL.\label{vscode}} | 
|         |     82 \end{boxedminipage} | 
|     80 \end{figure}   |     83 \end{figure}   | 
|     81  |     84  | 
|     82 What I like most about VS Code is that it provides an easy access to the |     85 What I like most about VS Code is that it provides easy access to the | 
|     83 Scala REPL. But if you prefer your own editor for coding, it |     86 Scala REPL. But if you prefer your own editor for coding, it | 
|     84 is also painless to work with Scala completely on the command line (like you |     87 is also painless to work with Scala completely on the command line (like you | 
|     85 might have done with \texttt{g++} in the earlier part of PEP). For the |     88 might have done with \texttt{g++} in the earlier part of PEP). For the | 
|     86 lazybones among us, there is even an online editor and environment for |     89 lazybones among us, there is even an online editor and environment for | 
|     87 developing and running Scala programs called \textit{ScalaFiddle}, which |     90 developing and running Scala programs called \textit{ScalaFiddle}, which | 
|     88 requires zero setup (assuming you have a browser handy). You can access |     91 requires zero setup (assuming you have a browser handy). You can access | 
|     89 it from: |     92 it from:  | 
|     90   |     93   | 
|     91 \begin{quote} |     94 \begin{quote} | 
|     92 \url{https://scalafiddle.io} |     95 \url{https://scalafiddle.io}\medskip | 
|     93 \end{quote} |     96 \end{quote} | 
|     94    |     97    | 
|     95  |     98  | 
|     96 Scala can be used with the heavy-duty IDEs Eclipse and IntelliJ. |     99 Scala can be used with the heavy-duty IDEs Eclipse and IntelliJ. | 
|     97 A ready-made Scala bundle for Eclipse is available from |    100 A ready-made Scala bundle for Eclipse is available from | 
|    130 state by issuing some commands (e.g.~updating a field in an array, |    133 state by issuing some commands (e.g.~updating a field in an array, | 
|    131 adding one to a variable and so on). The classic example for this style |    134 adding one to a variable and so on). The classic example for this style | 
|    132 of programming is a \texttt{for}-loop, say |    135 of programming is a \texttt{for}-loop, say | 
|    133  |    136  | 
|    134 \begin{lstlisting}[language=C,numbers=none] |    137 \begin{lstlisting}[language=C,numbers=none] | 
|    135   for (int i = 10; i < 20; i++) {  |    138 for (int i = 10; i < 20; i++) {  | 
|    136       ...Do something interesting with i... |    139       ...Do something interesting with i... | 
|    137    } |    140 } | 
|    138 \end{lstlisting} |    141 \end{lstlisting} | 
|    139  |    142  | 
|    140 \noindent Here the variable \texttt{i} embodies the state, which is |    143 \noindent Here the integer variable \texttt{i} embodies the state, which | 
|    141 first set to \texttt{10} and then increased by one in each |    144 is first set to \texttt{10} and then increased by one in each | 
|    142 loop-iteration until it reaches \texttt{20} when the loop is exited. |    145 loop-iteration until it reaches \texttt{20} at which point the loop | 
|    143 When this code is compiled and actually runs, there will be some |    146 exits. When this code is compiled and actually runs, there will be some | 
|    144 dedicated space reserved in memory which contains the value of |    147 dedicated space reserved for \texttt{i} in memory which contains its | 
|    145 \texttt{i}\ldots\texttt{10} at the beginning, and then the content will |    148 current value\ldots\texttt{10} at the beginning, and then the content | 
|    146 be updated, or replaced, by some new content in every iteration.  The |    149 will be updated, or replaced, by some new content in every iteration. | 
|    147 main point here is that this kind of updating, or manipulating, memory  |    150 The main point here is that this kind of updating, or manipulating, | 
|    148 is \textbf{PURE EVIL}!! |    151 memory is \textbf{PURE EVIL}!! | 
|    149  |    152  | 
|    150 \noindent |    153 \noindent | 
|    151 \ldots{}Well, it is perfectly benign if you have a sequential program |    154 \ldots{}Well, it is perfectly benign if you have a sequential program | 
|    152 that gets run instruction by instruction...nicely one after another. |    155 that gets run instruction by instruction...nicely one after another. | 
|    153 This kind of running code uses a single core of your CPU and goes as |    156 This kind of running code uses a single core of your CPU and goes as | 
|    154 fast as your CPU frequency (or clock-speed) allows. Unfortunately, this |    157 fast as your CPU frequency, also called clock-speed, allows. The problem | 
|    155 clock-speed has not much increased over the past few years and no |    158 is that this clock-speed has not much increased over the past decade and | 
|    156 dramatic increases are predicted any time soon. So you are a bit stuck, |    159 no dramatic increases are predicted for any time soon. So you are a bit | 
|    157 unlike previous generations of developers who could rely upon the fact |    160 stuck, unlike previous generations of developers who could rely upon the | 
|    158 that every 2 years or so their code run twice as fast (in ideal |    161 fact that every 2 years or so their code would run twice as fast (in | 
|    159 circumstances) because the clock-speed of their CPUs got twice as fast. |    162 ideal circumstances) because the clock-speed of their CPUs got twice as | 
|    160 This unfortunately does not happen any more nowadays. To get you out of |    163 fast. This unfortunately does not happen any more nowadays. To get you | 
|    161 this embarrassing situation, CPU producers pile more and more cores into |    164 out of this dreadful situation, CPU producers pile more and more | 
|    162 CPUs in order to make them more powerful and potentially make software |    165 cores into CPUs in order to make them more powerful and potentially make | 
|    163 faster. The task for you as developer is to take somehow advantage of |    166 software faster. The task for you as developer is to take somehow | 
|    164 these cores by running as much of your code as possible in parallel on |    167 advantage of these cores by running as much of your code as possible in | 
|    165 as many core you have available (typically 4 in modern laptops and |    168 parallel on as many core you have available (typically 4 in modern | 
|    166 sometimes much more on high-end machines). In this situation, |    169 laptops and sometimes much more on high-end machines). In this | 
|    167 \textit{mutable} variables like \texttt{i} above are evil, or at least a |    170 situation, \textit{mutable} variables like \texttt{i} above are evil, or | 
|    168 major nuisance. Because if you want to distribute some of the |    171 at least a major nuisance: Because if you want to distribute some of the | 
|    169 loop-iterations over the cores that are currently idle in your system, |    172 loop-iterations over the cores that are currently idle in your system, | 
|    170 you need to be extremely careful about who can read and write (update) |    173 you need to be extremely careful about who can read and write (update) | 
|    171 the variable \texttt{i}.\footnote{If you are of the belief that nothing |    174 the variable \texttt{i}.\footnote{If you are of the belief that nothing | 
|    172 nasty can happen to \texttt{i} inside the \texttt{for}-loop, then you |    175 nasty can happen to \texttt{i} inside the \texttt{for}-loop, then you | 
|    173 need to go back over the C++ material.} Especially the writing operation |    176 need to go back over the C++ material.} Especially the writing operation | 
|    174 is critical because you do not want that conflicting writes mess about |    177 is critical because you do not want that conflicting writes mess about | 
|    175 with \texttt{i}. An untold amount of misery has arisen from this |    178 with \texttt{i}. Take my word: an untold amount of misery has arisen | 
|    176 problem. The catch is that if you try to solve this problem in C++ or |    179 from this problem. The catch is that if you try to solve this problem in | 
|    177 Java, and be as defensive as possible about reads and writes to |    180 C++ or Java, and be as defensive as possible about reads and writes to | 
|    178 \texttt{i}, then you need to synchronise access to it and as a result |    181 \texttt{i}, then you need to synchronise access to it. The result is that | 
|    179 your program more often than not waits more than it runs, thereby |    182 your program more often than not waits more than it runs, thereby | 
|    180 defeating the point of trying to run the program in parallel in the |    183 defeating the point of trying to run the program in parallel in the | 
|    181 first place. If you are less defensive, then usually all hell breaks |    184 first place. If you are less defensive, then usually all hell breaks | 
|    182 loose by seemingly obtaining random results. And forget the idea of |    185 loose by seemingly obtaining random results. And forget the idea of | 
|    183 being able to debug such code. |    186 being able to debug such code. | 
|    184  |    187  | 
|    185 The idea of functional programming is to eliminate any state from |    188 The central idea of functional programming is to eliminate any state | 
|    186 programs. Because then it is easy to parallelize the resulting programs: |    189 from programs---or at least from the ``interesting'' (computational | 
|    187 if you do not have any state, then once created all memory content stays |    190 intensive) parts. Because then it is easy to parallelize the resulting | 
|    188 unchanged and reads to such memory are absolutely safe without the need |    191 programs: if you do not have any state, then once created, all memory | 
|    189 of any synchronisations. An example is given in Figure~\ref{mand} where |    192 content stays unchanged and reads to such memory are absolutely safe | 
|    190 in the absence of annoying state, Scala makes it easy to calculate the |    193 without the need of any synchronisation. An example is given in | 
|    191 Mandelbrot set on as many cores of your CPU as possible. Why is it so |    194 Figure~\ref{mand} where in the absence of the annoying state, Scala | 
|    192 easy in this example? Because each pixel in the Mandelbrot set can be |    195 makes it very easy to calculate the Mandelbrot set on as many cores of | 
|    193 calculated independently and the calculation does not need to update any |    196 your CPU as possible. Why is it so easy in this example? Because each | 
|    194 variable. It is so easy in fact, that going from the sequential version |    197 pixel in the Mandelbrot set can be calculated independently and the | 
|    195 of the program to the parallel version can be done by adding just eight |    198 calculation does not need to update any variable. It is so easy in fact | 
|    196 characters. What is not to be liked about that (try the same in C++)? |    199 that going from the sequential version of the Mandelbrot program to the | 
|         |    200 parallel version can be achieved by adding just eight characters.  | 
|         |    201 Try the same in C++ or Java! | 
|    197  |    202  | 
|    198 \begin{figure}[p] |    203 \begin{figure}[p] | 
|    199   \includegraphics[scale=0.15]{../pics/mand1.png} |    204 \begin{boxedminipage}{\textwidth} | 
|    200  |    205 \begin{center}     | 
|    201   \includegraphics[scale=0.15]{../pics/mand4.png} |    206 \begin{tabular}{c}   | 
|    202   \includegraphics[scale=0.15]{../pics/mand3.png} |    207 \includegraphics[scale=0.15]{../pics/mand1.png}\\ | 
|    203 \caption{\label{mand}} |    208 \end{tabular} | 
|         |    209  | 
|         |    210 Wellknown Mandelbrot program for generating pretty pictures due to | 
|         |    211 Benoit Mandelbrot. (\url{https://en.wikipedia.org/wiki/Mandelbrot_set}) | 
|         |    212 \bigskip | 
|         |    213  | 
|         |    214  | 
|         |    215 \begin{tabular}[t]{p{5cm}|p{5cm}} | 
|         |    216   \includegraphics[scale=0.15]{../pics/mand4.png} & | 
|         |    217   \includegraphics[scale=0.15]{../pics/mand3.png} \\ | 
|         |    218 \begin{minipage}{0.5\textwidth}\small  | 
|         |    219 a | 
|         |    220 \begin{lstlisting}[numbers=none] | 
|         |    221 ww | 
|         |    222 \end{lstlisting} | 
|         |    223 \end{minipage} | 
|         |    224 & \\ | 
|         |    225 \end{tabular} | 
|         |    226 \end{center} | 
|         |    227 \caption{Test \label{mand}} | 
|         |    228 \end{boxedminipage} | 
|    204 \end{figure}   |    229 \end{figure}   | 
|    205  |    230  | 
|    206 But remember that this easy parallelisation of code requires that we |    231 But remember that this easy parallelisation of code requires that we | 
|    207 have no state in our program\ldots{} that is no counters like\texttt{i} |    232 have no state in our program\ldots{} that is no counters like \texttt{i} | 
|    208 in \texttt{for}-loops. You might then ask, how do I write loops without |    233 in \texttt{for}-loops. You might then ask, how do I write loops without | 
|    209 such counters? Well, teaching you that this is possible is the main |    234 such counters? Well, teaching you that this is possible is one of the main | 
|    210 point of the Scala-part in PEP. I can assure you it is possible, but you |    235 points of the Scala-part in PEP. I can assure you it is possible, but you | 
|    211 have to get your head around it. Once you mastered this, it will be fun |    236 have to get your head around it. Once you mastered this, it will be fun | 
|    212 to have no state in your programs (a side product is that it much easier |    237 to have no state in your programs (a side product is that it much easier | 
|    213 to debug state-less code; and the memory we might waste by not allowing |    238 to debug state-less code and also more often than not easier to understand). | 
|    214 in-place updates is taken care of by the memory garbage collector of |    239 So good luck with Scala! | 
|    215 Java and Scala). |         | 
|    216  |         | 
|    217  |    240  | 
|    218  |    241  | 
|    219 \subsection*{The Very Basics} |    242 \subsection*{The Very Basics} | 
|    220  |    243  | 
|    221 One advantage of Scala over Java is that it includes an interpreter (a |    244 One advantage of Scala over Java is that it includes an interpreter (a |