
diff -r 31ba76ce016d -r d3d912d7e17f handouts/pep-ho.tex
--- a/handouts/pep-ho.tex	Wed Jun 13 14:38:15 2018 +0100
+++ b/handouts/pep-ho.tex	Sat Jun 16 00:07:32 2018 +0100
@@ -14,8 +14,8 @@
 
 \section*{A Crash-Course in Scala}
 
-\mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{C}ompiled 
-\underline{A}cademic \underline{LA}nguage''}\smallskip\\
+\mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{c}ompiled 
+\underline{a}cademic \underline{la}nguage''}\smallskip\\
 \mbox{}\hfill\textit{ --- a joke read on Twitter}\bigskip
  
 \noindent
@@ -24,19 +24,19 @@
 attention in the last five or so years. One reason for this attention is
 that, like the Java programming language, Scala compiles to the Java
 Virtual Machine (JVM) and therefore Scala programs can run under MacOSX,
-Linux and Windows.\footnote{There are also experimental backends for
-producing code under Android (\url{http://scala-android.org}); and also
-for generating JavaScript code to build browser applications
+Linux and Windows.\footnote{There are also experimental backends of
+Scala for producing code under Android (\url{http://scala-android.org});
+and also for generating JavaScript code to build browser applications
 \url{(https://www.scala-js.org)}. Moreover there is work under way to
 have a native Scala compiler generating X86-code
-(\url{http://www.scala-native.org}).} It therefore has also access to
+(\url{http://www.scala-native.org}).} Because of this it has also access to
 the myriads of Java libraries. Unlike Java, however, Scala often allows
 programmers to write very concise and elegant code.  Some therefore say:
-``Scala is the better Java''.\footnote{form
-\url{https://www.slideshare.net/maximnovak/joy-of-scala}} Also a number
+``Scala is the better Java''.\footnote{from
+\url{https://www.slideshare.net/maximnovak/joy-of-scala}} A number
 of companies---the Guardian, Twitter, Coursera, FourSquare, LinkedIn,
 Netflix to name a few---either use Scala exclusively in production code,
-or at least to some substantial degree. Scala seems also to be useful in
+or at least to some substantial degree. Scala seems useful as well in
 job-interviews (especially in Data Science) according to this anecdotal
 report
 
@@ -62,36 +62,39 @@
 
 \noindent
 and should already come pre-installed in the Department (together with
-the Scala compiler). VS Code includes a \textit{Marketplace} from which 
-a multitude of extensions can be downloaded that make editing and 
-running Scala code easier (see Figure~\ref{vscode}).
+the Scala compiler). VS Code is far from perfect, however it includes a
+\textit{Marketplace} from which a multitude of extensions can be
+downloaded that make editing and running Scala code easier (see
+Figure~\ref{vscode} for my own setup).
 
 \begin{figure}[t]
 \begin{center}  
 \includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{}
 \end{center}
-\caption{My personal installation of VS Code includes the following packages
-from Marketplace: Scala Syntax (official), Code Runner, Code Spell Checker,
-Rewrap and Subtle Match Brackets. I have also bound keys \keys{\^{}} \keys{Ret}
-to the action ``Run-Selected-Text-In-Active-Terminal'' in order to 
-quickly evaluate small code snippets in the Scala REPL.\label{vscode}}
+\caption{My personal installation of VS Code includes the following
+packages from Marketplace: Scala Syntax (official), Code Runner, Code
+Spell Checker, Rewrap and Subtle Match Brackets. I have also bound keys
+\keys{\^{}} \keys{Ret} to the action
+``Run-Selected-Text-In-Active-Terminal'' in order to quickly evaluate
+small code snippets in the Scala REPL.\label{vscode}}
 \end{figure}  
 
 What I like most about VS Code is that it provides an easy access to the
 Scala REPL. But if you prefer your own editor for coding, it
-is also easy to work with Scala completely on the command line (like you
+is also painless to work with Scala completely on the command line (like you
 might have done with \texttt{g++} in the earlier part of PEP). For the
 lazybones among us, there is even an online editor and environment for
 developing and running Scala programs called \textit{ScalaFiddle}, which
-requires zero setup (assuming you have a browser handy)
+requires zero setup (assuming you have a browser handy). You can access
+it from:
  
 \begin{quote}
 \url{https://scalafiddle.io}
 \end{quote}
   
 
-Scala can also be used with the heavy-duty IDEs Eclipse and IntelliJ.
-A ready-made Scala bundle for Eclipse is available at
+Scala can be used with the heavy-duty IDEs Eclipse and IntelliJ too.
+A ready-made Scala bundle for Eclipse is available from
 
 \begin{quote}
 \url{http://scala-ide.org/download/sdk.html}
@@ -99,13 +102,110 @@
 
 \noindent
 Also IntelliJ includes plugins for Scala. \textbf{BUT}, I do not
-recommend the usage of Eclipse or IntelliJ for PEP: these IDEs seem to
-make your life harder, rather than easier, for the small programs we
-will write in this module. They are really meant to be used when you
-have a million-lines codebase, rather than our ``toy-programs''\ldots{}why 
-on earth am I required to create a completely new project with
-several subdirectories when I just want to try out 20-lines of Scala
-code? ;o)
+recommend the usage of either Eclipse or IntelliJ for PEP: these IDEs
+seem to make your life harder, rather than easier, for the small
+programs we will write in this module. They are really meant to be used
+when you have a million-lines codebase, rather than our
+``toy-programs''\ldots{}for example why on earth am I required to create a
+completely new project with several subdirectories when I just want to
+try out 20-lines of Scala code? ;o)
+
+\subsection*{Why Functional Programming?}
+
+Before we go on, let me explain a bit more why we want to inflict
+upon you another programming language. You hopefully have mastered Java and
+C++\ldots{}the world should be your oyster, no? Well, it is not that
+easy. We require Scala in PEP, but actually we do not deeply care
+whether you learn Scala---after all it is just a programming language
+(albeit a nifty one IMHO). What we do care about is that you learn about
+\textit{functional programming}. Scala is just the vehicle for that.
+
+Very likely writing programs in a functional programming language is
+quite different from what you are  used to in your study so far. It might
+even be totally alien to you. The reason is that functional programming
+seems to go against the core principles of \textit{imperative
+programming} (which is what you do in Java and C++ for example). The
+main idea of imperative programming  is that you have some form of
+``state'' in your program and you continuously change this state by
+issuing some commands. The classic example for this style of programming
+is a \texttt{for}-loop, say
+
+\begin{lstlisting}[language=C,numbers=none]
+  for (int i = 10; i < 20; i++) { 
+      ...Do something interesting with i...
+   }
+\end{lstlisting}
+
+\noindent Here the variable \texttt{i} embodies the state, which is
+first set to \texttt{10} and then increased by one in each
+loop-iteration until it reaches \texttt{20} when the loop is exited, and
+something else happens in the program. When this code actually runs
+there will be some memory cell reserved containing the value of
+\texttt{i}, say \texttt{10} at the beginning, and the content is then
+updated, or replaced, by some new content in every iteration.  The main
+point is that this kind of updating memory cells is \textbf{PURE EVIL}!!
+
+\noindent
+\ldots{}Well, it is perfectly benign if you have a sequential program
+that gets run instruction by instruction...nicely one after another.
+This kind of running code uses a single core of your CPU and goes as
+fast as your CPU frequency (or clock-speed) allows. Unfortunately, this
+clock-speed has not much increased in the past few years and no dramatic
+increases are predicted any time soon. So you are a bit stuck, unlike
+previous generations of developers who could rely upon the fact that
+every 2 years or so their code run twice as fast (in ideal
+circumstances) because the clock-speed their CPUs got twice as fast.
+This does not happen any more unfortunately. To get you out of this
+embarrassing situation, CPU producers pile more and more cores into CPUs
+in order to make them more powerful and potentially make software
+faster. The task for you as developer is to take somehow advantage of
+these cores by running as much of your code as possible in parallel on
+as many core you have available (typically 4 in modern laptops and
+sometimes much more on high-end machines). In this situation variables
+like \texttt{i} are evil, or at least a major nuisance. Because if you
+want to distribute some of the loop-iterations from the for-loop above
+over some of the cores that are currently idle in your system, you need
+to be extremely careful about who can read and write (update) the
+variable \texttt{i}.\footnote{If you are of the belief that nothing
+nasty can happen to \texttt{i} inside the loop, then you need to go back
+over the C++ material.} Especially the writing operation is critical
+because you do not want that it gets unintentionally overwritten. Untold
+number of problems have arisen from this problem. The catch is that if
+you try to be as defensive as possible about reads and writes to
+\texttt{i}, then you synchronise access to it and as a result your
+program waits more than it runs, thereby  defeating the point of trying
+to run the program in parallel in the first place. If you are less
+defensive, then usually all hell breaks loose by seemingly obtaining
+random results.
+
+The idea of functional programming is to eliminate any state from
+programs. Because of this it is easy to parallelize your program,
+because if you do not have any state, then once created all
+memory content stays unchanged and reads (and writes) to memory are 
+safe without the need of any synchronisations. An example is given 
+in Figure~\ref{mand} where Scala makes it easy to calculate the 
+Mandelbrot set on as many cores of your CPU as possible. Why is it
+so easy? Because each pixel in the Mandelbrot set can be calculated
+independently. Going from the sequential version of the 
+program to the parallel version takes exactly the addition of 8 
+characters. What is not to be liked about that?
+
+\begin{figure}[p]
+
+\caption{\label{Bla}}
+\end{figure}  
+
+But remember that this easy parallelisation requires that we have no
+state in our program\ldots{} no counters like\texttt{i} seen in the
+\texttt{for}-loop. You might then ask, how do I write loops without such
+counters? Well, teaching you that this is possible is the point of the
+functional programming language Scala in PEP. I can assure you it is
+possible and actually fun to have no state in your programs (a side
+product is that it makes it easier to debug them; and the memory
+we might waste by not allowing in-place updates is taken care of by the
+memory garbage collector of Java and Scala).
+
+
 
 \subsection*{The Very Basics}
 
@@ -276,7 +376,7 @@
 \subsection*{Function Definitions}
 
 We do functional programming! So defining functions will be our main occupation.
-A function named, lets say, \code{f} taking a single argument of type 
+As an example, a function named \code{f} taking a single argument of type 
 \code{Int} can be defined in Scala as follows:
 
 \begin{lstlisting}[numbers=none]
@@ -952,6 +1052,12 @@
 
 
 
+\begin{flushright}\it
+There are only two kinds of languages: the ones people complain 
+about\\ and the ones nobody uses.\smallskip\\
+\mbox{}\hfill\small{}---Bjarne Stroustrup (the inventor of C++)
+\end{flushright}
+
 \end{document}
 
 %%% Local Variables: 




