handouts/pep-ho.tex
changeset 191 f78b18c4c886
parent 190 2f989951d147
child 193 ae307c3de4ee
equal deleted inserted replaced
190:2f989951d147 191:f78b18c4c886
     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}
    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.\footnote{Unlike \emph{Microsoft Visual Studio}, which
    75 obviously Windows.\footnote{Unlike \emph{Microsoft Visual Studio}---note
    60 is a heavy-duty, Windows-only IDE\ldots{}jeez, could they have not just
    76 the minuscule difference in the name---which is a heavy-duty,
    61 made up some different name for a complete different project? For the
    77 Windows-only IDE\ldots{}jeez, with all their money could they not come
    62 pedantic, Microsoft Visual Studio is an IDE, whereas Visual Studio Code
    78 up with a completely different name for a complete different project?
    63 is a source code editor. Anybody knows the what the difference is?} It
    79 For the pedantic, Microsoft Visual Studio is an IDE, whereas Visual
    64 can be downloaded for free from
    80 Studio Code is a source code editor. Anybody knows the what the
       
    81 difference is?} It can be downloaded for free from
    65 
    82 
    66 \begin{quote}
    83 \begin{quote}
    67 \url{https://code.visualstudio.com}
    84 \url{https://code.visualstudio.com}
    68 \end{quote}
    85 \end{quote}
    69 
    86 
   109 \begin{quote}
   126 \begin{quote}
   110 \url{http://scala-ide.org/download/sdk.html}
   127 \url{http://scala-ide.org/download/sdk.html}
   111 \end{quote}
   128 \end{quote}
   112 
   129 
   113 \noindent
   130 \noindent
   114 Also IntelliJ includes plugins for Scala. \textbf{BUT}, I do \textbf{not}
   131 Also IntelliJ includes plugins for Scala. \underline{\textbf{BUT}}, 
   115 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
   116 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
   117 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
   118 when you have a million-lines codebase, rather than our
   135 when you have a million-lines codebase, rather than our
   119 ``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
   120 completely new project with several subdirectories when I just want to
   137 completely new project with several subdirectories when I just want to
   140 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
   141 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
   142 programming seems to go against the core principles of
   159 programming seems to go against the core principles of
   143 \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++
   144 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
   145 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
   146 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
   147 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
   148 this style of programming are \texttt{for}-loops, for example
   165 example for this style of programming are \texttt{for}-loops, like
   149 
   166 
   150 \begin{lstlisting}[language=C,numbers=none]
   167 \begin{lstlisting}[language=C,numbers=none]
   151 for (int i = 10; i < 20; i++) { 
   168 for (int i = 10; i < 20; i++) { 
   152       //...Do something interesting with i...
   169       //...Do something interesting with i...
   153 }
   170 }
   157 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
   158 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
   159 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
   160 dedicated space reserved for \texttt{i} in memory. This space of
   177 dedicated space reserved for \texttt{i} in memory. This space of
   161 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}
   162 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
   163 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
   164 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
   165 25.806\ldots or \textbf{THE ROOT OF ALL EVIL}!!
   182 ALL EVIL}!!
   166 
   183 
   167 \begin{center}
   184 \begin{center}
   168 \includegraphics[scale=0.25]{../pics/root-of-all-evil.png}
   185 \includegraphics[scale=0.25]{../pics/root-of-all-evil.png}
   169 \end{center}  
   186 \end{center}  
   170 
   187 
   204 loose by seemingly obtaining random results. And forget the idea of
   221 loose by seemingly obtaining random results. And forget the idea of
   205 being able to debug such code.
   222 being able to debug such code.
   206 
   223 
   207 The central idea of functional programming is to eliminate any state
   224 The central idea of functional programming is to eliminate any state
   208 from programs---or at least from the ``interesting bits''. Because then
   225 from programs---or at least from the ``interesting bits''. Because then
   209 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
   210 state, then once created, all memory content stays unchanged and reads
   227 state, then once created, all memory content stays unchanged and reads
   211 to such memory are absolutely safe without the need of any
   228 to such memory are absolutely safe without the need of any
   212 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
   213 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
   214 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
   220 \texttt{.par}. Try the same in C++ or Java!
   237 \texttt{.par}. Try the same in C++ or Java!
   221 
   238 
   222 \begin{figure}[p]
   239 \begin{figure}[p]
   223 \begin{boxedminipage}{\textwidth}
   240 \begin{boxedminipage}{\textwidth}
   224 
   241 
   225 A Scala program for generating pretty pictures of the Mandelbrot set 
   242 A Scala program for generating pretty pictures of the Mandelbrot set.\smallskip\\ 
   226 (\url{https://en.wikipedia.org/wiki/Mandelbrot_set},
   243 (See \url{https://en.wikipedia.org/wiki/Mandelbrot_set} or\\
   227 \url{https://www.youtube.com/watch?v=aSg2Db3jF_4}):
   244 \phantom{(See }\url{https://www.youtube.com/watch?v=aSg2Db3jF_4}):
   228 \begin{center}    
   245 \begin{center}    
   229 \begin{tabular}{c}  
   246 \begin{tabular}{c}  
   230 \includegraphics[scale=0.12]{../pics/mand1.png}
   247 \includegraphics[scale=0.11]{../pics/mand1.png}\\[-8mm]\mbox{}
   231 \end{tabular}
   248 \end{tabular}
   232 \end{center}
   249 \end{center}
   233 
   250 
   234 \begin{center}
   251 \begin{center}
   235 \begin{tabular}{@{}p{0.45\textwidth}|p{0.45\textwidth}@{}}
   252 \begin{tabular}{@{}p{0.45\textwidth}|p{0.45\textwidth}@{}}
   236   \bf sequential version: & \bf parallel version (on 4 cores):\smallskip\\
   253   \bf sequential version: & \bf parallel version on 4 cores:\smallskip\\
   237 
   254 
   238   {\hfill\includegraphics[scale=0.12]{../pics/mand4.png}\hfill} &
   255   {\hfill\includegraphics[scale=0.11]{../pics/mand4.png}\hfill} &
   239   {\hfill\includegraphics[scale=0.12]{../pics/mand3.png}\hfill} \\
   256   {\hfill\includegraphics[scale=0.11]{../pics/mand3.png}\hfill} \\
   240 
   257 
   241 {\footnotesize\begin{lstlisting}[xleftmargin=-1mm]
   258 {\footnotesize\begin{lstlisting}[xleftmargin=-1mm]
   242 for (y <- (0 until H)) {
   259 for (y <- (0 until H)) {
   243   for (x <- (0 until W)) {
   260   for (x <- (0 until W)) {
   244     
   261     
   245     val c = start + 
   262     val c = start + 
   246       (x * d_x + y * d_y * i)
   263       (x * d_x + y * d_y * i)
   247     val iters = iterations(c, max) 
   264     val iters = iterations(c, max) 
   248     val col = 
   265     val colour = 
   249       if (iters == max) black 
   266       if (iters == max) black 
   250       else colours(iters % 16)
   267       else colours(iters % 16)
   251 
   268 
   252     pixel(x, y, col)
   269     pixel(x, y, colour)
   253   }
   270   }
   254   viewer.updateUI()
   271   viewer.updateUI()
   255 }   
   272 }   
   256 \end{lstlisting}}   
   273 \end{lstlisting}}   
   257 & 
   274 & 
   260   for (x <- (0 until W)/*@\keys{\texttt{.par}}@*/) {
   277   for (x <- (0 until W)/*@\keys{\texttt{.par}}@*/) {
   261       
   278       
   262     val c = start + 
   279     val c = start + 
   263       (x * d_x + y * d_y * i)
   280       (x * d_x + y * d_y * i)
   264     val iters = iterations(c, max) 
   281     val iters = iterations(c, max) 
   265     val col = 
   282     val colour = 
   266       if (iters == max) black 
   283       if (iters == max) black 
   267       else colours(iters % 16)
   284       else colours(iters % 16)
   268   
   285   
   269     pixel(x, y, col)
   286     pixel(x, y, colour)
   270   }
   287   }
   271   viewer.updateUI()
   288   viewer.updateUI()
   272 }   
   289 }   
   273 \end{lstlisting}}\\
   290 \end{lstlisting}}\\[-2mm]
   274 
   291 
   275 \centering\includegraphics[scale=0.5]{../pics/cpu2.png} &
   292 \centering\includegraphics[scale=0.5]{../pics/cpu2.png} &
   276 \centering\includegraphics[scale=0.5]{../pics/cpu1.png}
   293 \centering\includegraphics[scale=0.5]{../pics/cpu1.png}
   277 \end{tabular}
   294 \end{tabular}
   278 \end{center}
   295 \end{center}
   279 \caption{The main ``loops'' creating the picture of the Mandelbrot set. The parallel version
   296 \caption{The ``main'' loops in the Mandelbrot program.
   280 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
   281 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
   282 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,
   283 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.
   284 \label{mand}} 
   304 \label{mand}} 
   285 \end{boxedminipage}
   305 \end{boxedminipage}
   286 \end{figure}  
   306 \end{figure}  
   287 
   307 
   288 But remember this easy parallelisation of code requires that we
   308 But remember this easy parallelisation of code requires that we
   296 often than not easier to understand). So have fun with
   316 often than not easier to understand). So have fun with
   297 Scala!\footnote{If you are still not convinced about the function
   317 Scala!\footnote{If you are still not convinced about the function
   298 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
   299 in programming languages happens to take place in functional programming
   319 in programming languages happens to take place in functional programming
   300 languages. This has resulted in ultra-useful features such as
   320 languages. This has resulted in ultra-useful features such as
   301 pattern-matching, strong type-systems, lazyness, implicits, algebraic
   321 pattern-matching, strong type-systems, laziness, implicits, algebraic
   302 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
   303 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
   304 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
   305 least 40(!) years. See
   326 least 40(!) years. See
   306 \url{http://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html}.
   327 \url{http://cr.openjdk.java.net/~briangoetz/amber/pattern-match.html}.
   307 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
   308 2010 and is gaining quite some interest, borrows many ideas from
   329 2010 and is gaining quite some interest, borrows many ideas from
   309 functional programming from yesteryear.}
   330 functional programming from yesteryear.}
  1137 
  1158 
  1138 \begin{center}
  1159 \begin{center}
  1139   \url{http://scalapuzzlers.com} and
  1160   \url{http://scalapuzzlers.com} and
  1140   \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/}
  1141 \end{center}
  1162 \end{center}
  1142 
  1163      
  1143 Even if Scala has been a success in several high-profile
  1164 Even if Scala has been a success in several high-profile companies,
  1144 companies, there is also a company (Yammer) that first used
  1165 there is also a company (Yammer) that first used Scala in their
  1145 Scala in their production code, but then moved away from it.
  1166 production code, but then moved away from it. Allegedly they did not
  1146 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
  1147 and also that new versions of Scala often introduced
  1168 Scala often introduced incompatibilities in old code. Also the Java
  1148 incompatibilities in old code. In the past two months
  1169 language is lately developing at lightening speed taking on many
  1149 there have also been two forks of the Scala compiler.
  1170 features of Scala and other languages, and it seems even it introduces
  1150 It needs to be seen what the future brings for Scala.
  1171 new features on its own.
  1151 
  1172 
  1152 %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,
  1153 %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
  1154 %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
  1155 %it. In the coursework you can use any programming language you
  1176 %it. In the coursework you can use any programming language you
  1156 %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
  1157 %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
  1158 %with.
  1179 %with.
  1159 
  1180 
  1160 
  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}
  1161 
  1190 
  1162 \begin{flushright}\it
  1191 \begin{flushright}\it
  1163 There are only two kinds of languages: the ones people complain 
  1192 There are only two kinds of languages: the ones people complain 
  1164 about\\ and the ones nobody uses.\smallskip\\
  1193 about\\ and the ones nobody uses.\smallskip\\
  1165 \mbox{}\hfill\small{}---Bjarne Stroustrup (the inventor of C++)
  1194 \mbox{}\hfill\small{}---Bjarne Stroustrup (the inventor of C++)