| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Thu, 10 Nov 2022 23:44:35 +0000 | |
| changeset 895 | 550676e542d2 | 
| parent 870 | 1ea379515c6d | 
| child 917 | d3d371ae5fab | 
| permissions | -rw-r--r-- | 
| 754 | 1  | 
\documentclass{article}
 | 
2  | 
\usepackage{../style}
 | 
|
3  | 
\usepackage{../langs}
 | 
|
4  | 
\usepackage{marvosym}
 | 
|
5  | 
||
6  | 
%cheat sheet  | 
|
7  | 
%http://worldline.github.io/scala-cheatsheet/  | 
|
8  | 
||
9  | 
\begin{document}
 | 
|
| 838 | 10  | 
\fnote{\copyright{} Christian Urban, King's College London, 2020, 2021}
 | 
| 754 | 11  | 
|
12  | 
\section*{Scala in 6CCS3CFL}
 | 
|
13  | 
||
14  | 
For the coursework in this module you are free to use any programming  | 
|
| 756 | 15  | 
language you like, but I will show you all my code using Scala---I  | 
| 830 | 16  | 
hope you have fond memories of Scala from PEP. If you need a reminder  | 
17  | 
of the Scala handouts for PEP have a look  | 
|
18  | 
\hr{http://talisker.nms.kcl.ac.uk/cgi-bin/repos.cgi/pep-material/raw-file/tip/handouts/pep-ho.pdf}
 | 
|
19  | 
||
20  | 
||
21  | 
But as said, you do not need to use Scala for your own  | 
|
22  | 
code.\footnote{Haskell, Rust, Ocaml were other languages that have
 | 
|
23  | 
been used previously in CFL. I recommend to not use Java or C for  | 
|
24  | 
writing a compiler, but if you insist, feel free. It has been done  | 
|
25  | 
before.} I will use the  | 
|
26  | 
current stable version of Scala, which is 2.13.6. For various reasons,  | 
|
27  | 
I am NOT GOING TO USE THE LATEST VERSION OF SCALA 3.0! Please be  | 
|
28  | 
aware of this when you run my code.  | 
|
29  | 
\bigskip  | 
|
| 754 | 30  | 
|
31  | 
\noindent  | 
|
32  | 
The main difference to the Scala I showed you in PEP is that in CFL  | 
|
33  | 
I will use the Ammonite REPL  | 
|
34  | 
||
35  | 
\begin{quote}
 | 
|
36  | 
\url{https://ammonite.io/#Ammonite-REPL}
 | 
|
37  | 
\end{quote}
 | 
|
38  | 
||
39  | 
\noindent  | 
|
40  | 
This is a drop-in replacement for the original Scala REPL and  | 
|
41  | 
works very similarly, for example  | 
|
42  | 
||
43  | 
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
 | 
|
44  | 
$ amm  | 
|
45  | 
Loading...  | 
|
| 838 | 46  | 
Welcome to the Ammonite Repl 2.4.0 (Scala 2.13.6 Java 9)  | 
| 830 | 47  | 
scala> 1 + 2  | 
| 754 | 48  | 
res0: Int = 3  | 
49  | 
\end{lstlisting} %% $
 | 
|
50  | 
||
51  | 
\noindent  | 
|
52  | 
Ammonite uses the same Scala compiler, just adds some useful features  | 
|
53  | 
on top of it. It is quite main-stream in the Scala community and it should  | 
|
| 838 | 54  | 
therefore be very easy for you to install \texttt{amm}. If you work under
 | 
55  | 
a Unix-like system, a sure way to install the right version of Ammonite  | 
|
56  | 
is by using \texttt{curl}:
 | 
|
57  | 
||
58  | 
\begin{lstlisting}[numbers=none,language={},basicstyle=\ttfamily\small]
 | 
|
59  | 
$ curl -L https://github.com/com-lihaoyi/Ammonite/  | 
|
60  | 
releases/download/2.4.0/2.13-2.4.0 --output amm  | 
|
61  | 
\end{lstlisting}  
 | 
|
62  | 
||
| 759 | 63  | 
|
64  | 
The big advantage of Ammonite is that it comes with some additional  | 
|
65  | 
libraries already built-in and also allows one to easily break up code  | 
|
66  | 
into smaller modules. For example reading and writing files in  | 
|
67  | 
Ammonite can be achieved with  | 
|
| 754 | 68  | 
|
69  | 
\begin{lstlisting}[numbers=none,language=Scala]
 | 
|
| 
869
 
16247acc4b0e
changed os-lib as a replacement for ammonite-ops
 
Christian Urban <christian.urban@kcl.ac.uk> 
parents: 
838 
diff
changeset
 | 
70  | 
scala> import os._  | 
| 754 | 71  | 
|
72  | 
scala> read(pwd / "file.name")  | 
|
73  | 
res1: String = """..."""  | 
|
74  | 
||
75  | 
scala> write.over(pwd / "file.name", "foo bar")  | 
|
76  | 
\end{lstlisting}
 | 
|
77  | 
||
78  | 
\noindent  | 
|
| 759 | 79  | 
The latter writes the string \code{"foo bar"} into the file
 | 
80  | 
\code{"file.name"}, which is located in the current working
 | 
|
81  | 
directory. For loading and accessing code from another Scala file, you  | 
|
82  | 
can import it as follows:  | 
|
| 754 | 83  | 
|
84  | 
\begin{lstlisting}[numbers=none,language=Scala]
 | 
|
85  | 
import $file.name-of-the-file  | 
|
86  | 
import name-of-the-file._  | 
|
87  | 
\end{lstlisting}  %% $
 | 
|
88  | 
||
89  | 
\noindent  | 
|
90  | 
This assumes the other Scala file is called  | 
|
91  | 
\texttt{name-of-the-file.sc} and requires the file to be in the same
 | 
|
92  | 
directory where \texttt{amm} is working in. This will be very convenient 
 | 
|
93  | 
for the compiler we implement in CFL, because it allows us to easily  | 
|
| 760 | 94  | 
break up the code into the lexer, parser and code generator.  | 
| 754 | 95  | 
|
96  | 
Another feature which exists in Ammonite, but not yet in the  | 
|
97  | 
current version of Scala (it will be in the next version called dotty)  | 
|
98  | 
is that you can mark functions as \texttt{@main}. For example
 | 
|
99  | 
||
100  | 
\begin{lstlisting}[numbers=none,language=Scala]
 | 
|
101  | 
@main  | 
|
102  | 
def foo() = ...  | 
|
103  | 
\end{lstlisting}
 | 
|
104  | 
||
105  | 
\noindent  | 
|
106  | 
This means you can now call that function from the command line like  | 
|
107  | 
||
108  | 
\begin{lstlisting}[numbers=none,language=Scala]
 | 
|
109  | 
$ amm file.sc foo  | 
|
110  | 
\end{lstlisting} %% $
 | 
|
111  | 
||
112  | 
\noindent  | 
|
113  | 
If you want to specify an argument on the commandline, say an int and  | 
|
114  | 
a string, then you can write  | 
|
115  | 
||
116  | 
\begin{lstlisting}[numbers=none,language=Scala]
 | 
|
117  | 
@main  | 
|
118  | 
def bar(i: Int, s: String) = ...  | 
|
119  | 
\end{lstlisting}
 | 
|
120  | 
||
121  | 
\noindent  | 
|
122  | 
and then call  | 
|
123  | 
||
124  | 
\begin{lstlisting}[numbers=none,language=Scala]
 | 
|
125  | 
$ amm file.sc 42 foobar  | 
|
126  | 
\end{lstlisting} %% $
 | 
|
127  | 
||
128  | 
\noindent  | 
|
129  | 
What is also good in Ammonite that you can specify more than one  | 
|
130  | 
function to be ``main'' and then specify on the command line which  | 
|
| 760 | 131  | 
function you want to run as entry-point.  | 
132  | 
||
133  | 
Another feature you might like to use is that Ammonite can ``watch'' files.  | 
|
134  | 
This means it can automatically re-run a file when it is saved. For this  | 
|
135  | 
you have to call \texttt{amm} with the option \texttt{-w}, as in
 | 
|
136  | 
||
137  | 
\begin{lstlisting}[numbers=none,language=Scala]
 | 
|
138  | 
$ amm -w file.sc  | 
|
139  | 
\end{lstlisting} %% $
 | 
|
140  | 
||
141  | 
\noindent Of course this requires that you use \texttt{println} for
 | 
|
142  | 
inspecting any data, as otherwise nothing will be displayed at the  | 
|
143  | 
commandline.  | 
|
144  | 
\bigskip  | 
|
| 754 | 145  | 
|
146  | 
\noindent  | 
|
147  | 
To sum up, Ammonite is a really useful addition to the Scala ecosystem.  | 
|
148  | 
You can find more information about how to use it in the first five chapters  | 
|
149  | 
of the ``Hands-on Scala'' book by Li Haoyi. These chapters are  | 
|
150  | 
free and can be used as a reference, see:  | 
|
151  | 
||
152  | 
\begin{center}
 | 
|
153  | 
\url{https://www.handsonscala.com/part-i-introduction-to-scala.html}
 | 
|
154  | 
\end{center}
 | 
|
155  | 
||
156  | 
\end{document}
 | 
|
157  | 
||
158  | 
%%% Local Variables:  | 
|
159  | 
%%% mode: latex  | 
|
160  | 
%%% TeX-master: t  | 
|
161  | 
%%% End:  |