handouts/amm-ho.tex
changeset 917 89e05a230d2d
parent 870 739039774cee
child 924 6ad0f63e1968
equal deleted inserted replaced
916:10f834eb0a9e 917:89e05a230d2d
     1 \documentclass{article}
     1 \documentclass{article}
     2 \usepackage{../style}
     2 \usepackage{../style}
     3 \usepackage{../langs}
     3 \usepackage{../langs}
     4 \usepackage{marvosym}
     4 \usepackage{marvosym}
       
     5 \usepackage{tcolorbox}
       
     6 %% \usepackage[]{hvextern}
     5 
     7 
     6 %cheat sheet
     8 %cheat sheet
     7 %http://worldline.github.io/scala-cheatsheet/
     9 %http://worldline.github.io/scala-cheatsheet/
     8 
    10 
     9 \begin{document}
    11 \begin{document}
    10 \fnote{\copyright{} Christian Urban, King's College London, 2020, 2021}
    12 \fnote{\copyright{} Christian Urban, King's College London, 2020, 2021, 2023}
    11 
    13 
    12 \section*{Scala in 6CCS3CFL}
    14 \section*{Scala 3 in 6CCS3CFL}
    13 
    15 
    14 For the coursework in this module you are free to use any programming
    16 For the coursework in this module you are free to use any programming
    15 language you like, but I will show you all my code using Scala---I
    17 language you like, but I will show you all my code using Scala---I
    16 hope you have fond memories of Scala from PEP. If you need a reminder
    18 hope you have fond memories of Scala from PEP. The only difference
    17 of the Scala handouts for PEP have a look
    19 with PEP is that I will use the current
    18 \hr{http://talisker.nms.kcl.ac.uk/cgi-bin/repos.cgi/pep-material/raw-file/tip/handouts/pep-ho.pdf}
    20 stable version of Scala, which at teh time of writing is Scala 3.3.1.
    19 
    21 
       
    22 \begin{tcolorbox}[colback=red!5!white,colframe=red!75!black]
       
    23 If you intend to submit your code for the CW in Scala, you \underline{MUST} submit code that
       
    24 is compatible with Scala 3!! This is to make it easier for me to test
       
    25 your code and the changes between Scala 2 from last year PEP to Scala
       
    26 3 in CFL are not that great. In fact, most changes are just some new
       
    27 syntax.
       
    28 \end{tcolorbox}\medskip
    20 
    29 
    21 But as said, you do not need to use Scala for your own
    30 \noindent
    22 code.\footnote{Haskell, Rust, Ocaml were other languages that have
    31 If you need a reminder of the Scala handouts from PEP updated to Scala 3
    23   been used previously in CFL. I recommend to not use Java or C for
    32 have a look here
       
    33 \hr{http://talisker.nms.kcl.ac.uk/cgi-bin/repos.cgi/pep-material/raw-file/tip/handouts/pep-ho.pdf}. But as said, you do not need to use Scala for the CWs.\footnote{Haskell, Rust, Ocaml were other languages that have
       
    34   been used previously in CFL. I do not recommend to use Java or C or C++ for
    24   writing a compiler, but if you insist, feel free. It has been done
    35   writing a compiler, but if you insist, feel free. It has been done
    25 before.}  I will use the
    36   before.}  
    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
    37 \bigskip
    30 
    38 
    31 \noindent
    39 \noindent
    32 The main difference to the Scala I showed you in PEP is that in CFL
    40 The other difference between the Scala I showed you in PEP is that in CFL
    33 I will use the Ammonite REPL
    41 I will use the Ammonite REPL (with underlying Scala Version 3):
    34 
    42 
    35 \begin{quote}
    43 \begin{quote}
    36 \url{https://ammonite.io/#Ammonite-REPL}
    44 \url{https://ammonite.io/#Ammonite-REPL}
    37 \end{quote}
    45 \end{quote}
    38 
    46 
    40 This is a drop-in replacement for the original Scala REPL and
    48 This is a drop-in replacement for the original Scala REPL and
    41 works very similarly, for example
    49 works very similarly, for example
    42 
    50 
    43 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
    51 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
    44 $ amm
    52 $ amm
    45 Loading...  
    53 Loading...
    46 Welcome to the Ammonite Repl 2.4.0 (Scala 2.13.6 Java 9)
    54 Welcome to the Ammonite Repl 2.5.9 (Scala 3.2.2 Java 17.0.7)
    47 scala> 1 + 2
    55 scala> 1 + 2
    48 res0: Int = 3
    56 res0: Int = 3
    49 \end{lstlisting} %% $
    57 \end{lstlisting} %% $
       
    58 
       
    59 %%\runExtCmd[redirect]{ls -la}{voss}
       
    60 
    50 
    61 
    51 \noindent
    62 \noindent
    52 Ammonite uses the same Scala compiler, just adds some useful features
    63 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
    64 on top of it. It is quite main-stream in the Scala community and it should
    54 therefore be very easy for you to install \texttt{amm}. If you work under
    65 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
    66 a Unix-like system, a sure way to install the right version of Ammonite
    56 is by using \texttt{curl}:
    67 is by using \texttt{curl}:
    57 
    68 
    58 \begin{lstlisting}[numbers=none,language={},basicstyle=\ttfamily\small]
    69 \begin{lstlisting}[numbers=none,language={},basicstyle=\ttfamily\small]
    59 $ curl -L https://github.com/com-lihaoyi/Ammonite/
    70 $ curl -L https://github.com/com-lihaoyi/Ammonite/releases/\
    60                  releases/download/2.4.0/2.13-2.4.0 --output amm
    71   download/2.5.9/3.2-2.5.9 --output amm
    61 \end{lstlisting}  
    72 \end{lstlisting} %% $  
    62 
    73 
    63 
    74 \noindent
    64 The big advantage of Ammonite is that it comes with some additional
    75 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
    76 libraries already built-in and also allows one to easily break up code
    66 into smaller modules. For example reading and writing files in
    77 into smaller modules. For example reading and writing files in
    67 Ammonite can be achieved with
    78 Ammonite can be achieved with
    68 
    79 
    69 \begin{lstlisting}[numbers=none,language=Scala]
    80 \begin{lstlisting}[numbers=none,language=Scala]
    70 scala> import os._  
    81 scala> import os._
    71   
    82 scala> write.over(pwd / "file.name", "foo bar")  
    72 scala> read(pwd / "file.name")   
    83 scala> read(pwd / "file.name")   
    73 res1: String = """..."""
    84 res1: String = "foo bar"
    74 
       
    75 scala> write.over(pwd / "file.name", "foo bar")
       
    76 \end{lstlisting}
    85 \end{lstlisting}
    77 
    86 
    78 \noindent
    87 \noindent
    79 The latter writes the string \code{"foo bar"} into the file
    88 The second line writes the string \code{"foo bar"} into the file
    80 \code{"file.name"}, which is located in the current working
    89 \code{"file.name"}, which is located in the current working
    81 directory. For loading and accessing code from another Scala file, you
    90 directory (\code{pwd}). For loading and accessing code from
    82 can import it as follows:
    91 another Scala file, you can import the code into Ammonite
       
    92 as follows:
    83 
    93 
    84 \begin{lstlisting}[numbers=none,language=Scala]
    94 \begin{lstlisting}[numbers=none,language=Scala]
    85 import $file.name-of-the-file
    95 import $file.name-of-the-file
    86 import name-of-the-file._
    96 import name-of-the-file._
    87 \end{lstlisting}  %% $
    97 \end{lstlisting}  %% $
    91 \texttt{name-of-the-file.sc} and requires the file to be in the same
   101 \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 
   102 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
   103 for the compiler we implement in CFL, because it allows us to easily
    94 break up the code into the lexer, parser and code generator.
   104 break up the code into the lexer, parser and code generator.
    95 
   105 
    96 Another feature which exists in Ammonite, but not yet in the
   106 Another handy feature of Ammonite is that you can mark functions as
    97 current version of Scala (it will be in the next version called dotty)
   107 \texttt{@main}. For example
    98 is that you can mark functions as \texttt{@main}. For example
       
    99 
   108 
   100 \begin{lstlisting}[numbers=none,language=Scala]
   109 \begin{lstlisting}[numbers=none,language=Scala]
   101 @main
   110 @main
   102 def foo() = ...
   111 def foo() = ...
   103 \end{lstlisting}
   112 \end{lstlisting}
   124 \begin{lstlisting}[numbers=none,language=Scala]
   133 \begin{lstlisting}[numbers=none,language=Scala]
   125 $ amm file.sc 42 foobar
   134 $ amm file.sc 42 foobar
   126 \end{lstlisting} %% $
   135 \end{lstlisting} %% $
   127 
   136 
   128 \noindent
   137 \noindent
   129 What is also good in Ammonite that you can specify more than one
   138 What is also good in Ammonite is that you can specify more than one
   130 function to be ``main'' and then specify on the command line which
   139 function to be ``main'' and then specify on the command line which
   131 function you want to run as entry-point.
   140 function you want to run as entry-point.
   132 
   141 
   133 Another feature you might like to use is that Ammonite can ``watch'' files.
   142 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
   143 This means it can automatically re-run a file when it is saved. For this
   137 \begin{lstlisting}[numbers=none,language=Scala]
   146 \begin{lstlisting}[numbers=none,language=Scala]
   138 $ amm -w file.sc
   147 $ amm -w file.sc
   139 \end{lstlisting} %% $
   148 \end{lstlisting} %% $
   140 
   149 
   141 \noindent Of course this requires that you use \texttt{println} for
   150 \noindent Of course this requires that you use \texttt{println} for
   142 inspecting any data, as otherwise nothing will be displayed at the
   151 inspecting any data as otherwise nothing will be displayed at the
   143 commandline.
   152 commandline.
   144 \bigskip
   153 \bigskip
   145 
   154 
   146 \noindent
   155 \noindent
   147 To sum up, Ammonite is a really useful addition to the Scala ecosystem.
   156 To sum up, Ammonite is a really useful addition to the Scala ecosystem.