754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
\documentclass{article}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
\usepackage{../style}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
\usepackage{../langs}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
\usepackage{marvosym}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
%cheat sheet
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
%http://worldline.github.io/scala-cheatsheet/
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
\begin{document}
|
755
|
10 |
\fnote{\copyright{} Christian Urban, King's College London, 2020}
|
754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
\section*{Scala in 6CCS3CFL}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
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
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
The main difference to the Scala I showed you in PEP is that in CFL
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
I will use the Ammonite REPL
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
\begin{quote}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
\url{https://ammonite.io/#Ammonite-REPL}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
\end{quote}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
This is a drop-in replacement for the original Scala REPL and
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
works very similarly, for example
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
\begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
$ amm
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
Loading...
|
830
|
46 |
Welcome to the Ammonite Repl 2.3.8 (Scala 2.13.3 Java 9)
|
|
47 |
scala> 1 + 2
|
754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
res0: Int = 3
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
\end{lstlisting} %% $
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
50 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
51 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
Ammonite uses the same Scala compiler, just adds some useful features
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
on top of it. It is quite main-stream in the Scala community and it should
|
759
|
54 |
therefore be very easy for you to install \texttt{amm}.
|
|
55 |
|
|
56 |
The big advantage of Ammonite is that it comes with some additional
|
|
57 |
libraries already built-in and also allows one to easily break up code
|
|
58 |
into smaller modules. For example reading and writing files in
|
|
59 |
Ammonite can be achieved with
|
754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
61 |
\begin{lstlisting}[numbers=none,language=Scala]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
scala> import ammonite.ops._
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
64 |
scala> read(pwd / "file.name")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
65 |
res1: String = """..."""
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
66 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
67 |
scala> write.over(pwd / "file.name", "foo bar")
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
68 |
\end{lstlisting}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
\noindent
|
759
|
71 |
The latter writes the string \code{"foo bar"} into the file
|
|
72 |
\code{"file.name"}, which is located in the current working
|
|
73 |
directory. For loading and accessing code from another Scala file, you
|
|
74 |
can import it as follows:
|
754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
75 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
76 |
\begin{lstlisting}[numbers=none,language=Scala]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
77 |
import $file.name-of-the-file
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
78 |
import name-of-the-file._
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
79 |
\end{lstlisting} %% $
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
80 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
81 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
82 |
This assumes the other Scala file is called
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
83 |
\texttt{name-of-the-file.sc} and requires the file to be in the same
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
84 |
directory where \texttt{amm} is working in. This will be very convenient
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
85 |
for the compiler we implement in CFL, because it allows us to easily
|
760
|
86 |
break up the code into the lexer, parser and code generator.
|
754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
87 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
Another feature which exists in Ammonite, but not yet in the
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
89 |
current version of Scala (it will be in the next version called dotty)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
is that you can mark functions as \texttt{@main}. For example
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
92 |
\begin{lstlisting}[numbers=none,language=Scala]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
93 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
94 |
def foo() = ...
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
95 |
\end{lstlisting}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
96 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
This means you can now call that function from the command line like
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
100 |
\begin{lstlisting}[numbers=none,language=Scala]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
101 |
$ amm file.sc foo
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
102 |
\end{lstlisting} %% $
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
103 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
104 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
105 |
If you want to specify an argument on the commandline, say an int and
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
106 |
a string, then you can write
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
107 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
108 |
\begin{lstlisting}[numbers=none,language=Scala]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
109 |
@main
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
110 |
def bar(i: Int, s: String) = ...
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
111 |
\end{lstlisting}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
112 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
113 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
114 |
and then call
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
115 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
116 |
\begin{lstlisting}[numbers=none,language=Scala]
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
117 |
$ amm file.sc 42 foobar
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
118 |
\end{lstlisting} %% $
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
119 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
120 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
121 |
What is also good in Ammonite that you can specify more than one
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
122 |
function to be ``main'' and then specify on the command line which
|
760
|
123 |
function you want to run as entry-point.
|
|
124 |
|
|
125 |
Another feature you might like to use is that Ammonite can ``watch'' files.
|
|
126 |
This means it can automatically re-run a file when it is saved. For this
|
|
127 |
you have to call \texttt{amm} with the option \texttt{-w}, as in
|
|
128 |
|
|
129 |
\begin{lstlisting}[numbers=none,language=Scala]
|
|
130 |
$ amm -w file.sc
|
|
131 |
\end{lstlisting} %% $
|
|
132 |
|
|
133 |
\noindent Of course this requires that you use \texttt{println} for
|
|
134 |
inspecting any data, as otherwise nothing will be displayed at the
|
|
135 |
commandline.
|
|
136 |
\bigskip
|
754
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
137 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
138 |
\noindent
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
139 |
To sum up, Ammonite is a really useful addition to the Scala ecosystem.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
140 |
You can find more information about how to use it in the first five chapters
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
141 |
of the ``Hands-on Scala'' book by Li Haoyi. These chapters are
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
142 |
free and can be used as a reference, see:
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
143 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
144 |
\begin{center}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
145 |
\url{https://www.handsonscala.com/part-i-introduction-to-scala.html}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
146 |
\end{center}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
147 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
148 |
\end{document}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
149 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
150 |
%%% Local Variables:
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
151 |
%%% mode: latex
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
152 |
%%% TeX-master: t
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
153 |
%%% End:
|