Created
January 7, 2016 10:00
-
-
Save hossshy/9387bb1850dd2b2640e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Chapter2 { | |
def main(args: Array[String]) { | |
// declare functions | |
def hello() = { "Hello World!" } | |
println(hello) | |
def hello2():String = { "Hello World2!" } | |
println(hello2) | |
def hello3 = "Hello World3!" | |
println(hello3) | |
def square (i:Int) = { i * i } | |
println(square(3)) | |
def add(x: Int, y: Int): Int = { x + y } | |
println(add(5, 6)) | |
// Arrays | |
var books:Array[String] = new Array[String](3) | |
books(1) = "hoge" | |
for ( x <- books ) { | |
println(x) | |
} | |
var books2 = Array("a", "b", "c") | |
for ( x <- books2 ) { | |
println(x) | |
} | |
// List | |
var booklist: List[String] = List("Scala", "Groovy", "Java") | |
println(booklist.head) // like car? | |
println(booklist.tail) // like cdr? | |
// Range | |
println(1 to 5) | |
println(1 until 5) | |
println(1 to 20 by 4) | |
// Tuple | |
val tuple = (1, false, "Scala") | |
println(tuple) | |
println(tuple._1) // the first element has an index 1 | |
println(tuple._3) | |
val tuple2 = "title" -> "Beginning Scala" | |
println(tuple2) | |
// If | |
val ternary: Int = if (true) 1 else 3 | |
println(ternary) | |
val ternary2: Int = if (false) 1 | |
else { | |
val j = System.currentTimeMillis() | |
(j % 100L).toInt | |
} | |
println(ternary2) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment