|
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} |
|
10 |
|
11 \section*{Scala in 6CCS3CFL} |
|
12 |
|
13 For the coursework in this module you are free to use any programming |
|
14 language you like, but I will show you all my code using Scala---I hope |
|
15 you have fond memories of Scala from PEP. I will use the current |
|
16 stable version of Scala, which is 2.13.3. If you need a reminder of |
|
17 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}\bigskip |
|
19 |
|
20 \noindent |
|
21 The main difference to the Scala I showed you in PEP is that in CFL |
|
22 I will use the Ammonite REPL |
|
23 |
|
24 \begin{quote} |
|
25 \url{https://ammonite.io/#Ammonite-REPL} |
|
26 \end{quote} |
|
27 |
|
28 \noindent |
|
29 This is a drop-in replacement for the original Scala REPL and |
|
30 works very similarly, for example |
|
31 |
|
32 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small] |
|
33 $ amm |
|
34 Loading... |
|
35 Welcome to the Ammonite Repl 2.2.0 (Scala 2.13.3 Java 9) |
|
36 scala> 1 + 2 |
|
37 res0: Int = 3 |
|
38 \end{lstlisting} %% $ |
|
39 |
|
40 \noindent |
|
41 Ammonite uses the same Scala compiler, just adds some useful features |
|
42 on top of it. It is quite main-stream in the Scala community and it should |
|
43 therefore be very easy for you to install \texttt{amm}. The big |
|
44 advantage of Ammonite is that it comes with some additional libraries |
|
45 already built-in and also allows one to easily break up code into |
|
46 smaller modules. For example reading and writing files in Ammonite can |
|
47 be achieved with |
|
48 |
|
49 \begin{lstlisting}[numbers=none,language=Scala] |
|
50 scala> import ammonite.ops._ |
|
51 |
|
52 scala> read(pwd / "file.name") |
|
53 res1: String = """...""" |
|
54 |
|
55 scala> write.over(pwd / "file.name", "foo bar") |
|
56 \end{lstlisting} |
|
57 |
|
58 \noindent |
|
59 For loading and accessing code from another Scala file, you can import it |
|
60 as follows: |
|
61 |
|
62 \begin{lstlisting}[numbers=none,language=Scala] |
|
63 import $file.name-of-the-file |
|
64 import name-of-the-file._ |
|
65 \end{lstlisting} %% $ |
|
66 |
|
67 \noindent |
|
68 This assumes the other Scala file is called |
|
69 \texttt{name-of-the-file.sc} and requires the file to be in the same |
|
70 directory where \texttt{amm} is working in. This will be very convenient |
|
71 for the compiler we implement in CFL, because it allows us to easily |
|
72 break-up the code into the lexer, parser and code generator. |
|
73 |
|
74 Another feature which exists in Ammonite, but not yet in the |
|
75 current version of Scala (it will be in the next version called dotty) |
|
76 is that you can mark functions as \texttt{@main}. For example |
|
77 |
|
78 \begin{lstlisting}[numbers=none,language=Scala] |
|
79 @main |
|
80 def foo() = ... |
|
81 \end{lstlisting} |
|
82 |
|
83 \noindent |
|
84 This means you can now call that function from the command line like |
|
85 |
|
86 \begin{lstlisting}[numbers=none,language=Scala] |
|
87 $ amm file.sc foo |
|
88 \end{lstlisting} %% $ |
|
89 |
|
90 \noindent |
|
91 If you want to specify an argument on the commandline, say an int and |
|
92 a string, then you can write |
|
93 |
|
94 \begin{lstlisting}[numbers=none,language=Scala] |
|
95 @main |
|
96 def bar(i: Int, s: String) = ... |
|
97 \end{lstlisting} |
|
98 |
|
99 \noindent |
|
100 and then call |
|
101 |
|
102 \begin{lstlisting}[numbers=none,language=Scala] |
|
103 $ amm file.sc 42 foobar |
|
104 \end{lstlisting} %% $ |
|
105 |
|
106 \noindent |
|
107 What is also good in Ammonite that you can specify more than one |
|
108 function to be ``main'' and then specify on the command line which |
|
109 function do you want to run as entry-point.\bigskip |
|
110 |
|
111 \noindent |
|
112 To sum up, Ammonite is a really useful addition to the Scala ecosystem. |
|
113 You can find more information about how to use it in the first five chapters |
|
114 of the ``Hands-on Scala'' book by Li Haoyi. These chapters are |
|
115 free and can be used as a reference, see: |
|
116 |
|
117 \begin{center} |
|
118 \url{https://www.handsonscala.com/part-i-introduction-to-scala.html} |
|
119 \end{center} |
|
120 |
|
121 \end{document} |
|
122 |
|
123 %%% Local Variables: |
|
124 %%% mode: latex |
|
125 %%% TeX-master: t |
|
126 %%% End: |