2014-03-10 16:18:58 +01:00
%!TEX root = Programmierparadigmen.tex
2014-02-01 13:46:33 +01:00
\chapter { Scala}
\index { Scala|(}
2014-02-01 14:39:16 +01:00
2014-03-07 19:17:17 +01:00
Scala ist eine objektorientierte und funktionale Programmiersprache, die auf der JVM aufbaut und in Java Bytecode kompiliert wird. Scala bedeutet \underline { sca} lable
2014-03-05 13:42:36 +01:00
\underline { la} nguage.
Mit sog. \enquote { actors} bietet Scala eine Unterstützung für die Entwicklung
prallel ausführender Programme.
Weitere Materialien sind unter \url { http://www.scala-lang.org/} und
\url { http://www.simplyscala.com/} zu finden.
\section { Erste Schritte}
Scala kann auf Debian-basierten Systemen durch das Paket \texttt { scala} installiert
2014-03-07 19:17:17 +01:00
werden. Für andere Systeme stehen auf \url { http://www.scala-lang.org/download/}
verschiedene Binärdateien bereit.
2014-03-05 13:42:36 +01:00
\subsection { Hello World}
\subsubsection { Interaktiv}
2014-03-07 19:17:17 +01:00
\inputminted [numbersep=5pt, tabsize=4] { bash} { scripts/scala/scala-test.scala}
2014-03-05 13:42:36 +01:00
Es kann mit \texttt { ./scala-test.scala Scala funktioniert} ausgeführt werden.
\subsubsection { Kompiliert}
\inputminted [linenos, numbersep=5pt, tabsize=4, frame=lines, label=hello-world.scala] { scala} { scripts/scala/hello-world.scala}
Dieses Beispiel kann mit \texttt { scalac hello-world.scala} kompiliert und mit
\texttt { scala HelloWorld} ausgeführt werden.
\section { Vergleich mit Java}
Scala und Java haben einige Gemeinsamkeiten, wie den Java Bytecode, aber auch
einige Unterschiede.
\noindent \parbox [t] { 2.4in} { \raggedright %
\textbf { \textit { Gemeinsamkeiten} }
\begin { itemize} [topsep=0pt,itemsep=-2pt,leftmargin=13pt]
\item Java Bytecode
\item Keine Mehrfachvererbung
\item Statische Typisierung
\item Scopes
\end { itemize}
} %
\parbox [t] { 2.4in} { \raggedright %
\textbf { \textit { Unterschiede} }
\begin { itemize} [topsep=0pt,itemsep=-2pt,leftmargin=13pt]
\item Java hat Interfaces, Scala hat traits.
\item Java hat primitive Typen, Scala ausschließlich Objekte.
\item Scala benötigt kein \texttt { ;} am Ende von Anweisungen.
2014-03-05 14:36:32 +01:00
\item Scala ist kompakter.
2014-03-10 15:39:59 +01:00
\item Java hat \texttt { static} , Scala hat \texttt { object} (Singleton)
2014-03-05 13:42:36 +01:00
\end { itemize}
}
2014-03-05 14:36:32 +01:00
Weitere Informationen hat Graham Lea unter \url { http://tinyurl.com/scala-hello-world} zur Verfügung gestellt.
2014-02-01 14:39:16 +01:00
2014-02-01 13:46:33 +01:00
\section { Syntax}
2014-09-13 17:26:09 +02:00
In Scala gibt es sog. \textit { values} , die durch das Schlüsselwort \texttt { val} \xindex { val (Scala)@\texttt { val} (Scala)}
2014-03-05 13:42:36 +01:00
angezeigt werden. Diese sind Konstanten. Die Syntax ist der UML-Syntax ähnlich.
2014-03-05 14:36:32 +01:00
\inputminted [numbersep=5pt, tabsize=4] { scala} { scripts/scala/val-syntax.scala}
2014-03-05 13:42:36 +01:00
2014-09-13 17:26:09 +02:00
Variablen werden durch das Schlüsselwort \texttt { var} \xindex { var (Scala)@\texttt { var} (Scala)} angezeigt:
2014-02-01 13:46:33 +01:00
2014-03-05 14:36:32 +01:00
\inputminted [numbersep=5pt, tabsize=4] { scala} { scripts/scala/var-syntax.scala}
2014-03-05 13:42:36 +01:00
2014-09-13 17:26:09 +02:00
Methoden werden mit dem Schlüsselwort \texttt { def} \xindex { def (def)@\texttt { def} (Scala)} erzeugt:
2014-03-05 13:42:36 +01:00
2014-03-05 14:36:32 +01:00
\inputminted [numbersep=5pt, tabsize=4] { scala} { scripts/scala/method-syntax.scala}
Klassen werden wie folgt erstellt:
\inputminted [numbersep=5pt, tabsize=4] { scala} { scripts/scala/simple-class-example.scala}
2014-03-07 19:17:17 +01:00
und so instanziiert:
\inputminted [numbersep=5pt, tabsize=4] { scala} { scripts/scala/simple-class-instanciation.scala}
2014-03-05 13:42:36 +01:00
2014-09-13 17:26:09 +02:00
\subsection { Schleifen} \xindex { for (Scala)@\texttt { for} (Scala)}
Eine einfache \texttt { for} -Schleife sieht wie folgt aus:
\inputminted [numbersep=5pt, tabsize=4] { scala} { scripts/scala/extended-for.scala}
2014-03-08 13:16:08 +01:00
Listen können erstellt und durchgegangen werden:
\inputminted [numbersep=5pt, tabsize=4] { scala} { scripts/scala/extended-for.scala}
2014-03-31 13:31:31 +02:00
\subsection { Logische Operatoren}
2014-09-13 17:26:09 +02:00
\begin { table} [H]
2014-03-31 13:31:31 +02:00
\centering
\begin { tabular} { CCCC}
UND & ODER & Wahr & Falsch \\ \hline \hline
\& \& & || & true & false \\ [4ex]
GLEICH & UNGLEICH & NICHT & ~ \\ \hline \hline
== & != & ! & ~ \\
\end { tabular}
\caption { Logische Operatoren in Scala} \xindex { Logische Operatoren!Scala}
\end { table}
2014-08-23 13:57:49 -04:00
\section { Datenstrukturen}
\subsection { Listen}
\begin { itemize}
\item Erstellt man mit \verb +var myList = List();+
\item Zugriff auf das \verb +i+-te Element mit \verb +myList(i)+
\end { itemize}
\subsection { Tupel}
\begin { itemize}
\item Erstellt man mit \verb +var myTuple = (el1, el2, el3)+
\item Zugriff auf das \verb +i+-te Element mit \verb +myTuple._ i+
\end { itemize}
2014-03-10 15:39:59 +01:00
\section { Companion Object} \xindex { Companion Object}
Ein Companion Object ist ein Objekt mit dem Namen einer Klasse oder eines Traits.
Im Gegensatz zu anderen Objekten / Traits hat das Companion Object zugriff auf
die Klasse.
2014-03-16 19:00:22 +01:00
\section { actor}
2014-09-18 12:08:55 +02:00
\begin { definition} [Aktor]\xindex { Aktor} \index { actor|see{ Aktor} } %
Ein \textit { Aktor} ist ein Prozess, der nebenläufig zu anderen Aktoren
2014-03-16 19:00:22 +01:00
läuft. Er kommuniziert mit anderen Aktoren, indem er Nachrichten austauscht.
\end { definition}
Das folgende Wetter-Beispiel zeigt, wie man Aktoren benutzen kann.
2014-03-10 15:39:59 +01:00
2014-09-14 22:38:48 +02:00
\subsection { Message Passing} \xindex { "! (Scala)} %
2014-03-29 14:20:26 +01:00
Prozesse können nach dem Schema \texttt { adresse ! Nachricht} Nachrichten austauschen.
Dieses Schema ist asynchron.
Prozesse können mit \texttt { receive{ case x => print(x)} } Nachrichten empfangen,
wobei in diesem Beispiel \texttt { x} alles matcht. Wenn eine gesendete Nachricht
vom Empfänger nicht gematcht wird, bleibt sie dennoch gespeichert.
2014-03-24 23:25:10 +01:00
\section { Weiteres}
\texttt { def awaitAll(timeout: Long, fts: Future[Any]*):} \\
\- \hspace { 1.8cm} \texttt { List[Option[Any]]} \xindex { awaitAll} \\
\- \hspace { 0.8cm} $ \leftharpoonup $ \texttt { scala.actors.Futures.\_ }
Waits until either all futures are resolved or a given time span has passed. Results are collected in a list of options. The result of a future that resolved during the time span is its value wrapped in Some. The result of a future that did not resolve during the time span is None.
Note that some of the futures might already have been awaited, in which case their value is returned wrapped in Some. Passing a timeout of 0 causes awaitAll to return immediately.
2014-03-05 13:42:36 +01:00
\section { Beispiele}
2014-03-10 15:39:59 +01:00
\subsection { Wetter}
Das folgende Script sendet parallel Anfragen über verschiedene ZIP-Codes an
die Yahoo-Server, parst das XML und extrahiert die Stadt sowie die Temperatur:
\inputminted [linenos, numbersep=5pt, tabsize=4, frame=lines, label=weather.scala] { scala} { scripts/scala/weather.scala}
2014-02-01 13:46:33 +01:00
2014-08-23 13:57:49 -04:00
\subsection { High Product}
Das folgende Skript berechnet folgendes: Wenn man aus den Ziffern 0 - 9 zwei
Zahlen $ a $ , $ b $ bilden darf, welche Zahlen muss man dann bilden um das größte Produkt
$ a \cdot b $ zu erhalten?
\inputminted [linenos, numbersep=5pt, tabsize=4, frame=lines, label=main.scala] { scala} { scripts/scala/HighProduct.scala}
2014-09-16 16:56:23 +02:00
\subsection { Potenzierung}
Will man Zweierpotenzen bilden, so kann man die Berechnung beschleunigen, in dem
man immer wieder Quadriert:
\inputminted [linenos, numbersep=5pt, tabsize=4, frame=lines, label=power-futures.scala] { scala} { scripts/scala/power-futures.scala}
2014-09-17 15:28:17 +02:00
\subsection { Coffeetime 01: Two Bases}
Find three digits $ X $ , $ Y $ and $ Z $ such that $ XYZ $ in base 10 is equal to $ ZYX $
in base 9.
\inputminted [linenos, numbersep=5pt, tabsize=2, frame=lines, label=01-TwoBases.scala] { scala} { scripts/scala/01-TwoBases.scala}
\subsection { Coffeetime 04: Exactly a third}
Arrange the numerals 1-9 into a single fraction that equals exactly
$ \frac { 1 } { 3 } $ .
No other math symbols wanted; just concatenation some digits for the
numerator, and some to make a denominator.
\inputminted [linenos, numbersep=5pt, tabsize=4, frame=lines, label=04-ExactlyAThird.scala] { scala} { scripts/scala/04-ExactlyAThird.scala}
\subsection { Coffeetime 05: Three Dice} \xindex { yield (Scala)@\texttt { yield} (Scala)}
I roll three dice, and multiply the three numbers together.
What is the probability the total will be odd?
\inputminted [linenos, numbersep=5pt, tabsize=4, frame=lines, label=05-ThreeDice.scala] { scala} { scripts/scala/05-ThreeDice.scala}
2014-03-07 19:17:17 +01:00
\section { Weitere Informationen}
\begin { itemize}
2014-03-24 23:25:10 +01:00
\item \url { http://www.scala-lang.org/api}
2014-03-07 19:17:17 +01:00
\item \url { http://docs.scala-lang.org/style/naming-conventions.html}
\end { itemize}
2014-03-05 13:42:36 +01:00
\index { Scala|)}