32 \section*{A Crash-Course in Scala} |
32 \section*{A Crash-Course in Scala} |
33 |
33 |
34 \mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{c}ompiled |
34 \mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{c}ompiled |
35 \underline{a}cademic \underline{la}nguage''}\smallskip\\ |
35 \underline{a}cademic \underline{la}nguage''}\smallskip\\ |
36 \mbox{}\hfill\textit{ --- a joke(?) found on Twitter}\bigskip |
36 \mbox{}\hfill\textit{ --- a joke(?) found on Twitter}\bigskip |
37 |
37 |
|
38 |
|
39 \subsection*{Introduction} |
|
40 |
38 \noindent |
41 \noindent |
39 Scala is a programming language that combines functional and |
42 Scala is a programming language that combines functional and |
40 object-oriented programming-styles. It has received quite a bit of |
43 object-oriented programming-styles. It has received quite a bit of |
41 attention in the last five or so years. One reason for this attention is |
44 attention in the last five or so years. One reason for this attention is |
42 that, like the Java programming language, Scala compiles to the Java |
45 that, like the Java programming language, Scala compiles to the Java |
43 Virtual Machine (JVM) and therefore Scala programs can run under MacOSX, |
46 Virtual Machine (JVM) and therefore Scala programs can run under MacOSX, |
44 Linux and Windows.\footnote{There are also experimental backends of |
47 Linux and Windows. Because of this it has also access to |
45 Scala for producing code under Android (\url{http://scala-android.org}); |
|
46 for generating JavaScript code to build browser applications |
|
47 \url{(https://www.scala-js.org)}; and there is work under way to |
|
48 have a native Scala compiler generating X86-code |
|
49 (\url{http://www.scala-native.org}).} Because of this it has also access to |
|
50 the myriads of Java libraries. Unlike Java, however, Scala often allows |
48 the myriads of Java libraries. Unlike Java, however, Scala often allows |
51 programmers to write very concise and elegant code. Some therefore say |
49 programmers to write very concise and elegant code. Some therefore say |
52 ``Scala is the better Java''.\footnote{from |
50 ``Scala is the better Java''.\footnote{from |
53 \url{https://www.slideshare.net/maximnovak/joy-of-scala}} |
51 \url{https://www.slideshare.net/maximnovak/joy-of-scala}} |
54 |
52 |
64 |
62 |
65 \noindent |
63 \noindent |
66 The official Scala compiler can be downloaded from |
64 The official Scala compiler can be downloaded from |
67 |
65 |
68 \begin{quote} |
66 \begin{quote} |
69 \url{http://www.scala-lang.org} |
67 \url{http://www.scala-lang.org}\medskip |
70 \end{quote} |
68 \end{quote} |
71 |
69 |
72 \noindent |
70 \noindent |
|
71 If you are interested there are also experimental backends of Scala |
|
72 for producing code under Android (\url{http://scala-android.org}); for |
|
73 generating JavaScript code (\url{https://www.scala-js.org}); and there |
|
74 is work under way to have a native Scala compiler generating X86-code |
|
75 (\url{http://www.scala-native.org}). Though be warned these backends |
|
76 are still rather beta or even alpha. |
|
77 |
|
78 \subsection*{VS Code and Scala} |
|
79 |
73 I found a convenient IDE for writing Scala programs is Microsoft's |
80 I found a convenient IDE for writing Scala programs is Microsoft's |
74 \textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and |
81 \textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and |
75 obviously Windows.\footnote{Unlike \emph{Microsoft Visual Studio}---note |
82 obviously Windows.\footnote{Unlike \emph{Microsoft Visual Studio}---note |
76 the minuscule difference in the name---which is a heavy-duty, |
83 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 |
84 Windows-only IDE\ldots{}jeez, with all their money could they not come |
84 \url{https://code.visualstudio.com} |
91 \url{https://code.visualstudio.com} |
85 \end{quote} |
92 \end{quote} |
86 |
93 |
87 \noindent |
94 \noindent |
88 and should already come pre-installed in the Department (together with |
95 and should already come pre-installed in the Department (together with |
89 the Scala compiler). Being a project started in 2015, VS Code is |
96 the Scala compiler). Being a project that just started in 2015, VS Code is |
90 relatively new and thus far from perfect. However it includes a |
97 relatively new and thus far from perfect. However it includes a |
91 \textit{Marketplace} from which a multitude of extensions can be |
98 \textit{Marketplace} from which a multitude of extensions can be |
92 downloaded that make editing and running Scala code a little easier (see |
99 downloaded that make editing and running Scala code a little easier (see |
93 Figure~\ref{vscode} for my setup). |
100 Figure~\ref{vscode} for my setup). |
94 |
101 |
95 \begin{figure}[t] |
102 \begin{figure}[t] |
96 \begin{boxedminipage}{\textwidth} |
103 \begin{boxedminipage}{\textwidth} |
97 \begin{center} |
104 \begin{center} |
98 \includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{} |
105 \includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{} |
99 \end{center} |
106 \end{center} |
100 \caption{My personal installation of VS Code includes the following |
107 \caption{My installation of VS Code includes the following |
101 packages from Marketplace: Scala Syntax (official), Code Runner, Code |
108 packages from Marketplace: \textbf{Scala Syntax (official)} 0.2.0, |
102 Spell Checker, Rewrap and Subtle Match Brackets. I have also bound |
109 \textbf{Code Runner} 0.9.5, \textbf{Code Spell Checker} 1.6.10, |
103 the keys \keys{Ctrl} \keys{Ret} to the action |
110 \textbf{Rewrap} 1.9.1 and \textbf{Subtle Match |
104 ``Run-Selected-Text-In-Active-Terminal'' in order to quickly evaluate |
111 Brackets} 3.0.0. I have also bound the keys \keys{Ctrl} \keys{Ret} to the |
105 small code snippets in the Scala REPL.\label{vscode}} |
112 action ``Run-Selected-Text-In-Active-Terminal'' in order to quickly |
|
113 evaluate small code snippets in the Scala REPL.\label{vscode}} |
106 \end{boxedminipage} |
114 \end{boxedminipage} |
107 \end{figure} |
115 \end{figure} |
108 |
116 |
109 What I like most about VS Code is that it provides easy access to the |
117 What I like most about VS Code is that it provides easy access to the |
110 Scala REPL. But if you prefer another editor for coding, it is also |
118 Scala REPL. But if you prefer another editor for coding, it is also |
111 painless to work with Scala completely on the command line (as you might |
119 painless to work with Scala completely on the command line (as you might |
112 have done with \texttt{g++} in the earlier part of PEP). For the |
120 have done with \texttt{g++} in the earlier part of PEP). For the |
113 lazybones among us, there is even an online editor and environment for |
121 lazybones among us, there are even online editors and environments for |
114 developing and running Scala programs called \textit{ScalaFiddle}, which |
122 developing and running Scala programs called \textit{ScalaFiddle} |
115 requires zero setup (assuming you have a browser handy). You can access |
123 and \textit{Scastie}, which |
116 it from: |
124 require zero setup (assuming you have a browser handy). You can access |
|
125 them from: |
117 |
126 |
118 \begin{quote} |
127 \begin{quote} |
119 \url{https://scalafiddle.io}\medskip |
128 \url{https://scalafiddle.io}\\ |
|
129 \url{https://scastie.scala-lang.org}\medskip |
120 \end{quote} |
130 \end{quote} |
121 |
131 |
122 |
132 \noindent |
123 Scala can be used with the heavy-duty IDEs Eclipse and IntelliJ. |
133 Scala can be used with the heavy-duty IDEs Eclipse and IntelliJ. |
124 A ready-made Scala bundle for Eclipse is available from |
134 A ready-made Scala bundle for Eclipse is available from |
125 |
135 |
126 \begin{quote} |
136 \begin{quote} |
127 \url{http://scala-ide.org/download/sdk.html} |
137 \url{http://scala-ide.org/download/sdk.html} |
220 first place. If you are less defensive, then usually all hell breaks |
230 first place. If you are less defensive, then usually all hell breaks |
221 loose by seemingly obtaining random results. And forget the idea of |
231 loose by seemingly obtaining random results. And forget the idea of |
222 being able to debug such code. |
232 being able to debug such code. |
223 |
233 |
224 The central idea of functional programming is to eliminate any state |
234 The central idea of functional programming is to eliminate any state |
225 from programs---or at least from the ``interesting bits''. Because then |
235 from programs---or at least from the ``interesting bits'' of the |
226 it is easy to parallelise the resulting programs: if you do not have any |
236 programs. Because then it is easy to parallelise the resulting |
227 state, then once created, all memory content stays unchanged and reads |
237 programs: if you do not have any state, then once created, all memory |
228 to such memory are absolutely safe without the need of any |
238 content stays unchanged and reads to such memory are absolutely safe |
229 synchronisation. An example is given in Figure~\ref{mand} where in the |
239 without the need of any synchronisation. An example is given in |
230 absence of the annoying state, Scala makes it very easy to calculate the |
240 Figure~\ref{mand} where in the absence of the annoying state, Scala |
231 Mandelbrot set on as many cores of your CPU as possible. Why is it so |
241 makes it very easy to calculate the Mandelbrot set on as many cores of |
232 easy in this example? Because each pixel in the Mandelbrot set can be |
242 your CPU as possible. Why is it so easy in this example? Because each |
233 calculated independently and the calculation does not need to update any |
243 pixel in the Mandelbrot set can be calculated independently and the |
234 variable. It is so easy in fact that going from the sequential version |
244 calculation does not need to update any variable. It is so easy in |
235 of the Mandelbrot program to the parallel version can be achieved by |
245 fact that going from the sequential version of the Mandelbrot program |
236 adding just eight characters---in two places you have to add |
246 to the parallel version can be achieved by adding just eight |
237 \texttt{.par}. Try the same in C++ or Java! |
247 characters---in two places you have to add \texttt{.par}. Try the same |
|
248 in C++ or Java! |
238 |
249 |
239 \begin{figure}[p] |
250 \begin{figure}[p] |
240 \begin{boxedminipage}{\textwidth} |
251 \begin{boxedminipage}{\textwidth} |
241 |
252 |
242 A Scala program for generating pretty pictures of the Mandelbrot set.\smallskip\\ |
253 A Scala program for generating pretty pictures of the Mandelbrot set.\smallskip\\ |
291 |
302 |
292 \centering\includegraphics[scale=0.5]{../pics/cpu2.png} & |
303 \centering\includegraphics[scale=0.5]{../pics/cpu2.png} & |
293 \centering\includegraphics[scale=0.5]{../pics/cpu1.png} |
304 \centering\includegraphics[scale=0.5]{../pics/cpu1.png} |
294 \end{tabular} |
305 \end{tabular} |
295 \end{center} |
306 \end{center} |
296 \caption{The ``main'' loops in the Mandelbrot program. |
307 \caption{The code of the ``main'' loops in my Mandelbrot program. |
297 The parallel version differs only in \texttt{.par} being added to the |
308 The parallel version differs only in \texttt{.par} being added to the |
298 ``ranges'' of the x-y-coordinates. As can be seen from the CPU loads, in |
309 ``ranges'' of the x and y coordinates. As can be seen from the CPU loads, in |
299 the sequential versions there is a lower peak for an extended period, |
310 the sequential version there is a lower peak for an extended period, |
300 while in the parallel version there is a short sharp burst for |
311 while in the parallel version there is a short sharp burst for |
301 essentially the same workload\ldots{}meaning you get more work done |
312 essentially the same workload\ldots{}meaning you get more work done |
302 in a shorter amount of time. This \emph{parallelisation} |
313 in a shorter amount of time. This easy \emph{parallelisation} |
303 only works reliably in an immutable program. |
314 only works reliably with an immutable program. |
304 \label{mand}} |
315 \label{mand}} |
305 \end{boxedminipage} |
316 \end{boxedminipage} |
306 \end{figure} |
317 \end{figure} |
307 |
318 |
308 But remember this easy parallelisation of code requires that we |
319 But remember this easy parallelisation of code requires that we |
341 programs. Once you installed Scala, you can start the interpreter by |
352 programs. Once you installed Scala, you can start the interpreter by |
342 typing on the command line: |
353 typing on the command line: |
343 |
354 |
344 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
355 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
345 $ scala |
356 $ scala |
346 Welcome to Scala 2.12.6 (Java HotSpot(TM) 64-Bit Server VM, Java 9). |
357 Welcome to Scala 2.12.7 (Java HotSpot(TM) 64-Bit Server VM, Java 9). |
347 Type in expressions for evaluation. Or try :help. |
358 Type in expressions for evaluation. Or try :help. |
348 |
359 |
349 scala> |
360 scala> |
350 \end{lstlisting}%$ |
361 \end{lstlisting}%$ |
351 |
362 |
531 \noindent |
542 \noindent |
532 where each argument requires its type and the result type of the |
543 where each argument requires its type and the result type of the |
533 function, \code{rty}, should be given. If the body of the function is |
544 function, \code{rty}, should be given. If the body of the function is |
534 more complex, then it can be enclosed in braces, like above. If it it |
545 more complex, then it can be enclosed in braces, like above. If it it |
535 is just a simple expression, like \code{x + 1}, you can omit the |
546 is just a simple expression, like \code{x + 1}, you can omit the |
536 braces. Very often functions are recursive (call themselves) like |
547 braces. Very often functions are recursive (that is call themselves), |
537 the venerable factorial function: |
548 like the venerable factorial function: |
538 |
549 |
539 \begin{lstlisting}[numbers=none] |
550 \begin{lstlisting}[numbers=none] |
540 def fact(n: Int): Int = |
551 def fact(n: Int): Int = |
541 if (n == 0) 1 else n * fact(n - 1) |
552 if (n == 0) 1 else n * fact(n - 1) |
542 \end{lstlisting} |
553 \end{lstlisting} |
732 |
743 |
733 In most functional programming languages, types play an |
744 In most functional programming languages, types play an |
734 important role. Scala is such a language. You have already |
745 important role. Scala is such a language. You have already |
735 seen built-in types, like \code{Int}, \code{Boolean}, |
746 seen built-in types, like \code{Int}, \code{Boolean}, |
736 \code{String} and \code{BigInt}, but also user-defined ones, |
747 \code{String} and \code{BigInt}, but also user-defined ones, |
737 like \code{Rexp}. Unfortunately, types can be a thorny |
748 like \code{Rexp} (see coursework). Unfortunately, types can be a thorny |
738 subject, especially in Scala. For example, why do we need to |
749 subject, especially in Scala. For example, why do we need to |
739 give the type to \code{toSet[Int]}, but not to \code{toList}? |
750 give the type to \code{toSet[Int]}, but not to \code{toList}? |
740 The reason is the power of Scala, which sometimes means it |
751 The reason is the power of Scala, which sometimes means it |
741 cannot infer all necessary typing information. At the |
752 cannot infer all necessary typing information. At the |
742 beginning while getting familiar with Scala, I recommend a |
753 beginning, while getting familiar with Scala, I recommend a |
743 ``play-it-by-ear-approach'' to types. Fully understanding |
754 ``play-it-by-ear-approach'' to types. Fully understanding |
744 type-systems, especially complicated ones like in Scala, can |
755 type-systems, especially complicated ones like in Scala, can |
745 take a module on their own.\footnote{Still, such a study can |
756 take a module on their own.\footnote{Still, such a study can |
746 be a rewarding training: If you are in the business of |
757 be a rewarding training: If you are in the business of |
747 designing new programming languages, you will not be able to |
758 designing new programming languages, you will not be able to |