handouts/pep-ho.tex
changeset 193 ae307c3de4ee
parent 192 a112e0e2325c
parent 191 f78b18c4c886
child 195 fc3ac7b70a06
equal deleted inserted replaced
192:a112e0e2325c 193:ae307c3de4ee
     7 %cheat sheet
     7 %cheat sheet
     8 %http://worldline.github.io/scala-cheatsheet/
     8 %http://worldline.github.io/scala-cheatsheet/
     9 
     9 
    10 % case class, apply, unapply
    10 % case class, apply, unapply
    11 % see https://medium.com/@thejasbabu/scala-pattern-matching-9c9e73ba9a8a
    11 % see https://medium.com/@thejasbabu/scala-pattern-matching-9c9e73ba9a8a
       
    12 
       
    13 % the art of programming
       
    14 % https://www.youtube.com/watch?v=QdVFvsCWXrA
       
    15 
       
    16 % functional programming in Scala
       
    17 %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
       
    18 
       
    19 % functional programmming in C
       
    20 %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
       
    21 
       
    22 %speeding through haskell
       
    23 %https://openlibra.com/en/book/download/speeding-through-haskell
       
    24 
       
    25 % fp books --- ocaml
       
    26 % http://courses.cms.caltech.edu/cs134/cs134b/book.pdf
       
    27 % http://alexott.net/en/fp/books/
    12 
    28 
    13 \begin{document}
    29 \begin{document}
    14 \fnote{\copyright{} Christian Urban, King's College London, 2017, 2018}
    30 \fnote{\copyright{} Christian Urban, King's College London, 2017, 2018}
    15 
    31 
    16 \section*{A Crash-Course in Scala}
    32 \section*{A Crash-Course in Scala}
    34 the myriads of Java libraries. Unlike Java, however, Scala often allows
    50 the myriads of Java libraries. Unlike Java, however, Scala often allows
    35 programmers to write very concise and elegant code.  Some therefore say
    51 programmers to write very concise and elegant code.  Some therefore say
    36 ``Scala is the better Java''.\footnote{from
    52 ``Scala is the better Java''.\footnote{from
    37 \url{https://www.slideshare.net/maximnovak/joy-of-scala}} 
    53 \url{https://www.slideshare.net/maximnovak/joy-of-scala}} 
    38 
    54 
    39 A number of companies---the Guardian, Twitter, Coursera, FourSquare, Netflix,
    55 A number of companies---the Guardian, Twitter, Coursera, FourSquare,
    40 LinkedIn to name a few---either use Scala exclusively in
    56 Netflix, LinkedIn, ITV to name a few---either use Scala exclusively in
    41 production code, or at least to some substantial degree. Scala seems
    57 production code, or at least to some substantial degree. Scala seems
    42 also useful in job-interviews (especially in data science) according to
    58 also useful in job-interviews (especially in data science) according to
    43 this anecdotal report
    59 this anecdotal report
    44 
    60 
    45 \begin{quote}
    61 \begin{quote}
    49 \noindent
    65 \noindent
    50 The official Scala compiler can be downloaded from
    66 The official Scala compiler can be downloaded from
    51 
    67 
    52 \begin{quote}
    68 \begin{quote}
    53 \url{http://www.scala-lang.org}
    69 \url{http://www.scala-lang.org}
    54 \end{quote}
    70 \end{quote} 
    55 
    71 
    56 \noindent
    72 \noindent
    57 I found a convenient IDE for writing Scala programs is Microsoft's
    73 I found a convenient IDE for writing Scala programs is Microsoft's
    58 \textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and
    74 \textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and
    59 obviously Windows. It can be downloaded for free from
    75 obviously Windows.\footnote{Unlike \emph{Microsoft Visual Studio}---note
       
    76 the minuscule difference in the name---which is a heavy-duty,
       
    77 Windows-only IDE\ldots{}jeez, with all their money could they not come
       
    78 up with a completely different name for a complete different project?
       
    79 For the pedantic, Microsoft Visual Studio is an IDE, whereas Visual
       
    80 Studio Code is a source code editor. Anybody knows the what the
       
    81 difference is?} It can be downloaded for free from
    60 
    82 
    61 \begin{quote}
    83 \begin{quote}
    62 \url{https://code.visualstudio.com}
    84 \url{https://code.visualstudio.com}
    63 \end{quote}
    85 \end{quote}
    64 
    86 
    65 \noindent
    87 \noindent
    66 and should already come pre-installed in the Department (together with
    88 and should already come pre-installed in the Department (together with
    67 the Scala compiler). VS Code is far from perfect, however it includes a
    89 the Scala compiler). Being a project started in 2015, VS Code is
       
    90 relatively new and thus far from perfect. However it includes a
    68 \textit{Marketplace} from which a multitude of extensions can be
    91 \textit{Marketplace} from which a multitude of extensions can be
    69 downloaded that make editing and running Scala code a little easier (see
    92 downloaded that make editing and running Scala code a little easier (see
    70 Figure~\ref{vscode} for my setup).
    93 Figure~\ref{vscode} for my setup).
    71 
    94 
    72 \begin{figure}[t]
    95 \begin{figure}[t]
   103 \begin{quote}
   126 \begin{quote}
   104 \url{http://scala-ide.org/download/sdk.html}
   127 \url{http://scala-ide.org/download/sdk.html}
   105 \end{quote}
   128 \end{quote}
   106 
   129 
   107 \noindent
   130 \noindent
   108 Also IntelliJ includes plugins for Scala. \textbf{BUT}, I do \textbf{not}
   131 Also IntelliJ includes plugins for Scala. \underline{\textbf{BUT}}, 
   109 recommend the usage of either Eclipse or IntelliJ for PEP: these IDEs
   132 I do \textbf{not} recommend the usage of either Eclipse or IntelliJ for PEP: these IDEs
   110 seem to make your life harder, rather than easier, for the small
   133 seem to make your life harder, rather than easier, for the small
   111 programs we will write in this module. They are really meant to be used
   134 programs we will write in this module. They are really meant to be used
   112 when you have a million-lines codebase, rather than our
   135 when you have a million-lines codebase, rather than our
   113 ``toy-programs''\ldots{}for example why on earth am I required to create a
   136 ``toy-programs''\ldots{}for example why on earth am I required to create a
   114 completely new project with several subdirectories when I just want to
   137 completely new project with several subdirectories when I just want to
   134 quite different from what you are  used to in your study so far. It
   157 quite different from what you are  used to in your study so far. It
   135 might even be totally alien to you. The reason is that functional
   158 might even be totally alien to you. The reason is that functional
   136 programming seems to go against the core principles of
   159 programming seems to go against the core principles of
   137 \textit{imperative programming} (which is what you do in Java and C++
   160 \textit{imperative programming} (which is what you do in Java and C++
   138 for example). The main idea of imperative programming  is that you have
   161 for example). The main idea of imperative programming  is that you have
   139 some form of ``state'' in your program and you continuously change this
   162 some form of \emph{state} in your program and you continuously change this
   140 state by issuing some commands (e.g.~updating a field in an array or
   163 state by issuing some commands---for example for updating a field in an
   141 object, adding one to a variable and so on). The classic example for
   164 array or object, or for adding one to a variable and so on. The classic
   142 this style of programming are \texttt{for}-loops, for example
   165 example for this style of programming are \texttt{for}-loops, like
   143 
   166 
   144 \begin{lstlisting}[language=C,numbers=none]
   167 \begin{lstlisting}[language=C,numbers=none]
   145 for (int i = 10; i < 20; i++) { 
   168 for (int i = 10; i < 20; i++) { 
   146       //...Do something interesting with i...
   169       //...Do something interesting with i...
   147 }
   170 }
   151 is first set to \texttt{10} and then increased by one in each
   174 is first set to \texttt{10} and then increased by one in each
   152 loop-iteration until it reaches \texttt{20} at which point the loop
   175 loop-iteration until it reaches \texttt{20} at which point the loop
   153 exits. When this code is compiled and actually runs, there will be some
   176 exits. When this code is compiled and actually runs, there will be some
   154 dedicated space reserved for \texttt{i} in memory. This space of
   177 dedicated space reserved for \texttt{i} in memory. This space of
   155 typically 32 bits contains \texttt{i}'s current value\ldots\texttt{10}
   178 typically 32 bits contains \texttt{i}'s current value\ldots\texttt{10}
   156 at the beginning, and then the content will be updated by, or
   179 at the beginning, and then the content will be overwritten with some
   157 overwritten with, some new content in every iteration. The main point
   180 new content in every iteration. The main point here is that this kind of
   158 here is that this kind of updating, or manipulating, memory is
   181 updating, or manipulating, memory is 25.806\ldots or \textbf{THE ROOT OF
   159 25.806\ldots or \textbf{THE ROOT OF ALL EVIL}!!
   182 ALL EVIL}!!
   160 
   183 
   161 \begin{center}
   184 \begin{center}
   162 \includegraphics[scale=0.25]{../pics/root-of-all-evil.png}
   185 \includegraphics[scale=0.25]{../pics/root-of-all-evil.png}
   163 \end{center}  
   186 \end{center}  
   164 
   187 
   198 loose by seemingly obtaining random results. And forget the idea of
   221 loose by seemingly obtaining random results. And forget the idea of
   199 being able to debug such code.
   222 being able to debug such code.
   200 
   223 
   201 The central idea of functional programming is to eliminate any state
   224 The central idea of functional programming is to eliminate any state
   202 from programs---or at least from the ``interesting bits''. Because then
   225 from programs---or at least from the ``interesting bits''. Because then
   203 it is easy to parallelize the resulting programs: if you do not have any
   226 it is easy to parallelise the resulting programs: if you do not have any
   204 state, then once created, all memory content stays unchanged and reads
   227 state, then once created, all memory content stays unchanged and reads
   205 to such memory are absolutely safe without the need of any
   228 to such memory are absolutely safe without the need of any
   206 synchronisation. An example is given in Figure~\ref{mand} where in the
   229 synchronisation. An example is given in Figure~\ref{mand} where in the
   207 absence of the annoying state, Scala makes it very easy to calculate the
   230 absence of the annoying state, Scala makes it very easy to calculate the
   208 Mandelbrot set on as many cores of your CPU as possible. Why is it so
   231 Mandelbrot set on as many cores of your CPU as possible. Why is it so
   214 \texttt{.par}. Try the same in C++ or Java!
   237 \texttt{.par}. Try the same in C++ or Java!
   215 
   238 
   216 \begin{figure}[p]
   239 \begin{figure}[p]
   217 \begin{boxedminipage}{\textwidth}
   240 \begin{boxedminipage}{\textwidth}
   218 
   241 
   219 A Scala program for generating pretty pictures of the Mandelbrot set 
   242 A Scala program for generating pretty pictures of the Mandelbrot set.\smallskip\\ 
   220 (\url{https://en.wikipedia.org/wiki/Mandelbrot_set},
   243 (See \url{https://en.wikipedia.org/wiki/Mandelbrot_set} or\\
   221 \url{https://www.youtube.com/watch?v=aSg2Db3jF_4}):
   244 \phantom{(See }\url{https://www.youtube.com/watch?v=aSg2Db3jF_4}):
   222 \begin{center}    
   245 \begin{center}    
   223 \begin{tabular}{c}  
   246 \begin{tabular}{c}  
   224 \includegraphics[scale=0.12]{../pics/mand1.png}
   247 \includegraphics[scale=0.11]{../pics/mand1.png}\\[-8mm]\mbox{}
   225 \end{tabular}
   248 \end{tabular}
   226 \end{center}
   249 \end{center}
   227 
   250 
   228 \begin{center}
   251 \begin{center}
   229 \begin{tabular}{@{}p{0.45\textwidth}|p{0.45\textwidth}@{}}
   252 \begin{tabular}{@{}p{0.45\textwidth}|p{0.45\textwidth}@{}}
   230   \bf sequential version: & \bf parallel version (on 4 cores):\smallskip\\
   253   \bf sequential version: & \bf parallel version on 4 cores:\smallskip\\
   231 
   254 
   232   {\hfill\includegraphics[scale=0.12]{../pics/mand4.png}\hfill} &
   255   {\hfill\includegraphics[scale=0.11]{../pics/mand4.png}\hfill} &
   233   {\hfill\includegraphics[scale=0.12]{../pics/mand3.png}\hfill} \\
   256   {\hfill\includegraphics[scale=0.11]{../pics/mand3.png}\hfill} \\
   234 
   257 
   235 {\footnotesize\begin{lstlisting}[xleftmargin=-1mm]
   258 {\footnotesize\begin{lstlisting}[xleftmargin=-1mm]
   236 for (y <- (0 until H)) {
   259 for (y <- (0 until H)) {
   237   for (x <- (0 until W)) {
   260   for (x <- (0 until W)) {
   238     
   261     
   239     val c = start + 
   262     val c = start + 
   240       (x * d_x + y * d_y * i)
   263       (x * d_x + y * d_y * i)
   241     val iters = iterations(c, max) 
   264     val iters = iterations(c, max) 
   242     val col = 
   265     val colour = 
   243       if (iters == max) black 
   266       if (iters == max) black 
   244       else colours(iters % 16)
   267       else colours(iters % 16)
   245 
   268 
   246     pixel(x, y, col)
   269     pixel(x, y, colour)
   247   }
   270   }
   248   viewer.updateUI()
   271   viewer.updateUI()
   249 }   
   272 }   
   250 \end{lstlisting}}   
   273 \end{lstlisting}}   
   251 & 
   274 & 
   254   for (x <- (0 until W)/*@\keys{\texttt{.par}}@*/) {
   277   for (x <- (0 until W)/*@\keys{\texttt{.par}}@*/) {
   255       
   278       
   256     val c = start + 
   279     val c = start + 
   257       (x * d_x + y * d_y * i)
   280       (x * d_x + y * d_y * i)
   258     val iters = iterations(c, max) 
   281     val iters = iterations(c, max) 
   259     val col = 
   282     val colour = 
   260       if (iters == max) black 
   283       if (iters == max) black 
   261       else colours(iters % 16)
   284       else colours(iters % 16)
   262   
   285   
   263     pixel(x, y, col)
   286     pixel(x, y, colour)
   264   }
   287   }
   265   viewer.updateUI()
   288   viewer.updateUI()
   266 }   
   289 }   
   267 \end{lstlisting}}\\
   290 \end{lstlisting}}\\[-2mm]
   268 
   291 
   269 \centering\includegraphics[scale=0.5]{../pics/cpu2.png} &
   292 \centering\includegraphics[scale=0.5]{../pics/cpu2.png} &
   270 \centering\includegraphics[scale=0.5]{../pics/cpu1.png}
   293 \centering\includegraphics[scale=0.5]{../pics/cpu1.png}
   271 \end{tabular}
   294 \end{tabular}
   272 \end{center}
   295 \end{center}
   273 \caption{The main ``loops'' creating the picture of the Mandelbrot set. The parallel version
   296 \caption{The ``main'' loops in the Mandelbrot program.
   274 only differs in \texttt{.par} added to the ``ranges'' of the x-y-coordinates. As can be
   297 The parallel version differs only in \texttt{.par} being added to the
   275 seen from the CPU loads: in the sequential versions there is a lower peak for an
   298 ``ranges'' of the x-y-coordinates. As can be seen from the CPU loads, in
   276 extended period, while in the parallel version there is a short sharp burst for 
   299 the sequential versions there is a lower peak for an extended period,
   277 essentially the same workload.
   300 while in the parallel version there is a short sharp burst for
       
   301 essentially the same workload\ldots{}meaning you get more work done 
       
   302 in a shorter amount of time. This \emph{parallelisation} 
       
   303 only works reliably in an immutable program.
   278 \label{mand}} 
   304 \label{mand}} 
   279 \end{boxedminipage}
   305 \end{boxedminipage}
   280 \end{figure}  
   306 \end{figure}  
   281 
   307 
   282 But remember this easy parallelisation of code requires that we
   308 But remember this easy parallelisation of code requires that we
   290 often than not easier to understand). So have fun with
   316 often than not easier to understand). So have fun with
   291 Scala!\footnote{If you are still not convinced about the function
   317 Scala!\footnote{If you are still not convinced about the function
   292 programming ``thing'', there are a few more arguments: a lot of research
   318 programming ``thing'', there are a few more arguments: a lot of research
   293 in programming languages happens to take place in functional programming
   319 in programming languages happens to take place in functional programming
   294 languages. This has resulted in ultra-useful features such as
   320 languages. This has resulted in ultra-useful features such as
   295 pattern-matching, strong type-systems, lazyness, implicits, algebraic
   321 pattern-matching, strong type-systems, laziness, implicits, algebraic
   296 datatypes  to name a few. Imperative languages seem to often lag behind
   322 datatypes  to name a few. Imperative languages seem to often lag behind
   297 in adopting them: I know, for example, that Java will at some point in
   323 in adopting them: I know, for example, that Java will at some point in
   298 the future support pattern-matching, which has been used in SML for at
   324 the future support pattern-matching, which has been used for example 
       
   325 in SML for at
   299 least 40(!) years. See
   326 least 40(!) years. See
   300 \url{http://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html}.
   327 \url{http://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html}.
   301 Also Rust, a C-like programming language that has been developed since
   328 Also Rust, a C-like programming language that has been developed since
   302 2010 and is gaining quite some interest, borrows many ideas from
   329 2010 and is gaining quite some interest, borrows many ideas from
   303 functional programming from yesteryear.}
   330 functional programming from yesteryear.}
  1131 
  1158 
  1132 \begin{center}
  1159 \begin{center}
  1133   \url{http://scalapuzzlers.com} and
  1160   \url{http://scalapuzzlers.com} and
  1134   \url{http://latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/}
  1161   \url{http://latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/}
  1135 \end{center}
  1162 \end{center}
  1136 
  1163      
  1137 Even if Scala has been a success in several high-profile
  1164 Even if Scala has been a success in several high-profile companies,
  1138 companies, there is also a company (Yammer) that first used
  1165 there is also a company (Yammer) that first used Scala in their
  1139 Scala in their production code, but then moved away from it.
  1166 production code, but then moved away from it. Allegedly they did not
  1140 Allegedly they did not like the steep learning curve of Scala
  1167 like the steep learning curve of Scala and also that new versions of
  1141 and also that new versions of Scala often introduced
  1168 Scala often introduced incompatibilities in old code. Also the Java
  1142 incompatibilities in old code. In the past two months
  1169 language is lately developing at lightening speed taking on many
  1143 there have also been two forks of the Scala compiler.
  1170 features of Scala and other languages, and it seems even it introduces
  1144 It needs to be seen what the future brings for Scala.
  1171 new features on its own.
  1145 
  1172 
  1146 %So all in all, Scala might not be a great teaching language,
  1173 %So all in all, Scala might not be a great teaching language,
  1147 %but I hope this is mitigated by the fact that I never require
  1174 %but I hope this is mitigated by the fact that I never require
  1148 %you to write any Scala code. You only need to be able to read
  1175 %you to write any Scala code. You only need to be able to read
  1149 %it. In the coursework you can use any programming language you
  1176 %it. In the coursework you can use any programming language you
  1150 %like. If you want to use Scala for this, then be my guest; if
  1177 %like. If you want to use Scala for this, then be my guest; if
  1151 %you do not want, stick with the language you are most familiar
  1178 %you do not want, stick with the language you are most familiar
  1152 %with.
  1179 %with.
  1153 
  1180 
  1154 
  1181 
       
  1182 \subsection*{Conclusion}
       
  1183 
       
  1184 \begin{itemize}
       
  1185 \item no exceptions....there two kinds, one ``global'' exceptions, like
       
  1186 out of memory (not much can be done about this by the ``individual''
       
  1187 programmer); and ``local one'' open a file that might not exists - in
       
  1188 the latter you do not want to use exceptions, but Options
       
  1189 \end{itemize}
  1155 
  1190 
  1156 \begin{flushright}\it
  1191 \begin{flushright}\it
  1157 There are only two kinds of languages: the ones people complain 
  1192 There are only two kinds of languages: the ones people complain 
  1158 about\\ and the ones nobody uses.\smallskip\\
  1193 about\\ and the ones nobody uses.\smallskip\\
  1159 \mbox{}\hfill\small{}---Bjarne Stroustrup (the inventor of C++)
  1194 \mbox{}\hfill\small{}---Bjarne Stroustrup (the inventor of C++)