Dienstag, 6. Oktober 2009

learning Scala 3

this tutorial based on:
http://www.artima.com/scalazine/articles/steps.html


def max(x: Int, y: Int): Int = if (x < y) y else x


def max(x: Int, y: Int) -> define a function, with 2 parameters (integer)
: Int -> return is integer.
if (x < y) y else x -> function body



scala> def greet() = println("Hello, world!")
greet: ()Unit

scala> greet()
Hello, world!

scala> greet
Hello, world!


greet save a Unit -> a function


args.foreach(arg => println(arg))
args.foreach((arg: String) => println(arg))
args.foreach(println)

for (arg <- args)
println(arg)


scala> for (i <- 0 to 5)
| println(i)
0
1
2
3
4
5



val greetStrings: Array[String] = new Array[String](3)

create an array with 3 strings


greetStrings(0) = "Hello"
greetStrings(1) = ", "
greetStrings(2) = "world!\n"


important indexes are in brakes () not square brackets[]


for (i <- 0 to 2)
print(greetStrings(i))

Keine Kommentare:

Kommentar veröffentlichen