\documentclass{article}\usepackage{../style}\usepackage{../langs}\usepackage{marvosym}%cheat sheet%http://worldline.github.io/scala-cheatsheet/\begin{document}\section*{Scala in 6CCS3CFL}For the coursework in this module you are free to use any programminglanguage you like, but I will show you all my code using Scala---I hopeyou have fond memories of Scala from PEP. I will use the currentstable version of Scala, which is 2.13.3. If you need a reminder ofthe Scala handouts for PEP have a look\hr{http://talisker.nms.kcl.ac.uk/cgi-bin/repos.cgi/pep-material/raw-file/tip/handouts/pep-ho.pdf}\bigskip\noindentThe main difference to the Scala I showed you in PEP is that in CFLI will use the Ammonite REPL\begin{quote}\url{https://ammonite.io/#Ammonite-REPL}\end{quote}\noindentThis is a drop-in replacement for the original Scala REPL andworks very similarly, for example\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]$ ammLoading... Welcome to the Ammonite Repl 2.2.0 (Scala 2.13.3 Java 9)scala> 1 + 2 res0: Int = 3\end{lstlisting} %% $\noindentAmmonite uses the same Scala compiler, just adds some useful featureson top of it. It is quite main-stream in the Scala community and it shouldtherefore be very easy for you to install \texttt{amm}. The bigadvantage of Ammonite is that it comes with some additional librariesalready built-in and also allows one to easily break up code intosmaller modules. For example reading and writing files in Ammonite canbe achieved with\begin{lstlisting}[numbers=none,language=Scala]scala> import ammonite.ops._scala> read(pwd / "file.name") res1: String = """..."""scala> write.over(pwd / "file.name", "foo bar")\end{lstlisting}\noindentFor loading and accessing code from another Scala file, you can import itas follows:\begin{lstlisting}[numbers=none,language=Scala]import $file.name-of-the-fileimport name-of-the-file._\end{lstlisting} %% $\noindentThis assumes the other Scala file is called\texttt{name-of-the-file.sc} and requires the file to be in the samedirectory where \texttt{amm} is working in. This will be very convenient for the compiler we implement in CFL, because it allows us to easilybreak-up the code into the lexer, parser and code generator.Another feature which exists in Ammonite, but not yet in thecurrent version of Scala (it will be in the next version called dotty)is that you can mark functions as \texttt{@main}. For example\begin{lstlisting}[numbers=none,language=Scala]@maindef foo() = ...\end{lstlisting}\noindentThis means you can now call that function from the command line like\begin{lstlisting}[numbers=none,language=Scala]$ amm file.sc foo\end{lstlisting} %% $\noindentIf you want to specify an argument on the commandline, say an int anda string, then you can write\begin{lstlisting}[numbers=none,language=Scala]@maindef bar(i: Int, s: String) = ...\end{lstlisting}\noindentand then call\begin{lstlisting}[numbers=none,language=Scala]$ amm file.sc 42 foobar\end{lstlisting} %% $\noindentWhat is also good in Ammonite that you can specify more than onefunction to be ``main'' and then specify on the command line whichfunction do you want to run as entry-point.\bigskip\noindentTo sum up, Ammonite is a really useful addition to the Scala ecosystem.You can find more information about how to use it in the first five chaptersof the ``Hands-on Scala'' book by Li Haoyi. These chapters arefree and can be used as a reference, see:\begin{center}\url{https://www.handsonscala.com/part-i-introduction-to-scala.html}\end{center}\end{document}%%% Local Variables: %%% mode: latex%%% TeX-master: t%%% End: