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