17 |
17 |
18 \section*{A Crash-Course on Scala} |
18 \section*{A Crash-Course on Scala} |
19 |
19 |
20 Scala is a programming language that combines functional and |
20 Scala is a programming language that combines functional and |
21 object-oriented programming-styles, and has received in the |
21 object-oriented programming-styles, and has received in the |
22 last five years quite a bit of attention. One reason for this |
22 last five years or so quite a bit of attention. One reason for |
23 attention is that, like the Java programming language, Scala |
23 this attention is that, like the Java programming language, |
24 compiles to the Java Virtual Machine (JVM) and therefore can |
24 Scala compiles to the Java Virtual Machine (JVM) and therefore |
25 run under MacOSX, Linux and Windows.\footnote{There are also |
25 Scala programs can run under MacOSX, Linux and |
26 experimental backends for Android and JavaScript.} Unlike |
26 Windows.\footnote{There are also experimental backends for |
27 Java, however, Scala often allows programmers to write very |
27 Android and JavaScript.} Unlike Java, however, Scala often |
28 concise and elegant code. Some therefore say Scala is the much |
28 allows programmers to write very concise and elegant code. |
29 better Java. The Guardian, Twitter, Coursera, LinkedIn to name |
29 Some therefore say Scala is the much better Java. Some |
30 a few either rely entirely in their infrastructures on Scala, |
30 companies (The Guardian, Twitter, Coursera, LinkedIn to name a |
31 or some parts of there infrastructure uses it. If you want to |
31 few) either use Scala excusively in production code, or some |
32 try it out yourself, the Scala compiler can be downloaded from |
32 part of it are written in Scala. If you want to try out Scala |
|
33 yourself, the Scala compiler can be downloaded from |
33 |
34 |
34 \begin{quote} |
35 \begin{quote} |
35 \url{http://www.scala-lang.org} |
36 \url{http://www.scala-lang.org} |
36 \end{quote} |
37 \end{quote} |
37 |
38 |
52 will run the programs on the command line. One advantage of |
53 will run the programs on the command line. One advantage of |
53 Scala over Java is that it includes an interpreter (a REPL, or |
54 Scala over Java is that it includes an interpreter (a REPL, or |
54 Read-Eval-Print-Loop) with which you can run and test small |
55 Read-Eval-Print-Loop) with which you can run and test small |
55 code-snippets without the need of the compiler. This helps a |
56 code-snippets without the need of the compiler. This helps a |
56 lot with interactively developing programs. Once you installed |
57 lot with interactively developing programs. Once you installed |
57 Scala correctly, you can start the interpreter by typing |
58 Scala correctly, you can start the interpreter by typing on |
58 |
59 the command line: |
59 |
60 |
60 \begin{quote} |
61 \begin{quote} |
61 \begin{alltt} |
62 \begin{alltt} |
62 $ scala\small |
63 $ scala\small |
63 Welcome to Scala version 2.11.2 (Java HotSpot(TM) 64-Bit Server VM). |
64 Welcome to Scala version 2.11.2 (Java HotSpot(TM) 64-Bit Server VM). |
826 def | (r: Rexp) = ALT(s, r) |
827 def | (r: Rexp) = ALT(s, r) |
827 def | (r: String) = ALT(s, r) |
828 def | (r: String) = ALT(s, r) |
828 def ~ (r: Rexp) = SEQ(s, r) |
829 def ~ (r: Rexp) = SEQ(s, r) |
829 def ~ (r: String) = SEQ(s, r) |
830 def ~ (r: String) = SEQ(s, r) |
830 def % = STAR(s) |
831 def % = STAR(s) |
831 |
|
832 } |
832 } |
833 \end{lstlisting} |
833 \end{lstlisting} |
834 \end{quote} |
834 \end{quote} |
835 |
835 |
836 \noindent This might seem a bit complicated, but its effect is |
836 \noindent This might seem a bit overly complicated, but its effect is |
837 that I can now write regular expressions such as $ab + ac$ |
837 that I can now write regular expressions such as $ab + ac$ |
838 even simpler as |
838 even simpler as |
839 |
839 |
840 \begin{quote} |
840 \begin{quote} |
841 \begin{alltt} |
841 \begin{alltt} |
896 hand using only the ``plain'' regular expressions from the |
896 hand using only the ``plain'' regular expressions from the |
897 inductive datatype. |
897 inductive datatype. |
898 |
898 |
899 \subsection*{More Info} |
899 \subsection*{More Info} |
900 |
900 |
901 There is much more to Scala, then I can possibly describe |
901 There is much more to Scala than I can possibly describe in |
902 here. There are a number of free books about Scala and of |
902 this document. Fortunately there are a number of free books |
903 course lots of help online. |
903 about Scala and of course lots of help online. For example |
904 |
904 |
905 While I am quite enthusiastic about Scala, I am happy to admit |
905 \begin{itemize} |
906 that it has more than a fair share of faults. The problem of |
906 \item \url{http://www.scala-lang.org/docu/files/ScalaByExample.pdf} |
907 having to give an explicit type to {\tt toSet}, but not {\tt |
907 \item \url{http://www.scala-lang.org/docu/files/ScalaTutorial.pdf} |
908 toList} is one of them. There are also many ``deep'' things |
908 \end{itemize} |
909 about types in Scala, which even to me seasoned functional |
909 |
910 programmer are puzzling. While implicits are great, they |
910 While I am quite enthusiastic about Scala, I am also happy to |
911 can also be a source of great head-aches, for example |
911 admit that it has more than its fair share of faults. The |
|
912 problem seen earlier of having to give an explicit type to |
|
913 {\tt toSet}, but not {\tt toList} is one of them. There are |
|
914 also many ``deep'' ideas about types in Scala, which even to |
|
915 me as seasoned functional programmer are puzzling. Whilst |
|
916 implicits are great, they can also be a source of great |
|
917 headaches, for example consider the code: |
912 |
918 |
913 \begin{quote} |
919 \begin{quote} |
914 \begin{alltt} |
920 \begin{alltt} |
915 scala> List (1, 2, 3) contains "your mom" |
921 scala> List (1, 2, 3) contains "your mom" |
916 res1: Boolean = false |
922 res1: Boolean = false |
917 \end{alltt} |
923 \end{alltt} |
918 \end{quote} |
924 \end{quote} |
919 |
925 |
920 \noindent This should raise a typing-error, but it does not. |
926 \noindent Rather than returning {\tt false}, this code should |
921 |
927 throw a typing-error. There are also many limitations Scala |
922 While Scala has been a success in several high-profile |
928 inherited from the JVM that can be really annoying. For |
923 companies, there is also a company, Yammer, which first used |
929 example a fixed stack size. |
924 Scala in their production code but then moved away from it. |
930 |
925 According to Wikipedia, they did not like steep learning curve |
931 Even if Scala has been a success in several high-profile |
926 of Scala and also that new versions of Scala often introduced |
932 companies, there is also a company (Yammer) that first used |
927 incompatibilities. |
933 Scala in their production code, but then moved away from it. |
928 |
934 Allegedly they did not like the steep learning curve of Scala |
929 So all in all, Scala might not be a great teaching |
935 and also that new versions of Scala often introduced |
930 language, but I hope this is mitigated by the fact that I |
936 incompatibilities in old code. |
931 never require from you to write any Scala code. You only |
937 |
932 need to be able to read it. In the coursework you can use |
938 So all in all, Scala might not be a great teaching language, |
933 any programming language you like. If you want to use |
939 but I hope this is mitigated by the fact that I never require |
934 Scala for this, be my guest; if you do not, stick with the |
940 you to write any Scala code. You only need to be able to read |
935 language you are most familiar with. |
941 it. In the coursework you can use any programming language you |
|
942 like. If you want to use Scala for this, then be my guest; if |
|
943 you do not want, stick with the language you are most familiar |
|
944 with. |
936 |
945 |
937 \end{document} |
946 \end{document} |
938 |
947 |
939 %%% Local Variables: |
948 %%% Local Variables: |
940 %%% mode: latex |
949 %%% mode: latex |