2
0
Fork 0
mirror of https://github.com/MartinThoma/LaTeX-examples.git synced 2025-04-25 06:18:05 +02:00

Added Scala example

This commit is contained in:
Martin Thoma 2014-03-10 15:39:59 +01:00
parent 420b6b31fd
commit 7e76256e4e
8 changed files with 60 additions and 5 deletions

View file

@ -117,5 +117,9 @@ Speichere den folgenden Text als \texttt{hello-world.c}:
Compiliere ihn mit \texttt{gcc hello-world.c}. Es wird eine ausführbare
Datei namens \texttt{a.out} erzeugt.
\subsection{Pointer}
\inputminted[linenos, numbersep=5pt, tabsize=4]{c}{scripts/c/pointer.c}
Die Ausgabe hier ist \texttt{0 3}.
\index{C|)}

View file

@ -162,16 +162,16 @@ Ein paar Beispiele zur Typinferenz:
\label{fig:haskell-type-hierarchy}
\end{figure}
\subsection{type}
\subsection{type}\xindex{type}%
Mit \texttt{type} können Typsynonyme erstellt werden:
\inputminted[linenos, numbersep=5pt, tabsize=4]{haskell}{scripts/haskell/alt-types.hs}
\inputminted[numbersep=5pt, tabsize=4]{haskell}{scripts/haskell/alt-types.hs}
\subsection{data}
\subsection{data}\xindex{data}%
Mit dem Schlüsselwort \texttt{data} können algebraische Datentypen\xindex{Datentyp!algebraischer}
erzeugt werden:
\inputminted[numbersep=5pt, tabsize=4]{haskell}{scripts/haskell/data-example.hs}
\section{Lazy Evaluation}\xindex{Lazy Evaluation}
Haskell wertet Ausdrücke nur aus, wenn es nötig ist.

View file

@ -46,6 +46,7 @@ einige Unterschiede.
\item Java hat primitive Typen, Scala ausschließlich Objekte.
\item Scala benötigt kein \texttt{;} am Ende von Anweisungen.
\item Scala ist kompakter.
\item Java hat \texttt{static}, Scala hat \texttt{object} (Singleton)
\end{itemize}
}
@ -75,7 +76,20 @@ Listen können erstellt und durchgegangen werden:
\inputminted[numbersep=5pt, tabsize=4]{scala}{scripts/scala/extended-for.scala}
\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.
\section{actor}\xindex{actor}
actor dient der Concurrency.
\section{Beispiele}
\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}
\section{Weitere Informationen}
\begin{itemize}

View file

@ -0,0 +1,8 @@
#include <stdio.h>
int arr[] = {0,1,2,3,4,5};
int main() {
printf("%i %i", arr[0], (&arr[3])[0]);
return 0;
}

View file

@ -2,3 +2,4 @@ type Prename = String
type Age = Double
type Person = (Prename, Age)
type Friends = [Person]
type Polynom = [Double]

View file

@ -0,0 +1,4 @@
data Bool = False | True
data Color = Red | Green | Blue | Indigo | Violet
data Tree a = Leaf a | Branch (Tree a) (Tree a)
data Point = Point Float Float deriving (Show)

View file

@ -0,0 +1,24 @@
import scala.io._
import scala.xml.{Source => Source2, _}
import scala.actors._
import Actor._
def getWeatherInfo(woeid: String) = {
val url = "http://weather.yahooapis.com/forecastrss?w=" + woeid
val response = Source.fromURL(url).mkString
val xmlResponse = XML.loadString(response)
println(xmlResponse \\ "location" \\ "@city",
xmlResponse \\ "condition" \\ "@temp")
}
val caller = self
for(id <- 2391271 to 2391279) {
actor{ getWeatherInfo(id.toString) }
}
for(id <- 2391271 to 2391279) {
receiveWithin(5000) {
case msg => println(msg)
}
}