handouts/pep-ho.tex
changeset 184 84dc794928de
parent 183 f3767098552b
child 186 f211d9cb856e
equal deleted inserted replaced
183:f3767098552b 184:84dc794928de
     1 \documentclass{article}
     1 \documentclass{article}
     2 \usepackage{../style}
     2 \usepackage{../style}
     3 \usepackage{../langs}
     3 \usepackage{../langs}
     4 \usepackage{marvosym}
     4 \usepackage{marvosym}
       
     5 \usepackage{boxedminipage}
     5 
     6 
     6 %cheat sheet
     7 %cheat sheet
     7 %http://worldline.github.io/scala-cheatsheet/
     8 %http://worldline.github.io/scala-cheatsheet/
     8 
     9 
     9 % case class, apply, unapply
    10 % case class, apply, unapply
    14 
    15 
    15 \section*{A Crash-Course in Scala}
    16 \section*{A Crash-Course in Scala}
    16 
    17 
    17 \mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{c}ompiled 
    18 \mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{c}ompiled 
    18 \underline{a}cademic \underline{la}nguage''}\smallskip\\
    19 \underline{a}cademic \underline{la}nguage''}\smallskip\\
    19 \mbox{}\hfill\textit{ --- a joke on Twitter}\bigskip
    20 \mbox{}\hfill\textit{ --- a joke(?) on Twitter}\bigskip
    20  
    21  
    21 \noindent
    22 \noindent
    22 Scala is a programming language that combines functional and
    23 Scala is a programming language that combines functional and
    23 object-oriented programming-styles. It has received quite a bit of
    24 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
    25 attention in the last five or so years. One reason for this attention is
    35 ``Scala is the better Java''.\footnote{from
    36 ``Scala is the better Java''.\footnote{from
    36 \url{https://www.slideshare.net/maximnovak/joy-of-scala}} A number
    37 \url{https://www.slideshare.net/maximnovak/joy-of-scala}} A number
    37 of companies---the Guardian, Twitter, Coursera, FourSquare, LinkedIn,
    38 of companies---the Guardian, Twitter, Coursera, FourSquare, LinkedIn,
    38 Netflix to name a few---either use Scala exclusively in production code,
    39 Netflix to name a few---either use Scala exclusively in production code,
    39 or at least to some substantial degree. Scala seems also useful in
    40 or at least to some substantial degree. Scala seems also useful in
    40 job-interviews (especially in Data Science) according to this anecdotal
    41 job-interviews (especially in data science) according to this anecdotal
    41 report
    42 report
    42 
    43 
    43 \begin{quote}
    44 \begin{quote}
    44 \url{http://techcrunch.com/2016/06/14/scala-is-the-new-golden-child}
    45 \url{http://techcrunch.com/2016/06/14/scala-is-the-new-golden-child}
    45 \end{quote}
    46 \end{quote}
    50 \begin{quote}
    51 \begin{quote}
    51 \url{http://www.scala-lang.org}
    52 \url{http://www.scala-lang.org}
    52 \end{quote}
    53 \end{quote}
    53 
    54 
    54 \noindent
    55 \noindent
    55 I found a convenient IDE for Scala programming is Microsoft's
    56 I found a convenient IDE for writing Scala programs is Microsoft's
    56 \textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and
    57 \textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and
    57 obviously Windows. It can be downloaded for free from
    58 obviously Windows. It can be downloaded for free from
    58 
    59 
    59 \begin{quote}
    60 \begin{quote}
    60 \url{https://code.visualstudio.com}
    61 \url{https://code.visualstudio.com}
    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
   106 seem to make your life harder, rather than easier, for the small
   109 seem to make your life harder, rather than easier, for the small
   107 programs we will write in this module. They are really meant to be used
   110 programs we will write in this module. They are really meant to be used
   108 when you have a million-lines codebase, rather than our
   111 when you have a million-lines codebase, rather than our
   109 ``toy-programs''\ldots{}for example why on earth am I required to create a
   112 ``toy-programs''\ldots{}for example why on earth am I required to create a
   110 completely new project with several subdirectories when I just want to
   113 completely new project with several subdirectories when I just want to
   111 try out 20-lines of Scala code? ;o)
   114 try out 20-lines of Scala code? Your mileage may vary. ;o)
   112 
   115 
   113 \subsection*{Why Functional Programming?}
   116 \subsection*{Why Functional Programming?}
   114 
   117 
   115 Before we go on, let me explain a bit more why we want to inflict
   118 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
   119 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
   120 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
   121 easy. We do require Scala in PEP, but actually we do not deeply care
   119 whether you learn Scala---after all it is just a programming language
   122 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
   123 (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.
   124 \textit{functional programming}. Scala is just the vehicle for that.
   122 
   125 
   123 Very likely writing programs in a functional programming language is
   126 Very likely writing programs in a functional programming language is
   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