# HG changeset patch # User Christian Urban # Date 1530712085 -3600 # Node ID 937c995b047adae954f4153f17df0f2780f4747a # Parent 4d300409f2fee085a5360366663ce2272694734c updated diff -r 4d300409f2fe -r 937c995b047a handouts/pep-ho.pdf Binary file handouts/pep-ho.pdf has changed diff -r 4d300409f2fe -r 937c995b047a handouts/pep-ho.tex --- a/handouts/pep-ho.tex Tue Jun 26 01:49:32 2018 +0100 +++ b/handouts/pep-ho.tex Wed Jul 04 14:48:05 2018 +0100 @@ -34,12 +34,13 @@ 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{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 useful in -job-interviews (especially in data science) according to this anecdotal -report +\url{https://www.slideshare.net/maximnovak/joy-of-scala}} + +A number of companies---the Guardian, Twitter, Coursera, FourSquare, Netflix, +LinkedIn to name a few---either use Scala exclusively in +production code, or at least to some substantial degree. Scala seems +also useful in job-interviews (especially in data science) according to +this anecdotal report \begin{quote} \url{http://techcrunch.com/2016/06/14/scala-is-the-new-golden-child} @@ -122,9 +123,9 @@ 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. Still you need to learn Scala well enough to get good grades +for that. Still, you need to learn Scala well enough to get good marks in PEP, but functional programming could equally be taught with Haskell, -F\#, SML, Ocaml, Kotlin, Scheme, Elm and many other functional +F\#, SML, Ocaml, Kotlin, Clojure, Scheme, Elm and many other functional programming languages. %Your friendly lecturer just happens to like Scala %and the Department agreed that it is a good idea to inflict Scala upon %you. @@ -151,11 +152,11 @@ loop-iteration until it reaches \texttt{20} at which point the loop exits. When this code is compiled and actually runs, there will be some dedicated space reserved for \texttt{i} in memory. This space of -typically 32 bits contains its current value\ldots\texttt{10} at the -beginning, and then the content will be updated by, or overwritten with, -some new content in every iteration. The main point here is that this -kind of updating, or manipulating, memory is 25.806\ldots or \textbf{THE -ROOT OF ALL EVIL}!! +typically 32 bits contains \texttt{i}'s current value\ldots\texttt{10} +at the beginning, and then the content will be updated by, or +overwritten with, some new content in every iteration. The main point +here is that this kind of updating, or manipulating, memory is +25.806\ldots or \textbf{THE ROOT OF ALL EVIL}!! \begin{center} \includegraphics[scale=0.25]{../pics/root-of-all-evil.png} @@ -215,8 +216,9 @@ \begin{figure}[p] \begin{boxedminipage}{\textwidth} -A Scala programm for generating pretty pictures of the Mandelbrot Set -(\url{https://en.wikipedia.org/wiki/Mandelbrot_set}): +A Scala program for generating pretty pictures of the Mandelbrot set +(\url{https://en.wikipedia.org/wiki/Mandelbrot_set}, +\url{https://www.youtube.com/watch?v=aSg2Db3jF_4}): \begin{center} \begin{tabular}{c} \includegraphics[scale=0.12]{../pics/mand1.png} @@ -225,7 +227,7 @@ \begin{center} \begin{tabular}{@{}p{0.45\textwidth}|p{0.45\textwidth}@{}} - \bf sequential version: & \bf parallel version: (on 4 cores)\smallskip\\ + \bf sequential version: & \bf parallel version (on 4 cores):\smallskip\\ {\hfill\includegraphics[scale=0.12]{../pics/mand4.png}\hfill} & {\hfill\includegraphics[scale=0.12]{../pics/mand3.png}\hfill} \\ @@ -248,8 +250,8 @@ \end{lstlisting}} & {\footnotesize\begin{lstlisting}[xleftmargin=0mm] -for (y <- (0 until H).par) { - for (x <- (0 until W).par) { +for (y <- (0 until H)/*@\keys{\texttt{.par}}@*/) { + for (x <- (0 until W)/*@\keys{\texttt{.par}}@*/) { val c = start + (x * d_x + y * d_y * i) @@ -265,30 +267,33 @@ \end{lstlisting}}\\ \centering\includegraphics[scale=0.5]{../pics/cpu2.png} & -\centering\includegraphics[scale=0.5]{../pics/cpu1.png}\\ - - +\centering\includegraphics[scale=0.5]{../pics/cpu1.png} \end{tabular} \end{center} -\caption{Test \label{mand}} +\caption{The main ``loops'' creating the picture of the Mandelbrot set. The parallel version +only differs in \texttt{.par} added to the ``ranges'' of the x-y-coordinates. As can be +seen from the CPU loads: in the sequential versions there is a lower peak for an +extended period, while in the parallel version there is a short sharp burst for +essentially the same workload. +\label{mand}} \end{boxedminipage} \end{figure} -But remember that this easy parallelisation of code requires that we -have no state in our programs\ldots{} that is no counters like +But remember this easy parallelisation of code requires that we +have no state in our programs\ldots{}that is no counters like \texttt{i} in \texttt{for}-loops. You might then ask, how do I write loops without such counters? Well, teaching you that this is possible is one of the main points of the Scala-part in PEP. I can assure you it is possible, but you have to get your head around it. Once you have mastered this, it will be fun to have no state in your programs (a side product is that it much easier to debug state-less code and also more -often than not easier to understand). So good luck with +often than not easier to understand). So have fun with Scala!\footnote{If you are still not convinced about the function programming ``thing'', there are a few more arguments: a lot of research in programming languages happens to take place in functional programming languages. This has resulted in ultra-useful features such as pattern-matching, strong type-systems, lazyness, implicits, algebraic -datatypes to name a few. Imperative languages seem to always lag behind +datatypes to name a few. Imperative languages seem to often lag behind in adopting them: I know, for example, that Java will at some point in the future support pattern-matching, which has been used in SML for at least 40(!) years. See @@ -297,6 +302,7 @@ 2010 and is gaining quite some interest, borrows many ideas from functional programming from yesteryear.} + \subsection*{The Very Basics} One advantage of Scala over Java is that it includes an interpreter (a @@ -304,7 +310,7 @@ \underline{R}ead-\underline{E}val-\underline{P}rint-\underline{L}oop) with which you can run and test small code snippets without the need of a compiler. This helps a lot with interactively developing -programs. This is really my preferred way of writing small Scala +programs. It is my preferred way of writing small Scala programs. Once you installed Scala, you can start the interpreter by typing on the command line: @@ -326,10 +332,10 @@ res0: Int = 5 \end{lstlisting} -\noindent indicating that the result of the addition is of type +\noindent The answer means that he result of the addition is of type \code{Int} and the actual result is 5; \code{res0} is a name that Scala gives automatically to the result. You can reuse this name later -on. +on, for example \begin{lstlisting}[numbers=none] scala> res0 + 4 @@ -354,7 +360,7 @@ function that causes a side-effect, like \code{print}. We shall come back to this point later, but if you are curious now, the latter kind of functions always has \code{Unit} as -return type. It is just not printed. +return type. It is just not printed by Scala. You can try more examples with the Scala REPL, but feel free to first guess what the result is (not all answers by Scala are obvious): @@ -391,7 +397,7 @@ \end{lstlisting} \noindent save it in a file, for example {\tt hello-world.scala}, and -then run the compiler (\texttt{scalac}) and followed by the runtime +then run the compiler (\texttt{scalac}) and start the runtime environment (\texttt{scala}): \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] @@ -460,7 +466,8 @@ \noindent but try to guess what Scala will print out for \code{z}? Will it be \code{6} or \code{10}? A final word about values: Try to stick to the convention that names of values should be -lower case, like \code{x}, \code{y}, \code{foo41} and so on. +lower case, like \code{x}, \code{y}, \code{foo41} and so on. Upper-case +names you should reserve for what is called \emph{constructors}. \subsection*{Function Definitions} @@ -500,12 +507,15 @@ more complex, then it can be enclosed in braces, like above. If it it is just a simple expression, like \code{x + 1}, you can omit the braces. Very often functions are recursive (call themselves) like -the venerable factorial function. +the venerable factorial function: \begin{lstlisting}[numbers=none] def fact(n: Int): Int = if (n == 0) 1 else n * fact(n - 1) \end{lstlisting} + +\noindent +Note that Scala does not have a \code{then}-keyword in an \code{if}-statement. \subsection*{Loops, or better the Absence thereof} @@ -1086,8 +1096,9 @@ \item \url{https://www.youtube.com/user/ShadowofCatron} \item \url{http://docs.scala-lang.org/tutorials} \item \url{https://www.scala-exercises.org} +\item \url{https://twitter.github.io/scala_school} \end{itemize} - + \noindent There is also a course at Coursera on Functional Programming Principles in Scala by Martin Odersky, the main developer of the Scala language. And a document that explains