handouts/scala-ho.tex
changeset 405 30dd644ba71a
parent 402 55f097ab96c9
child 430 e0492fe3d10b
equal deleted inserted replaced
404:245d302791c7 405:30dd644ba71a
     9 \begin{document}
     9 \begin{document}
    10 
    10 
    11 \section*{A Crash-Course on Scala}
    11 \section*{A Crash-Course on Scala}
    12 
    12 
    13 Scala is a programming language that combines functional and
    13 Scala is a programming language that combines functional and
    14 object-oriented programming-styles. It has received in the
    14 object-oriented programming-styles. It has received quite a
    15 last five years or so quite a bit of attention. One reason for
    15 bit of attention in the last five years or so. One reason for
    16 this attention is that, like the Java programming language,
    16 this attention is that, like the Java programming language,
    17 Scala compiles to the Java Virtual Machine (JVM) and therefore
    17 Scala compiles to the Java Virtual Machine (JVM) and therefore
    18 Scala programs can run under MacOSX, Linux and
    18 Scala programs can run under MacOSX, Linux and
    19 Windows.\footnote{There are also experimental backends for
    19 Windows.\footnote{There are also experimental backends for
    20 Android and JavaScript.} Unlike Java, however, Scala often
    20 Android and JavaScript; and also work is under way to have a
       
    21 native compiler, see \url{https://github.com/scala-native/scala-native}.} Unlike Java, however, Scala often
    21 allows programmers to write very concise and elegant code.
    22 allows programmers to write very concise and elegant code.
    22 Some therefore say Scala is the much better Java. A number of
    23 Some therefore say: Scala is the much better Java. A number of
    23 companies, The Guardian, Twitter, Coursera, FourSquare,
    24 companies, The Guardian, Twitter, Coursera, FourSquare,
    24 LinkedIn to name a few, either use Scala exclusively in
    25 LinkedIn to name a few, either use Scala exclusively in
    25 production code, or at least to some substantial degree. If
    26 production code, or at least to some substantial degree. It
    26 you want to try out Scala yourself, the Scala compiler can be
    27 also seems to be useful in job-interviews (in Data Science)
       
    28 according to this annectotical report
       
    29 
       
    30 \begin{quote}
       
    31 \url{https://techcrunch.com/2016/06/14/scala-is-the-new-golden-child/}
       
    32 \end{quote}
       
    33 
       
    34 \noindent
       
    35 If you want to try out Scala yourself, the official Scala compiler can be
    27 downloaded from
    36 downloaded from
    28 
    37 
    29 \begin{quote}
    38 \begin{quote}
    30 \url{http://www.scala-lang.org}
    39 \url{http://www.scala-lang.org}
    31 \end{quote}
    40 \end{quote}
    38 code-snippets. If I had to do this in Java, I would first have
    47 code-snippets. If I had to do this in Java, I would first have
    39 to go through heaps of boilerplate code and the code-snippets
    48 to go through heaps of boilerplate code and the code-snippets
    40 would not look pretty. Since the Scala compiler is free, you
    49 would not look pretty. Since the Scala compiler is free, you
    41 can download the code-snippets and run every example I give.
    50 can download the code-snippets and run every example I give.
    42 But if you prefer, you can also easily translate them into any
    51 But if you prefer, you can also easily translate them into any
    43 other functional language, for example Haskell, Standard ML,
    52 other functional language, for example Haskell, Swift,
    44 F$^\#$, Ocaml and so on.
    53 Standard ML, F$^\#$, Ocaml and so on.
    45 
    54 
    46 Developing programs in Scala can be done with the Eclipse IDE
    55 Developing programs in Scala can be done with the Eclipse IDE
    47 and also with IntelliJ IDE, but for the small programs I will
    56 and also with the IntelliJ IDE, but for the small programs I will
    48 develop the good old Emacs-editor is adequate for me and I
    57 develop the good old Emacs-editor is adequate for me and I
    49 will run the programs on the command line. One advantage of
    58 will run the programs on the command line. One advantage of
    50 Scala over Java is that it includes an interpreter (a REPL, or
    59 Scala over Java is that it includes an interpreter (a REPL, or
    51 \underline{R}ead-\underline{E}val-\underline{P}rint-\underline{L}oop)
    60 \underline{R}ead-\underline{E}val-\underline{P}rint-\underline{L}oop)
    52 with which you can run and test small code-snippets without
    61 with which you can run and test small code-snippets without
    60 Type in expressions for evaluation. Or try :help.
    69 Type in expressions for evaluation. Or try :help.
    61 
    70 
    62 scala>
    71 scala>
    63 \end{lstlisting}
    72 \end{lstlisting}
    64 
    73 
    65 \noindent The precise response may vary due to the platform
    74 \noindent Of course the precise response may vary due to the
    66 where you installed Scala. At the Scala prompt you can type
    75 version and platform where you installed Scala. At the Scala
    67 things like \code{2 + 3} \keys{Ret} and the output will be
    76 prompt you can type things like \code{2 + 3} \keys{Ret} and
       
    77 the output will be
    68 
    78 
    69 \begin{lstlisting}[numbers=none]
    79 \begin{lstlisting}[numbers=none]
    70 scala> 2 + 3
    80 scala> 2 + 3
    71 res0: Int = 5
    81 res0: Int = 5
    72 \end{lstlisting}
    82 \end{lstlisting}
   195 scala> val r = ALT(CHAR('a'), CHAR('b'))
   205 scala> val r = ALT(CHAR('a'), CHAR('b'))
   196 r: ALT = ALT(CHAR(a),CHAR(b))
   206 r: ALT = ALT(CHAR(a),CHAR(b))
   197 \end{lstlisting}
   207 \end{lstlisting}
   198 
   208 
   199 \noindent As you can see, in order to make such assignments,
   209 \noindent As you can see, in order to make such assignments,
   200 no constructor is required in the class (as in Java). However,
   210 no \code{new} or constructor is required in the class (as in
   201 if there is the need for some non-standard initialisation, you
   211 Java). However, if there is the need for some non-standard
   202 can of course define such a constructor in Scala too. But we
   212 initialisation, you can of course define such a constructor in
   203 omit such ``tricks'' here. 
   213 Scala too. But we omit such ``tricks'' here. 
   204 
   214 
   205 Note that Scala in its response says the variable \code{r} is
   215 Note that Scala in its response says the variable \code{r} is
   206 of type \code{ALT}, not \code{Rexp}. This might be a bit
   216 of type \code{ALT}, not \code{Rexp}. This might be a bit
   207 unexpected, but can be explained as follows: Scala always
   217 unexpected, but can be explained as follows: Scala always
   208 tries to find the most general type that is needed for a
   218 tries to find the most general type that is needed for a
   209 variable or expression, but does not ``over-generalise''. In
   219 variable or expression, but does not ``over-generalise''. In
   210 our definition the type \code{Rexp} is more general than
   220 our definition the type \code{Rexp} is more general than
   211 \code{Rexp}, since it is the abstract class. But in this case
   221 \code{ALT}, since it is the abstract class for all regular
   212 there is no need to give \code{r} the more general type of
   222 expressions. But in this particular case there is no need to
   213 \code{Rexp}. This is different if you want
   223 give \code{r} the more general type of \code{Rexp}. This is
   214 to form a list of regular expressions, for example
   224 different if you want to form a list of regular expressions,
       
   225 for example
   215 
   226 
   216 \begin{lstlisting}[numbers=none]
   227 \begin{lstlisting}[numbers=none]
   217 scala> val ls = List(ALT(CHAR('a'), CHAR('b')), ZERO)
   228 scala> val ls = List(ALT(CHAR('a'), CHAR('b')), ZERO)
   218 ls: List[Rexp] = List(ALT(CHAR(a),CHAR(b)), ZERO)
   229 ls: List[Rexp] = List(ALT(CHAR(a),CHAR(b)), ZERO)
   219 \end{lstlisting}
   230 \end{lstlisting}
   358 Line 4 indicates that we do not care what the pattern looks
   369 Line 4 indicates that we do not care what the pattern looks
   359 like. Thus this case acts like a default case whenever the
   370 like. Thus this case acts like a default case whenever the
   360 cases above did not match. Cases are always tried out from top
   371 cases above did not match. Cases are always tried out from top
   361 to bottom.
   372 to bottom.
   362  
   373  
   363 \subsection*{Loops, or the Absence thereof}
   374 \subsection*{Loops, or better the Absence thereof}
   364 
   375 
   365 Coming from Java or C, you might be surprised that Scala does
   376 Coming from Java or C, you might be surprised that Scala does
   366 not really have loops. It has instead, what is in functional
   377 not really have loops. It has instead, what is in functional
   367 programming called, \emph{maps}. To illustrate how they work,
   378 programming called, \emph{maps}. To illustrate how they work,
   368 let us assume you have a list of numbers from 1 to 8 and want to
   379 let us assume you have a list of numbers from 1 to 8 and want to
   931 \begin{itemize}
   942 \begin{itemize}
   932 \item \url{http://www.scala-lang.org/docu/files/ScalaByExample.pdf}
   943 \item \url{http://www.scala-lang.org/docu/files/ScalaByExample.pdf}
   933 \item \url{http://www.scala-lang.org/docu/files/ScalaTutorial.pdf}
   944 \item \url{http://www.scala-lang.org/docu/files/ScalaTutorial.pdf}
   934 \item \url{https://www.youtube.com/user/ShadowofCatron}
   945 \item \url{https://www.youtube.com/user/ShadowofCatron}
   935 \item \url{http://docs.scala-lang.org/tutorials}
   946 \item \url{http://docs.scala-lang.org/tutorials}
       
   947 \item \url{https://www.scala-exercises.org}
   936 \end{itemize}
   948 \end{itemize}
   937 
   949 
   938 \noindent There is also a course at Coursera on Functional
   950 \noindent There is also a course at Coursera on Functional
   939 Programming Principles in Scala by Martin Odersky, the main
   951 Programming Principles in Scala by Martin Odersky, the main
   940 developer of the Scala language. And a document that explains
   952 developer of the Scala language. And a document that explains