2
0
Fork 0
mirror of https://github.com/MartinThoma/LaTeX-examples.git synced 2025-04-19 11:38:05 +02:00

added another scala example

This commit is contained in:
Martin Thoma 2014-08-23 13:57:49 -04:00
parent 5dd282f596
commit accdf1507c
3 changed files with 59 additions and 0 deletions

View file

@ -89,6 +89,21 @@ Listen können erstellt und durchgegangen werden:
\caption{Logische Operatoren in Scala}\xindex{Logische Operatoren!Scala}
\end{table}
\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}
\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
@ -127,6 +142,13 @@ 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}
\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}
\section{Weitere Informationen}
\begin{itemize}
\item \url{http://www.scala-lang.org/api}

View file

@ -0,0 +1,37 @@
object HighProduct {
def main(arg: Array[String]) {
var max = List(BigInt(0), BigInt(0),
BigInt(0));
val digits = List(0, 1, 2, 3, 4, 5, 6,
7, 8, 9);
for (c <- digits.combinations(4)) {
// Sort the digits so that the
// highest number gets built
val a = c.sorted(Ordering[Int]);
val b = (digits filterNot a.contains).
sorted(Ordering[Int]);
// calculate number a
var anum = BigInt(0);
for ((digit, place) <- a.zipWithIndex) {
anum += digit *
scala.math.pow(10, place).
toInt;
}
// calculate number b
var bnum = BigInt(0);
for ((digit, place) <- b.zipWithIndex) {
bnum += digit *
scala.math.pow(10, place).
toInt;
}
// calculate number a
if (anum * bnum > max(0)) {
max = List(anum * bnum, anum, bnum);
}
}
println("%d • %d = %d".format(max(1),
max(2),
max(0)));
}
}