handouts/pep-ho.tex
changeset 491 2a30c7dfe3ed
parent 489 6ee9ad5d766f
equal deleted inserted replaced
490:f70d74fea67f 491:2a30c7dfe3ed
   184 \end{quote}
   184 \end{quote}
   185 
   185 
   186 \noindent\alert
   186 \noindent\alert
   187 For PEP, make sure you are using the version 3(!) of Scala. This is
   187 For PEP, make sure you are using the version 3(!) of Scala. This is
   188 the version I am going to use in the lectures and in the coursework. This
   188 the version I am going to use in the lectures and in the coursework. This
   189 can be any version of Scala 3.X where $X=\{3,4\}$. Also the minor
   189 can be any version of Scala 3.X where $X=\{4,5\}$. Also the minor
   190 number does not matter. Note that this will be the second year I am
   190 number does not matter. Note that this will be the second year I am
   191 using this newer version of Scala -- some hiccups can still happen. Apologies
   191 using this newer version of Scala -- some hiccups can still happen. Apologies
   192 in advance!\bigskip
   192 in advance!\bigskip
   193 
   193 
   194 \begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
   194 %\begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
   195   I will be using the \textbf{\texttt{scala-cli}} REPL for Scala 3, rather
   195 %  I will be using the \textbf{\texttt{scala-cli}} REPL for Scala 3, rather
   196   than the ``plain'' Scala REPL. This is a batteries included version of
   196 %  than the ``plain'' Scala REPL. This is a batteries included version of
   197   Scala 3 and is easier to use and to install. In fact
   197 %  Scala 3 and is easier to use and to install. In fact
   198   \texttt{scala-cli} is designated to replace
   198 %  \texttt{scala-cli} is designated to replace
   199   the ``plain'' Scala REPL in future versions of Scala.
   199 %  the ``plain'' Scala REPL in future versions of Scala.
   200   So why not using it now?
   200 %  So why not using it now?
   201   It can be downloaded from:
   201 %  It can be downloaded from:%
   202 
   202 %
   203   \begin{center}
   203 %  \begin{center}
   204   \url{https://scala-cli.virtuslab.org}
   204 % \url{https://scala-cli.virtuslab.org}
   205   \end{center}
   205 %  \end{center}
   206 \end{tcolorbox}\medskip
   206 %\end{tcolorbox}\medskip
   207 
   207 
   208 
   208 
   209 \noindent
   209 \noindent
   210 If you are interested, there are also experimental backends for Scala
   210 If you are interested, there are also experimental backends for Scala
   211 for generating JavaScript code (\url{https://www.scala-js.org}), and
   211 for generating JavaScript code (\url{https://www.scala-js.org}), and
   249 \caption{My installation of VS Code / Codium includes the
   249 \caption{My installation of VS Code / Codium includes the
   250   package  \textbf{Scala Syntax (official)} 0.5.7 from Marketplace.
   250   package  \textbf{Scala Syntax (official)} 0.5.7 from Marketplace.
   251   I have also bound the keys \keys{Ctrl} \keys{Ret} to the
   251   I have also bound the keys \keys{Ctrl} \keys{Ret} to the
   252   action ``Run-Selected-Text-In-Active-Terminal'' in order to quickly
   252   action ``Run-Selected-Text-In-Active-Terminal'' in order to quickly
   253   evaluate small code snippets in the Scala REPL. I use Codium's internal
   253   evaluate small code snippets in the Scala REPL. I use Codium's internal
   254   terminal to run \texttt{scala-cli} version 1.0.5 which
   254   terminal to run \texttt{scala} version 1.0.5 which
   255   uses Scala 3.3.1.\label{vscode}}
   255   uses Scala 3.3.1.\label{vscode}}
   256 \end{boxedminipage}
   256 \end{boxedminipage}
   257 \end{figure}
   257 \end{figure}
   258 
   258 
   259 Actually \alert last year I switched to VS Codium as IDE for writing Scala programs. VS Codium is VS Code
   259 Actually \alert last year I switched to VS Codium as IDE for writing Scala programs. VS Codium is VS Code
   282 \end{quote}
   282 \end{quote}
   283 
   283 
   284 \noindent
   284 \noindent
   285 But you should be careful if you use them for your coursework: they
   285 But you should be careful if you use them for your coursework: they
   286 are meant to play around, not really for serious work. Therefore make
   286 are meant to play around, not really for serious work. Therefore make
   287 sure \texttt{scala-cli} works on your own machine ASAP!
   287 sure \texttt{scala} works on your own machine ASAP!
   288 
   288 
   289 As one might expect, Scala can be used with the heavy-duty IDEs
   289 As one might expect, Scala can be used with the heavy-duty IDEs
   290 Eclipse and IntelliJ. For example IntelliJ includes plugins for
   290 Eclipse and IntelliJ. For example IntelliJ includes plugins for
   291 Scala
   291 Scala
   292 
   292 
   532 \noindent
   532 \noindent
   533 Relevant xkcd entries about functional programming are XXX.
   533 Relevant xkcd entries about functional programming are XXX.
   534 
   534 
   535 \subsection*{The Very Basics}
   535 \subsection*{The Very Basics}
   536 
   536 
   537 Let us get back to Scala and \texttt{scala-cli}: One advantage of
   537 Let us get back to Scala: One advantage of
   538 Scala over Java is that it includes an interpreter (a REPL, or
   538 Scala over Java is that it includes an interpreter (a REPL, or
   539 \underline{R}ead-\underline{E}val-\underline{P}rint-\underline{L}oop)
   539 \underline{R}ead-\underline{E}val-\underline{P}rint-\underline{L}oop)
   540 with which you can run and test small code snippets without the need
   540 with which you can run and test small code snippets without the need
   541 of a compiler. This helps a lot with interactively developing
   541 of a compiler. This helps a lot with interactively developing
   542 programs. It is my preferred way of writing small Scala programs. Once
   542 programs. It is my preferred way of writing small Scala programs. Once
   543 you installed \texttt{scala-cli}, you can start the interpreter by typing on the
   543 you installed \texttt{scala}, you can start the interpreter by typing on the
   544 command line:
   544 command line:
   545 
   545 
   546 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
   546 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
   547 $ scala-cli
   547 $ scala
   548 Welcome to Scala 3.4.1 (21.0.2, Java OpenJDK 64-Bit Server VM).
   548 Welcome to Scala 3.5.1 (21.0.4, Java OpenJDK 64-Bit Server VM).
   549 Type in expressions for evaluation. Or try :help.
   549 Type in expressions for evaluation. Or try :help.  
   550 
   550 
   551 scala>
   551 scala>
   552 \end{lstlisting}%$
   552 \end{lstlisting}%$
   553 
   553 
   554 \noindent The precise response may vary depending on the version and
   554 \noindent The precise response may vary depending on the version and
   555 platform where you installed \texttt{scala-cli}. Make sure however that
   555 platform where you installed \texttt{scala}. Make sure however that
   556 \texttt{scala-cli} uses Scala version 3---you can find the version
   556 \texttt{scala} uses version 3---you can find the version
   557 number in the welcome message. Also note that at the first time
   557 number in the welcome message. Also note that at the first time
   558 \texttt{scala-cli} runs, it might download various components, for
   558 \texttt{scala} runs, it might download various components, for
   559 example the Scala compiler, Scala runtimes etc. Once
   559 example the Scala compiler, Scala runtimes etc. Once
   560 \texttt{scala-cli} is up and running, you can type at the prompt
   560 \texttt{scala} is up and running, you can type at the prompt
   561 expressions like \code{2 + 3}\;\keys{Ret} and the output will be
   561 expressions like \code{2 + 3}\;\keys{Ret} and the output will be
   562 
   562 
   563 \begin{lstlisting}[numbers=none,language={}]
   563 \begin{lstlisting}[numbers=none,language={}]
   564 scala> 2 + 3
   564 scala> 2 + 3
   565 val res0: Int = 5
   565 val res0: Int = 5
   593 function that causes a side-effect, like \code{println}. We
   593 function that causes a side-effect, like \code{println}. We
   594 shall come back to this point later, but if you are curious
   594 shall come back to this point later, but if you are curious
   595 now, the latter kind of functions always has \code{Unit} as
   595 now, the latter kind of functions always has \code{Unit} as
   596 return type. It is just not printed by Scala.
   596 return type. It is just not printed by Scala.
   597 
   597 
   598 You can try more examples with the \texttt{scala-cli} REPL, but feel free to
   598 You can try more examples with the \texttt{scala} REPL, but feel free to
   599 first guess what the result is (not all answers by Scala are obvious):
   599 first guess what the result is (not all answers by Scala are obvious):
   600 
   600 
   601 \begin{lstlisting}[numbers=none,language={}]
   601 \begin{lstlisting}[numbers=none,language={}]
   602 scala> 2 + 2
   602 scala> 2 + 2
   603 scala> 1 / 2
   603 scala> 1 / 2
   652 %object Hello extends App {
   652 %object Hello extends App {
   653 %    println("hello world")
   653 %    println("hello world")
   654 %}
   654 %}
   655 
   655 
   656 \noindent save it in a file, say {\tt hello-world.scala}, and
   656 \noindent save it in a file, say {\tt hello-world.scala}, and
   657 then use \texttt{scala-cli} (which compiles the
   657 then use \texttt{scala} (which compiles the
   658 scala file and runs it):
   658 scala file and runs it):
   659 
   659 
   660 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
   660 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
   661 $ scala-cli hello-world.scala
   661 $ scala hello-world.scala
   662 hello world
   662 hello world
   663 \end{lstlisting}
   663 \end{lstlisting}
   664 
   664 
   665 \noindent
   665 \noindent
   666 Like Java, Scala targets the JVM and consequently
   666 Like Java, Scala targets the JVM and consequently
   667 Scala programs can also be executed by the bog-standard Java
   667 Scala programs can also be executed by the bog-standard Java
   668 Runtime. This can be done as follows:
   668 Runtime. This can be done as follows:
   669 
   669 
   670 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
   670 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
   671 $ scala-cli --power package --assembly hello-world.scala
   671 $ scala --power package --assembly hello-world.scala
   672 $ java -jar Hello.jar
   672 $ java -jar Hello.jar
   673 hello world
   673 hello world
   674 \end{lstlisting}
   674 \end{lstlisting}
   675 
   675 
   676 
   676