# HG changeset patch # User Christian Urban # Date 1528897095 -3600 # Node ID 31ba76ce016def961d30c732063747143ef9ad9c # Parent 35b4dc785a40d5e97f7f8456ca4d9a54cc15ef85 updated diff -r 35b4dc785a40 -r 31ba76ce016d LINKS --- a/LINKS Mon Jun 11 14:59:09 2018 +0100 +++ b/LINKS Wed Jun 13 14:38:15 2018 +0100 @@ -1,3 +1,7 @@ + + +============= + scala -Yrepl-class-based @@ -51,7 +55,14 @@ http://exercism.io https://www.scala-exercises.org/ http://www.sofiacole.com/technology/adopting-scala-the-next-steps/ + +Reverse polish notation exercise +http://www.codeabbey.com/index/task_view/reverse-polish-notation +Game of 2048 (deterministic version of prefilling +the board and not letting any new tiles in; finding the +maximum) +http://www.codeabbey.com/index/task_view/game-of-2048 https://medium.com/@markcanlasnyc/scala-saturday-functions-for-the-object-oriented-4218f9ed192b#.dw8x8zxvb diff -r 35b4dc785a40 -r 31ba76ce016d handouts/pep-ho.pdf Binary file handouts/pep-ho.pdf has changed diff -r 35b4dc785a40 -r 31ba76ce016d handouts/pep-ho.tex --- a/handouts/pep-ho.tex Mon Jun 11 14:59:09 2018 +0100 +++ b/handouts/pep-ho.tex Wed Jun 13 14:38:15 2018 +0100 @@ -6,7 +6,7 @@ %cheat sheet %http://worldline.github.io/scala-cheatsheet/ -% case class, apply, unappy +% case class, apply, unapply % see https://medium.com/@thejasbabu/scala-pattern-matching-9c9e73ba9a8a \begin{document} @@ -14,30 +14,34 @@ \section*{A Crash-Course in Scala} -\mbox{}\hfill\textit{``Scala --- Slowly Compiled Academic LAnguage''}\\ +\mbox{}\hfill\textit{``Scala --- \underline{S}lowly \underline{C}ompiled +\underline{A}cademic \underline{LA}nguage''}\smallskip\\ \mbox{}\hfill\textit{ --- a joke read on Twitter}\bigskip \noindent Scala is a programming language that combines functional and object-oriented programming-styles. It has received quite a bit of -attention in the last five or so years. One reason for this attention -is that, like the Java programming language, Scala compiles to the -Java Virtual Machine (JVM) and therefore Scala programs can run under -MacOSX, Linux and Windows.\footnote{There are also experimental - backends for Android and JavaScript; and also work is under way to - have a native compiler, see - \url{https://github.com/scala-native/scala-native}.} Unlike Java, -however, Scala often allows programmers to write very concise and -elegant code. Some therefore say: ``Scala is the better -Java''.\footnote{\url{https://www.slideshare.net/maximnovak/joy-of-scala}} -Also a number of companies (the Guardian, Twitter, Coursera, -FourSquare, LinkedIn to name a few) either use Scala exclusively in -production code, or at least to some substantial degree. Scala seems -also to be useful in job-interviews (in Data Science) according to -this anecdotal report +attention in the last five or so years. One reason for this attention is +that, like the Java programming language, Scala compiles to the Java +Virtual Machine (JVM) and therefore Scala programs can run under MacOSX, +Linux and Windows.\footnote{There are also experimental backends for +producing code under Android (\url{http://scala-android.org}); and also +for generating JavaScript code to build browser applications +\url{(https://www.scala-js.org)}. Moreover there is work under way to +have a native Scala compiler generating X86-code +(\url{http://www.scala-native.org}).} It therefore has also access to +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{form +\url{https://www.slideshare.net/maximnovak/joy-of-scala}} Also 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 to be useful in +job-interviews (especially in Data Science) according to this anecdotal +report -\begin{quote}\small -\url{https://techcrunch.com/2016/06/14/scala-is-the-new-golden-child/} +\begin{quote} +\url{http://techcrunch.com/2016/06/14/scala-is-the-new-golden-child} \end{quote} \noindent @@ -48,41 +52,75 @@ \end{quote} \noindent -A ready-made bundle with the Eclipse IDE is at +I found a convenient IDE for Scala programming is Microsoft's +\textit{Visual Studio Code} (VS Code) which runs under MacOSX, Linux and +obviously Windows. It can be downloaded for free from + +\begin{quote} +\url{https://code.visualstudio.com} +\end{quote} + +\noindent +and should already come pre-installed in the Department (together with +the Scala compiler). VS Code includes a \textit{Marketplace} from which +a multitude of extensions can be downloaded that make editing and +running Scala code easier (see Figure~\ref{vscode}). + +\begin{figure}[t] +\begin{center} +\includegraphics[scale=0.15]{../pics/vscode.png}\\[-10mm]\mbox{} +\end{center} +\caption{My personal installation of VS Code includes the following packages +from Marketplace: Scala Syntax (official), Code Runner, Code Spell Checker, +Rewrap and Subtle Match Brackets. I have also bound keys \keys{\^{}} \keys{Ret} +to the action ``Run-Selected-Text-In-Active-Terminal'' in order to +quickly evaluate small code snippets in the Scala REPL.\label{vscode}} +\end{figure} + +What I like most about VS Code is that it provides an easy access to the +Scala REPL. But if you prefer your own editor for coding, it +is also easy to work with Scala completely on the command line (like you +might have done with \texttt{g++} in the earlier part of PEP). For the +lazybones among us, there is even an online editor and environment for +developing and running Scala programs called \textit{ScalaFiddle}, which +requires zero setup (assuming you have a browser handy) + +\begin{quote} +\url{https://scalafiddle.io} +\end{quote} + + +Scala can also be used with the heavy-duty IDEs Eclipse and IntelliJ. +A ready-made Scala bundle for Eclipse is available at \begin{quote} \url{http://scala-ide.org/download/sdk.html} \end{quote} \noindent -When developing Scala programs, I personally prefer to use Emacs -or Sublime as my environment, since they provide an easy access -to the Scala REPL (see below). But it is also possible to work -completely on the command line and also with heavy-duty IDEs -like Eclipse of IntelliJ. There is even an online editor and -environment for developing Scala programs called ScalaFiddle - -\begin{quote} -\url{https://scalafiddle.io} -\end{quote} - - - +Also IntelliJ includes plugins for Scala. \textbf{BUT}, I do not +recommend the usage of Eclipse or IntelliJ for PEP: these IDEs seem to +make your life harder, rather than easier, for the small programs we +will write in this module. They are really meant to be used when you +have a million-lines codebase, rather than our ``toy-programs''\ldots{}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? ;o) \subsection*{The Very Basics} One advantage of Scala over Java is that it includes an interpreter (a REPL, or \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 +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 the preferred way of writing small Scala +programs. This is really my preferred way of writing small Scala programs. Once you installed Scala, you can start the interpreter by typing on the command line: \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] $ scala -Welcome to Scala 2.12.4 (Java HotSpot(TM) 64-Bit Server VM, Java 9). +Welcome to Scala 2.12.6 (Java HotSpot(TM) 64-Bit Server VM, Java 9). Type in expressions for evaluation. Or try :help. scala> @@ -101,7 +139,15 @@ \noindent indicating that the 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. Another classic example you can try out is +on. + +\begin{lstlisting}[numbers=none] +scala> res0 + 4 +res1: Int = 9 +\end{lstlisting} + +\noindent +Another classic example you can try out is \begin{lstlisting}[numbers=none] scala> print("hello world") @@ -120,8 +166,8 @@ now, the latter kind of functions always has \code{Unit} as return type. It is just not printed. -You can try more examples with the Scala interpreter, but try -first to guess what the result is (not all answers by Scala are obvious): +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): \begin{lstlisting}[numbers=none] scala> 2 + 2 @@ -134,9 +180,16 @@ scala> true && false scala> 1 > 1.0 scala> "12345".length -\end{lstlisting} +scala> List(1,2,1).size +scala> Set(1,2,1).size +\end{lstlisting}\smallskip -\subsection*{Stand-Alone Scala Apps} +\noindent +Please take the Scala REPL seriously: If you want to take advantage of my +reference implementation for the assignments, you will need to be +able to ``play around'' with it! + +\subsection*{Standalone Scala Apps} If you want to write a stand-alone app in Scala, you can implement an object that is an instance of \code{App}, say @@ -148,7 +201,8 @@ \end{lstlisting} \noindent save it in a file, for example {\tt hello-world.scala}, and -then run the compiler and runtime environment: +then run the compiler (\texttt{scalac}) and followed by the runtime +environment (\texttt{scala}): \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] $ scalac hello-world.scala @@ -191,8 +245,8 @@ \end{lstlisting} \noindent -Why the kerfuffle about values? Well, values are \emph{immutable}. You cannot -change their value after you defined them. If you try to reassign +Why the kerfuffle about values? Well, values are \emph{immutable}. You +cannot change their value after you defined them. If you try to reassign \code{z} above, Scala will yell at you: \begin{lstlisting}[numbers=none] @@ -204,7 +258,7 @@ \noindent So it would be a bit absurd to call values as variables...you cannot -change them. You might think you can re-assign them like +change them; they cannot vary. You might think you can re-assign them like \begin{lstlisting}[numbers=none] scala> val x = 42 @@ -221,18 +275,18 @@ \subsection*{Function Definitions} -We do functional programming. So defining functions will be our main occupation. -A function \code{f} taking a single argument of type \code{Int} can be defined in Scala -as follows: +We do functional programming! So defining functions will be our main occupation. +A function named, lets say, \code{f} taking a single argument of type +\code{Int} can be defined in Scala as follows: \begin{lstlisting}[numbers=none] -def f(x: Int) : String = EXPR +def f(x: Int) : String = ...EXPR... \end{lstlisting} \noindent This function returns the value resulting from evaluating the expression \code{EXPR} (whatever is substituted for this). The result will be -of type \code{String}. It is a good habbit to include this information +of type \code{String}. It is a good habit to include this information about the return type always. Simple examples of Scala functions are: \begin{lstlisting}[numbers=none] @@ -627,7 +681,7 @@ take as input an integer and produce a string like above). This means we cannot fix the type of the generic traversal functions, but have to keep them -\emph{polymorphic}.\footnote{Another interestic topic about +\emph{polymorphic}.\footnote{Another interesting topic about types, but we omit it here for the sake of brevity.} There is one more type constructor that is rather special. It @@ -828,6 +882,8 @@ \bigskip\noindent \textit{More TBD.} +\subsection*{Coursework} + \subsection*{More Info} There is much more to Scala than I can possibly describe in diff -r 35b4dc785a40 -r 31ba76ce016d pics/vscode.png Binary file pics/vscode.png has changed