163 \begin{quote} |
163 \begin{quote} |
164 \url{http://techcrunch.com/2016/06/14/scala-is-the-new-golden-child} |
164 \url{http://techcrunch.com/2016/06/14/scala-is-the-new-golden-child} |
165 \end{quote} |
165 \end{quote} |
166 |
166 |
167 \noindent |
167 \noindent |
168 The official Scala compiler can be downloaded from |
168 The official Scala web-page is here: |
169 |
169 |
170 \begin{quote} |
170 \begin{quote} |
171 \url{http://www.scala-lang.org}\medskip |
171 \url{http://www.scala-lang.org}\medskip |
172 \end{quote} |
172 \end{quote} |
173 |
173 |
174 \noindent\alert |
174 \noindent\alert |
175 Just make sure you are using the version 3(!) of Scala. This is |
175 Just make sure you are using the version 3(!) of Scala. This is |
176 the version I am going to use in the lectures and in the coursework. This |
176 the version I am going to use in the lectures and in the coursework. This |
177 can be any version of Scala 3.X where $X=\{1,2,3\}$. Also the minor |
177 can be any version of Scala 3.X where $X=\{1,2,3\}$. Also the minor |
178 number does not matter. Note that this will be the first year I am |
178 number does not matter. Note that this will be the first year I am |
179 using this version -- so some hiccups are bound to happen. Apologies |
179 using this newer version -- so some hiccups are bound to happen. Apologies |
180 in advance!\bigskip |
180 in advance!\bigskip |
|
181 |
|
182 \begin{tcolorbox}[colback=red!5!white,colframe=red!75!black] |
|
183 I will be using the \textbf{\texttt{scala-cli}} REPL for scala, rather |
|
184 than the ``plain'' Scala REPL. This is a batteries included version of |
|
185 Scala and is easier to use. In fact \texttt{scala-cli} will replace |
|
186 the ``plain'' Scala REPL in future versions. So why not using it now? |
|
187 It can be downloaded from: |
|
188 |
|
189 \begin{center} |
|
190 \url{https://scala-cli.virtuslab.org} |
|
191 \end{center} |
|
192 \end{tcolorbox}\medskip |
|
193 |
181 |
194 |
182 \noindent |
195 \noindent |
183 If you are interested, there are also experimental backend of Scala |
196 If you are interested, there are also experimental backend of Scala |
184 for generating JavaScript code (\url{https://www.scala-js.org}), and |
197 for generating JavaScript code (\url{https://www.scala-js.org}), and |
185 there is work under way to have a native Scala compiler generating |
198 there is work under way to have a native Scala compiler generating |
216 \begin{figure}[t] |
229 \begin{figure}[t] |
217 \begin{boxedminipage}{\textwidth} |
230 \begin{boxedminipage}{\textwidth} |
218 \begin{center} |
231 \begin{center} |
219 \includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{} |
232 \includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{} |
220 \end{center} |
233 \end{center} |
221 \caption{My installation of VS Code includes the following |
234 \caption{My installation of VS Code / Codium includes the |
222 packages from Marketplace: \textbf{Scala Syntax (official)} 0.5.4, |
235 package \textbf{Scala Syntax (official)} 0.5.7 from Marketplace. |
223 \textbf{Code Runner} 0.11.6, \textbf{Code Spell Checker} 2.0.12, |
236 I have also bound the keys \keys{Ctrl} \keys{Ret} to the |
224 \textbf{Rewrap} 1.14.0 and \textbf{Subtle Match |
|
225 Brackets} 3.0.0. I have also bound the keys \keys{Ctrl} \keys{Ret} to the |
|
226 action ``Run-Selected-Text-In-Active-Terminal'' in order to quickly |
237 action ``Run-Selected-Text-In-Active-Terminal'' in order to quickly |
227 evaluate small code snippets in the Scala REPL. I use the internal |
238 evaluate small code snippets in the Scala REPL. I use Codium's internal |
228 terminal to run Scala 2.13.6.\label{vscode}} |
239 terminal to run \texttt{scala-cli} version 1.0.5 which |
|
240 uses Scala 3.3.1.\label{vscode}} |
229 \end{boxedminipage} |
241 \end{boxedminipage} |
230 \end{figure} |
242 \end{figure} |
231 |
243 |
232 Actually \alert last year I switched to VS Codium, which is VS Code |
244 Actually \alert last year I switched to VS Codium, which is VS Code |
233 minus all the telemetry that is normally sent to Microsoft. Apart from |
245 minus all the telemetry that is normally sent to Microsoft. Apart from |
239 \begin{quote} |
251 \begin{quote} |
240 \url{https://vscodium.com} |
252 \url{https://vscodium.com} |
241 \end{quote} |
253 \end{quote} |
242 |
254 |
243 |
255 |
244 What I like most about VS Code/Codium is that it provides easy access to the |
256 What I like most about VS Code/Codium is that it provides easy access |
245 Scala REPL. But if you prefer another editor for coding, it is also |
257 to the Scala REPL. But if you prefer another editor for coding, it is |
246 painless to work with Scala completely on the command line (as you might |
258 also painless to work with Scala completely on the command line (as |
247 have done with \texttt{g++} in the earlier part of PEP). For the |
259 you might have done with \texttt{g++} in the earlier part of PEP). For |
248 lazybones among us, there are even online editors and environments for |
260 the lazybones among us, there are even online editors and environments |
249 developing and running Scala programs: \textit{ScalaFiddle} |
261 for developing and running Scala programs: \textit{Scastie} and |
250 and \textit{Scastie} are two of them. They require zero setup |
262 \textit{ScalaFiddle} are two of them. They require zero setup |
251 (assuming you have a browser handy). You can access them at |
263 (assuming you have a browser handy). You can access them at |
252 |
264 |
253 \begin{quote} |
265 \begin{quote} |
254 \url{https://scalafiddle.io}\\ |
266 \url{https://scastie.scala-lang.org}\\ |
255 \url{https://scastie.scala-lang.org}\medskip |
267 \url{https://scalafiddle.io}\medskip |
256 \end{quote} |
268 \end{quote} |
257 |
269 |
258 \noindent |
270 \noindent |
259 But you should be careful if you use them for your coursework: they |
271 But you should be careful if you use them for your coursework: they |
260 are meant to play around, not really for serious work. |
272 are meant to play around, not really for serious work. Make |
|
273 sure your \texttt{scala-cli} works on your own machine ASAP! |
261 |
274 |
262 As one might expect, Scala can be used with the heavy-duty IDEs |
275 As one might expect, Scala can be used with the heavy-duty IDEs |
263 Eclipse and IntelliJ. A ready-made Scala bundle for Eclipse is |
276 Eclipse and IntelliJ. For example IntelliJ includes plugins for |
264 available from |
277 Scala |
265 |
278 |
266 \begin{quote} |
279 \begin{quote} |
267 \url{http://scala-ide.org/download/sdk.html} |
280 \url{https://scalacenter.github.io/bloop/docs/ides/intellij} |
268 \end{quote} |
281 \end{quote} |
269 |
282 |
270 \noindent |
283 \noindent |
271 Also IntelliJ includes plugins for Scala. \underline{\textbf{BUT}}, |
284 \underline{\textbf{BUT}}, I do \textbf{not} recommend the usage of |
272 I do \textbf{not} recommend the usage of either Eclipse or IntelliJ for PEP: these IDEs |
285 either Eclipse or IntelliJ for PEP: these IDEs seem to make your life |
273 seem to make your life harder, rather than easier, for the small |
286 harder, rather than easier, for the small programs that we will write |
274 programs that we will write in this module. They are really meant to be used |
287 in this module. They are really meant to be used when you have a |
275 when you have a million-lines codebase instead of our small |
288 million-lines codebase instead of our small |
276 ``toy-programs''\ldots{}for example why on earth am I required to create a |
289 ``toy-programs''\ldots{}for example why on earth am I required to |
277 completely new project with several subdirectories when I just want to |
290 create a completely new project with several subdirectories when I |
278 try out 20-lines of Scala code? Your mileage may vary though.~\texttt{;o)} |
291 just want to try out 20-lines of Scala code? Your mileage may vary |
|
292 though.~\texttt{;o)} |
279 |
293 |
280 \subsection*{Why Functional Programming?} |
294 \subsection*{Why Functional Programming?} |
281 |
295 |
282 Before we go on, let me explain a bit more why we want to inflict upon |
296 Before we go on, let me explain a bit more why we want to inflict upon |
283 you another programming language. You hopefully have mastered Java and |
297 you another programming language. You hopefully have mastered Java and |
284 C++\ldots{}the world should be your oyster, no? Well, matters are not as |
298 C++, possibly Python\ldots{} the world should be your oyster, no? |
285 simple as one might wish. We do require Scala in PEP, but actually we do |
299 Well, matters are not as simple as one might wish. We do require Scala |
286 not religiously care whether you learn Scala---after all it is just a |
300 in PEP, but actually we do not religiously care whether you learn |
287 programming language (albeit a nifty one IMHO). What we do care about is |
301 Scala---after all it is just a programming language (albeit a nifty |
288 that you learn about \textit{functional programming}. Scala is just the |
302 one IMHO). What we do care about is that you learn about |
289 vehicle for that. Still, you need to learn Scala well enough to get good |
303 \textit{functional programming}. Scala is just the vehicle for |
290 marks in PEP, but functional programming could perhaps equally be taught |
304 that. Still, you need to learn Scala well enough to get good marks in |
291 with Haskell, F\#, SML, Ocaml, Kotlin, Clojure, Scheme, Elm and many |
305 PEP, but functional programming could perhaps equally be taught with |
292 other functional programming languages. |
306 Haskell, F\#, SML, Ocaml, Kotlin, Clojure, Scheme, Elm and many other |
|
307 functional programming languages. |
293 |
308 |
294 %Your friendly lecturer just |
309 %Your friendly lecturer just |
295 %happens to like Scala and the Department agreed that it is a good idea |
310 %happens to like Scala and the Department agreed that it is a good idea |
296 %to inflict Scala upon you. |
311 %to inflict Scala upon you. |
297 |
312 |
298 Very likely writing programs in a functional programming language is |
313 Very likely writing programs in a functional programming language is |
299 quite different from what you are used to in your study so far. It |
314 quite different from what you are used to in your study so far. It |
300 might even be totally alien to you. The reason is that functional |
315 might even be totally alien to you. The reason is that functional |
301 programming seems to go against the core principles of |
316 programming seems to go against the core principles of |
302 \textit{imperative programming} (which is what you do in Java and |
317 \textit{imperative programming} (which is what you do in Java and |
303 C/C++ for example). The main idea of imperative programming is that |
318 C/C++). The main idea of imperative programming is that |
304 you have some form of \emph{state} in your program and you |
319 you have some form of \emph{state} in your program and you |
305 continuously change this state by issuing some commands---for example |
320 continuously change this state by issuing some commands---for example |
306 for updating a field in an array or for adding one to a variable |
321 for updating a field in an array or for adding one to a variable |
307 stored in memory and so on. The classic example for this style of |
322 stored in memory and so on. The classic example for this style of |
308 programming is a \texttt{for}-loop in C/C++. Consider the snippet: |
323 programming is a \texttt{for}-loop in C/C++. Consider the snippet: |
530 |
545 |
531 \noindent |
546 \noindent |
532 Another classic example you can try out is |
547 Another classic example you can try out is |
533 |
548 |
534 \begin{lstlisting}[numbers=none,language={}] |
549 \begin{lstlisting}[numbers=none,language={}] |
535 scala> print("hello world") |
550 scala> println("hello world") |
536 hello world |
551 hello world |
537 \end{lstlisting} |
552 \end{lstlisting} |
538 |
553 |
539 \noindent Note that in this case there is no result. The |
554 \noindent Note that in this case there is no result. The |
540 reason is that \code{print} does not actually produce a result |
555 reason is that \code{println} does not actually produce a result |
541 (there is no \code{resX} and no type), rather it is a |
556 (there is no \code{resX} and no type), rather it is a |
542 function that causes the \emph{side-effect} of printing out a |
557 function that causes the \emph{side-effect} of printing out a |
543 string. Once you are more familiar with the functional |
558 string. Once you are more familiar with the functional |
544 programming-style, you will know what the difference is |
559 programming-style, you will know what the difference is |
545 between a function that returns a result, like addition, and a |
560 between a function that returns a result, like addition, and a |
546 function that causes a side-effect, like \code{print}. We |
561 function that causes a side-effect, like \code{println}. We |
547 shall come back to this point later, but if you are curious |
562 shall come back to this point later, but if you are curious |
548 now, the latter kind of functions always has \code{Unit} as |
563 now, the latter kind of functions always has \code{Unit} as |
549 return type. It is just not printed by Scala. |
564 return type. It is just not printed by Scala. |
550 |
565 |
551 You can try more examples with the Scala REPL, but feel free to |
566 You can try more examples with the \texttt{scala-cli} REPL, but feel free to |
552 first guess what the result is (not all answers by Scala are obvious): |
567 first guess what the result is (not all answers by Scala are obvious): |
553 |
568 |
554 \begin{lstlisting}[numbers=none] |
569 \begin{lstlisting}[numbers=none,language={}] |
555 scala> 2 + 2 |
570 scala> 2 + 2 |
556 scala> 1 / 2 |
571 scala> 1 / 2 |
557 scala> 1.0 / 2 |
572 scala> 1.0 / 2 |
558 scala> 1 / 2.0 |
573 scala> 1 / 2.0 |
559 scala> 1 / 0 |
574 scala> 1 / 0 |
589 able to ``play around'' with it! |
604 able to ``play around'' with it! |
590 |
605 |
591 \subsection*{Standalone Scala Apps} |
606 \subsection*{Standalone Scala Apps} |
592 |
607 |
593 If you want to write a standalone app in Scala, you can |
608 If you want to write a standalone app in Scala, you can |
594 implement an object that is an instance of \code{App}. For example |
609 implement a function \texttt{hello} and annotate the tag |
595 write |
610 \texttt{@main}. For example write |
596 |
611 |
597 \begin{lstlisting}[numbers=none] |
612 \begin{lstlisting}[numbers=none] |
598 object Hello extends App { |
613 @main |
599 println("hello world") |
614 def Hello() = println("hello world") |
600 } |
615 \end{lstlisting} |
601 \end{lstlisting} |
616 |
|
617 %object Hello extends App { |
|
618 % println("hello world") |
|
619 %} |
602 |
620 |
603 \noindent save it in a file, say {\tt hello-world.scala}, and |
621 \noindent save it in a file, say {\tt hello-world.scala}, and |
604 then run the compiler (\texttt{scalac}) and start the runtime |
622 then use \texttt{scala-cli run} (which internally compiles the |
605 environment (\texttt{scala}): |
623 scala file and runs it): |
606 |
624 |
607 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
625 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
608 $ scalac hello-world.scala |
626 $ scala-cli run hello-world.scala |
609 $ scala Hello |
|
610 hello world |
627 hello world |
611 \end{lstlisting} |
628 \end{lstlisting} |
612 |
629 |
613 \noindent |
630 \noindent |
614 Like Java, Scala targets the JVM and consequently |
631 Like Java, Scala targets the JVM and consequently |
615 Scala programs can also be executed by the bog-standard Java |
632 Scala programs can also be executed by the bog-standard Java |
616 Runtime. This only requires the inclusion of {\tt |
633 Runtime. This can be done as follows: |
617 scala-library.jar}, which on my computer can be done as |
|
618 follows: |
|
619 |
634 |
620 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
635 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
621 $ scalac hello-world.scala |
636 $ scala-cli --power package --assembly hello-world.scala |
622 $ java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello |
637 $ java -jar Hello.jar |
623 hello world |
638 hello world |
624 \end{lstlisting} |
639 \end{lstlisting} |
625 |
640 |
626 \noindent You might need to adapt the path to where you have |
|
627 installed Scala. |
|
628 |
641 |
629 \subsection*{Values} |
642 \subsection*{Values} |
630 |
643 |
631 \begin{tcolorbox}[colback=red!5!white,colframe=red!75!black] |
644 \begin{tcolorbox}[colback=red!5!white,colframe=red!75!black] |
632 Do not use \code{var} in your code for PEP! This declares a mutable variable. |
645 Do not use \code{var} in your code for PEP! This declares a mutable variable. |
747 } |
760 } |
748 \end{lstlisting} |
761 \end{lstlisting} |
749 |
762 |
750 \noindent |
763 \noindent |
751 but this seems a bit overkill for a small function like \code{fact}. |
764 but this seems a bit overkill for a small function like \code{fact}. |
752 Notice that Scala does not have a \code{then}-keyword in an |
765 Notice that I did not use a \code{then}-keyword in the |
753 \code{if}-statement. Also important is that there should be always an |
766 \code{if}-statements and that I enclosed the condition inside |
754 \code{else}-branch. Never write an \code{if} without an \code{else}, |
767 parentheses, like \code{(n == 0)}. Your eyes might hurt to not see an |
755 unless you know what you are doing! While \code{def} is the main |
768 \code{else} with an \code{if}, but this has been long established |
|
769 syntax for \code{if}-statements. Scala, to my knowledge, was pretty |
|
770 unique in that for nearly 20 years of its existence\ldots{}until Scala |
|
771 3 came along. While people like me have perfectly adapted to the sight |
|
772 of \code{if}s without \code{then}s, it seems the developers of Scala |
|
773 caved in to the special eyesight of Gen-Python people and now allow to |
|
774 write the same function also as |
|
775 |
|
776 \begin{lstlisting}[numbers=none] |
|
777 def fact(n: Int) : Int = { |
|
778 if n == 0 then 1 |
|
779 else n * fact(n - 1) |
|
780 } |
|
781 \end{lstlisting} |
|
782 |
|
783 \noindent |
|
784 I accept this might look a bit more familiar to beginners of Scala, if |
|
785 they come from other languages, like Java or C++. But that we also had |
|
786 to get rid in Scala 3 of the familiar \texttt{\{\}}-parentheses is |
|
787 completely beyond me. So in Scala 3 the braces are optional and the |
|
788 \texttt{fact}-function can even be written as |
|
789 |
|
790 \begin{lstlisting}[numbers=none] |
|
791 def fact(n: Int) : Int = |
|
792 if n == 0 then 1 |
|
793 else n * fact(n - 1) |
|
794 \end{lstlisting} |
|
795 |
|
796 \noindent on the condition that indents now become \emph{meaningful} |
|
797 (as in Python).\raisebox{-0.7mm}{\emoji{face-vomiting}} All versions are now permitted in Scala 3. While you |
|
798 are free to use any syntax version you want in PEP, or even mix them, |
|
799 I will \textbf{not} show you any of my code in the newfangled |
|
800 Pythonesque meaningful-indent-syntax. When necessary, I will always |
|
801 use braces to indicate the beginning and end of a code block, and I |
|
802 have not yet get used to the \code{if}s with |
|
803 \code{then}s.\footnote{Scala adopted some very fine features of Python, for example string interpolations, but that we had to completely cave in to |
|
804 the demands of Gen-Python is a step to far for my completely |
|
805 insignificant opinion. For me this is a bridge too far. |
|
806 I always assumed escaping Python's dependency hell |
|
807 is every software developers life goal---apparently not. ;o)} |
|
808 |
|
809 |
|
810 However, no matter which syntax style you adopt for \code{if}s, never |
|
811 write an \code{if} without an \code{else}-branch! That has something |
|
812 to do with functional programming and you will see its purpose later |
|
813 on. Coming back to function definitions: While \code{def} is the main |
756 mechanism for defining functions, there are a few other ways for doing |
814 mechanism for defining functions, there are a few other ways for doing |
757 this. We will see some of them in the next sections. |
815 this. We will see some of them in the next sections. |
758 |
816 |
759 Before we go on, let me explain one tricky point in function |
817 Before we go on, let me explain one tricky point in function |
760 definitions, especially in larger definitions. What does a Scala |
818 definitions, especially in larger definitions. What does a Scala |
1841 Even if Scala has been a success in several high-profile companies, |
1899 Even if Scala has been a success in several high-profile companies, |
1842 there is also a company (Yammer) that first used Scala in their |
1900 there is also a company (Yammer) that first used Scala in their |
1843 production code, but then moved away from it. Allegedly they did not |
1901 production code, but then moved away from it. Allegedly they did not |
1844 like the steep learning curve of Scala and also that new versions of |
1902 like the steep learning curve of Scala and also that new versions of |
1845 Scala often introduced incompatibilities in old code. Also the Java |
1903 Scala often introduced incompatibilities in old code. Also the Java |
1846 language is lately developing at lightening speed (in comparison to the past) |
1904 language is lately developing at lightening speed (in comparison to |
1847 taking on many |
1905 the past) taking on many features of Scala and other languages, and it |
1848 features of Scala and other languages, and it seems it even introduces |
1906 seems it even introduces new features on its own. So there is |
1849 new features on its own. So there is seemingly even more incentive to |
1907 seemingly even more incentive to stick with the old stuff you |
1850 stick with the old stuff you know. |
1908 know. Still, the goal of this part of PEP is to bend your mind about |
|
1909 what programming is\ldots{}namely functional programming. I promise |
|
1910 you, this will be useful no matter with which programming language you |
|
1911 will work. |
1851 |
1912 |
1852 |
1913 |
1853 Scala is deep: After many years, I still continue to learn new technique |
1914 Scala is deep: After many years, I still continue to learn new technique |
1854 for writing more elegant code. |
1915 for writing more elegant code. |
1855 %Unfortunately, I have not yet managed to |
1916 %Unfortunately, I have not yet managed to |