--- a/handouts/pep-ho.tex Sat Sep 23 23:49:44 2023 +0100
+++ b/handouts/pep-ho.tex Wed Nov 01 15:01:32 2023 +0000
@@ -11,7 +11,7 @@
\newcommand{\annotation}[1]{\hfill\footnotesize{}#1}
\usepackage{menukeys}
-
+\usepackage{emoji}
%cheat sheet
%http://worldline.github.io/scala-cheatsheet/
@@ -165,7 +165,7 @@
\end{quote}
\noindent
-The official Scala compiler can be downloaded from
+The official Scala web-page is here:
\begin{quote}
\url{http://www.scala-lang.org}\medskip
@@ -176,9 +176,22 @@
the version I am going to use in the lectures and in the coursework. This
can be any version of Scala 3.X where $X=\{1,2,3\}$. Also the minor
number does not matter. Note that this will be the first year I am
-using this version -- so some hiccups are bound to happen. Apologies
+using this newer version -- so some hiccups are bound to happen. Apologies
in advance!\bigskip
+\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
+ I will be using the \textbf{\texttt{scala-cli}} REPL for scala, rather
+ than the ``plain'' Scala REPL. This is a batteries included version of
+ Scala and is easier to use. In fact \texttt{scala-cli} will replace
+ the ``plain'' Scala REPL in future versions. So why not using it now?
+ It can be downloaded from:
+
+ \begin{center}
+ \url{https://scala-cli.virtuslab.org}
+ \end{center}
+\end{tcolorbox}\medskip
+
+
\noindent
If you are interested, there are also experimental backend of Scala
for generating JavaScript code (\url{https://www.scala-js.org}), and
@@ -218,14 +231,13 @@
\begin{center}
\includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{}
\end{center}
-\caption{My installation of VS Code includes the following
- packages from Marketplace: \textbf{Scala Syntax (official)} 0.5.4,
- \textbf{Code Runner} 0.11.6, \textbf{Code Spell Checker} 2.0.12,
- \textbf{Rewrap} 1.14.0 and \textbf{Subtle Match
- Brackets} 3.0.0. I have also bound the keys \keys{Ctrl} \keys{Ret} to the
+\caption{My installation of VS Code / Codium includes the
+ package \textbf{Scala Syntax (official)} 0.5.7 from Marketplace.
+ I have also bound the keys \keys{Ctrl} \keys{Ret} to the
action ``Run-Selected-Text-In-Active-Terminal'' in order to quickly
- evaluate small code snippets in the Scala REPL. I use the internal
- terminal to run Scala 2.13.6.\label{vscode}}
+ evaluate small code snippets in the Scala REPL. I use Codium's internal
+ terminal to run \texttt{scala-cli} version 1.0.5 which
+ uses Scala 3.3.1.\label{vscode}}
\end{boxedminipage}
\end{figure}
@@ -241,55 +253,58 @@
\end{quote}
-What I like most about VS Code/Codium is that it provides easy access to the
-Scala REPL. But if you prefer another editor for coding, it is also
-painless to work with Scala completely on the command line (as you might
-have done with \texttt{g++} in the earlier part of PEP). For the
-lazybones among us, there are even online editors and environments for
-developing and running Scala programs: \textit{ScalaFiddle}
-and \textit{Scastie} are two of them. They require zero setup
-(assuming you have a browser handy). You can access them at
+What I like most about VS Code/Codium is that it provides easy access
+to the Scala REPL. But if you prefer another editor for coding, it is
+also painless to work with Scala completely on the command line (as
+you might have done with \texttt{g++} in the earlier part of PEP). For
+the lazybones among us, there are even online editors and environments
+for developing and running Scala programs: \textit{Scastie} and
+\textit{ScalaFiddle} are two of them. They require zero setup
+(assuming you have a browser handy). You can access them at
\begin{quote}
- \url{https://scalafiddle.io}\\
- \url{https://scastie.scala-lang.org}\medskip
+ \url{https://scastie.scala-lang.org}\\
+ \url{https://scalafiddle.io}\medskip
\end{quote}
\noindent
But you should be careful if you use them for your coursework: they
-are meant to play around, not really for serious work.
+are meant to play around, not really for serious work. Make
+sure your \texttt{scala-cli} works on your own machine ASAP!
As one might expect, Scala can be used with the heavy-duty IDEs
-Eclipse and IntelliJ. A ready-made Scala bundle for Eclipse is
-available from
+Eclipse and IntelliJ. For example IntelliJ includes plugins for
+Scala
\begin{quote}
-\url{http://scala-ide.org/download/sdk.html}
+\url{https://scalacenter.github.io/bloop/docs/ides/intellij}
\end{quote}
\noindent
-Also IntelliJ includes plugins for Scala. \underline{\textbf{BUT}},
-I do \textbf{not} recommend the usage of either Eclipse or IntelliJ for PEP: these IDEs
-seem to make your life harder, rather than easier, for the small
-programs that we will write in this module. They are really meant to be used
-when you have a million-lines codebase instead of our small
-``toy-programs''\ldots{}for example why on earth am I required to create a
-completely new project with several subdirectories when I just want to
-try out 20-lines of Scala code? Your mileage may vary though.~\texttt{;o)}
+\underline{\textbf{BUT}}, I do \textbf{not} recommend the usage of
+either Eclipse or IntelliJ for PEP: these IDEs seem to make your life
+harder, rather than easier, for the small programs that we will write
+in this module. They are really meant to be used when you have a
+million-lines codebase instead of our small
+``toy-programs''\ldots{}for example why on earth am I required to
+create a completely new project with several subdirectories when I
+just want to try out 20-lines of Scala code? Your mileage may vary
+though.~\texttt{;o)}
\subsection*{Why Functional Programming?}
Before we go on, let me explain a bit more why we want to inflict upon
you another programming language. You hopefully have mastered Java and
-C++\ldots{}the world should be your oyster, no? Well, matters are not as
-simple as one might wish. We do require Scala in PEP, but actually we do
-not religiously 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
-marks in PEP, but functional programming could perhaps equally be taught
-with Haskell, F\#, SML, Ocaml, Kotlin, Clojure, Scheme, Elm and many
-other functional programming languages.
+C++, possibly Python\ldots{} the world should be your oyster, no?
+Well, matters are not as simple as one might wish. We do require Scala
+in PEP, but actually we do not religiously 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 marks in
+PEP, but functional programming could perhaps equally be taught with
+Haskell, 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
@@ -300,7 +315,7 @@
might even be totally alien to you. The reason is that functional
programming seems to go against the core principles of
\textit{imperative programming} (which is what you do in Java and
-C/C++ for example). The main idea of imperative programming is that
+C/C++). The main idea of imperative programming is that
you have some form of \emph{state} in your program and you
continuously change this state by issuing some commands---for example
for updating a field in an array or for adding one to a variable
@@ -500,16 +515,16 @@
command line:
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
-$ scala
+$ scala-cli
Welcome to Scala 3.3.1 (17.0.8.1, Java OpenJDK 64-Bit Server VM).
-Type in expressions for evaluation. Or try :help.
+Type in expressions for evaluation. Or try :help.
scala>
\end{lstlisting}%$
\noindent The precise response may vary depending
on the version and platform where you installed Scala. Make sure
-you have installed Scala version 3. At the Scala
+\texttt{scala-cli} uses Scala version 3. At the Scala
prompt you can type things like \code{2 + 3}\;\keys{Ret} and
the output will be
@@ -532,26 +547,26 @@
Another classic example you can try out is
\begin{lstlisting}[numbers=none,language={}]
-scala> print("hello world")
+scala> println("hello world")
hello world
\end{lstlisting}
\noindent Note that in this case there is no result. The
-reason is that \code{print} does not actually produce a result
+reason is that \code{println} does not actually produce a result
(there is no \code{resX} and no type), rather it is a
function that causes the \emph{side-effect} of printing out a
string. Once you are more familiar with the functional
programming-style, you will know what the difference is
between a function that returns a result, like addition, and a
-function that causes a side-effect, like \code{print}. We
+function that causes a side-effect, like \code{println}. 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 by Scala.
-You can try more examples with the Scala REPL, but feel free to
+You can try more examples with the \texttt{scala-cli} REPL, but feel free to
first guess what the result is (not all answers by Scala are obvious):
-\begin{lstlisting}[numbers=none]
+\begin{lstlisting}[numbers=none,language={}]
scala> 2 + 2
scala> 1 / 2
scala> 1.0 / 2
@@ -591,40 +606,38 @@
\subsection*{Standalone Scala Apps}
If you want to write a standalone app in Scala, you can
-implement an object that is an instance of \code{App}. For example
-write
+implement a function \texttt{hello} and annotate the tag
+\texttt{@main}. For example write
\begin{lstlisting}[numbers=none]
-object Hello extends App {
- println("hello world")
-}
+@main
+def Hello() = println("hello world")
\end{lstlisting}
+%object Hello extends App {
+% println("hello world")
+%}
+
\noindent save it in a file, say {\tt hello-world.scala}, and
-then run the compiler (\texttt{scalac}) and start the runtime
-environment (\texttt{scala}):
+then use \texttt{scala-cli run} (which internally compiles the
+scala file and runs it):
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
-$ scalac hello-world.scala
-$ scala Hello
+$ scala-cli run hello-world.scala
hello world
\end{lstlisting}
\noindent
Like Java, Scala targets the JVM and consequently
Scala programs can also be executed by the bog-standard Java
-Runtime. This only requires the inclusion of {\tt
-scala-library.jar}, which on my computer can be done as
-follows:
+Runtime. This can be done as follows:
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
-$ scalac hello-world.scala
-$ java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello
+$ scala-cli --power package --assembly hello-world.scala
+$ java -jar Hello.jar
hello world
\end{lstlisting}
-\noindent You might need to adapt the path to where you have
-installed Scala.
\subsection*{Values}
@@ -749,10 +762,55 @@
\noindent
but this seems a bit overkill for a small function like \code{fact}.
-Notice that Scala does not have a \code{then}-keyword in an
-\code{if}-statement. Also important is that there should be always an
-\code{else}-branch. Never write an \code{if} without an \code{else},
-unless you know what you are doing! While \code{def} is the main
+Notice that I did not use a \code{then}-keyword in the
+\code{if}-statements and that I enclosed the condition inside
+parentheses, like \code{(n == 0)}. Your eyes might hurt to not see an
+\code{else} with an \code{if}, but this has been long established
+syntax for \code{if}-statements. Scala, to my knowledge, was pretty
+unique in that for nearly 20 years of its existence\ldots{}until Scala
+3 came along. While people like me have perfectly adapted to the sight
+of \code{if}s without \code{then}s, it seems the developers of Scala
+caved in to the special eyesight of Gen-Python people and now allow to
+write the same function also as
+
+\begin{lstlisting}[numbers=none]
+def fact(n: Int) : Int = {
+ if n == 0 then 1
+ else n * fact(n - 1)
+}
+\end{lstlisting}
+
+\noindent
+I accept this might look a bit more familiar to beginners of Scala, if
+they come from other languages, like Java or C++. But that we also had
+to get rid in Scala 3 of the familiar \texttt{\{\}}-parentheses is
+completely beyond me. So in Scala 3 the braces are optional and the
+\texttt{fact}-function can even be written as
+
+\begin{lstlisting}[numbers=none]
+def fact(n: Int) : Int =
+ if n == 0 then 1
+ else n * fact(n - 1)
+\end{lstlisting}
+
+\noindent on the condition that indents now become \emph{meaningful}
+(as in Python).\raisebox{-0.7mm}{\emoji{face-vomiting}} All versions are now permitted in Scala 3. While you
+are free to use any syntax version you want in PEP, or even mix them,
+I will \textbf{not} show you any of my code in the newfangled
+Pythonesque meaningful-indent-syntax. When necessary, I will always
+use braces to indicate the beginning and end of a code block, and I
+have not yet get used to the \code{if}s with
+\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
+ the demands of Gen-Python is a step to far for my completely
+ insignificant opinion. For me this is a bridge too far.
+ I always assumed escaping Python's dependency hell
+is every software developers life goal---apparently not. ;o)}
+
+
+However, no matter which syntax style you adopt for \code{if}s, never
+write an \code{if} without an \code{else}-branch! That has something
+to do with functional programming and you will see its purpose later
+on. Coming back to function definitions: While \code{def} is the main
mechanism for defining functions, there are a few other ways for doing
this. We will see some of them in the next sections.
@@ -1401,7 +1459,7 @@
by looking into the documentation
\begin{quote}
-\url{http://www.scala-lang.org/api/current/}
+\url{https://dotty.epfl.ch/api/index.html}
\end{quote}
The function arrow can also be iterated, as in
@@ -1843,11 +1901,14 @@
production code, but then moved away from it. Allegedly they did not
like the steep learning curve of Scala and also that new versions of
Scala often introduced incompatibilities in old code. Also the Java
-language is lately developing at lightening speed (in comparison to the past)
-taking on many
-features of Scala and other languages, and it seems it even introduces
-new features on its own. So there is seemingly even more incentive to
-stick with the old stuff you know.
+language is lately developing at lightening speed (in comparison to
+the past) taking on many features of Scala and other languages, and it
+seems it even introduces new features on its own. So there is
+seemingly even more incentive to stick with the old stuff you
+know. Still, the goal of this part of PEP is to bend your mind about
+what programming is\ldots{}namely functional programming. I promise
+you, this will be useful no matter with which programming language you
+will work.
Scala is deep: After many years, I still continue to learn new technique
@@ -1884,7 +1945,7 @@
He makes pretty much the same arguments about functional programming and
immutability (one section is teasingly called \textit{``Where Did all
the Bugs Go?''}). If you happen to moan about all the idiotic features
-of Scala, well, I guess this is part of the package according to this
+of Scala (3), well, I guess this is part of the package according to this
quote:\bigskip
%\begin{itemize}