Clean up of section 1

This commit is contained in:
Geoff Liu 2014-11-10 19:38:48 -07:00
parent 57c9f70417
commit 761f150b4b

View File

@ -32,23 +32,36 @@ Scala - the scalable language
## 1. Basics ## 1. Basics
################################################# #################################################
// Single line comments start with two forward slashes
/*
Multi line comments, as you can already see from above, look like this.
*/
// Printing, and forcing a new line on the next print // Printing, and forcing a new line on the next print
println("Hello world!") println("Hello world!")
println(10)
// Printing, without forcing a new line on next print // Printing, without forcing a new line on next print
print("Hello world") print("Hello world")
// Declaring values is done using either var or val // Declaring values is done using either var or val.
// val declarations are immutable, whereas var's are mutable. Immutability is // val declarations are immutable, whereas var's are mutable. Immutability is
// a good thing. // a good thing.
val x = 10 // x is now 10 val x = 10 // x is now 10
x = 20 // error: reassignment to val x = 20 // error: reassignment to val
var x = 10 var y = 10
x = 20 // x is now 20 y = 20 // y is now 20
// Single line comments start with two forward slashes
/* /*
Multi line comments look like this. Scala is a statically typed language, yet note that in the above declarations, we did not specify
a type. This is due to a language feature called type inference. In most cases, Scala compiler can
guess what the type of a variable is, so you don't have to type it every time. We can explicitly
declare the type of a variable like so:
*/ */
val z: Int = 10
val a: Double = 1.0
val b: Double = 10 // Notice automatic conversion from Int to Double, result is 10.0, not 10
// Boolean values // Boolean values
true true
@ -65,9 +78,11 @@ true == false // false
2 - 1 // 1 2 - 1 // 1
5 * 3 // 15 5 * 3 // 15
6 / 2 // 3 6 / 2 // 3
6 / 4 // 1
6.0 / 4 // 1.5
// Evaluating a command in the REPL gives you the type and value of the result // Evaluating an expression in the REPL gives you the type and value of the result
1 + 7 1 + 7
@ -79,48 +94,46 @@ true == false // false
This means the result of evaluating 1 + 7 is an object of type Int with a This means the result of evaluating 1 + 7 is an object of type Int with a
value of 8 value of 8
1+7 will give you the same result Note that "res29" is a sequentially generated variable name to store the results of the
expressions you typed, your output may differ.
*/ */
"Scala strings are surrounded by double quotes"
// Strings
"Scala strings are surrounded by double quotes" //
'a' // A Scala Char 'a' // A Scala Char
'Single quote strings don't exist' // Error 'Single quote strings don't exist' // Error
"Strings have the usual Java methods defined on them".length
"They also have some extra Scala methods.".reverse
// Seealso: scala.collection.immutable.StringOps // Strings have the usual Java methods defined on them
"hello world".length
"ABCDEF".substring(2, 6)
"ABCDEF".replace("C", "3")
println("ABCDEF".length) // They also have some extra Scala methods. See also: scala.collection.immutable.StringOps
println("ABCDEF".substring(2, 6)) "hello world".take(5)
println("ABCDEF".replace("C", "3"))
// String interpolation // String interpolation: notice the prefix "s"
val n = 45 val n = 45
println(s"We have $n apples") // => "We have 45 apples" s"We have $n apples" // => "We have 45 apples"
// Expressions inside interpolated strings are also possible // Expressions inside interpolated strings are also possible
val a = Array(11, 9, 6) val a = Array(11, 9, 6)
println(s"My second daughter is ${a(0) - a(2)} years old.") // => "My second daughter is 5 years old." s"My second daughter is ${a(0) - a(2)} years old." // => "My second daughter is 5 years old."
println(s"We have double the amount of ${n / 2.0} in apples.") // => "We have double the amount of 22.5 in apples." s"We have double the amount of ${n / 2.0} in apples." // => "We have double the amount of 22.5 in apples."
println(s"Power of 2: ${math.pow(2, 2)}") // => "Power of 2: 4" s"Power of 2: ${math.pow(2, 2)}" // => "Power of 2: 4"
// Formatting with interpolated strings (note the prefixed f) // Formatting with interpolated strings with the prefix "f"
println(f"Power of 5: ${math.pow(5, 2)}%1.0f") // "Power of 5: 25" f"Power of 5: ${math.pow(5, 2)}%1.0f" // "Power of 5: 25"
println(f"Square root of 122: ${math.sqrt(122)}%1.4f") // "Square root of 122" f"Square root of 122: ${math.sqrt(122)}%1.4f" // "Square root of 122"
// Ignoring special characters. // Raw strings, ignoring special characters.
println(raw"New line feed: \n. Carriage return: \r.") // => "New line feed: \n. Carriage return: \r." raw"New line feed: \n. Carriage return: \r." // => "New line feed: \n. Carriage return: \r."
// Some characters need to be 'escaped', e.g. a double quote inside a string: // Some characters need to be "escaped", e.g. a double quote inside a string:
val a = "They stood outside the \"Rose and Crown\"" // => "They stood outside the "Rose and Crown"" "They stood outside the \"Rose and Crown\"" // => "They stood outside the "Rose and Crown""
// Triple double-quotes let strings span multiple rows and contain quotes // Triple double-quotes let strings span multiple rows and contain quotes
val html = """<form id="daform"> val html = """<form id="daform">
<p>Press belo', Joe</p> <p>Press belo', Joe</p>
| <input type="submit"> <input type="submit">
</form>""" </form>"""