summaryrefslogtreecommitdiffhomepage
path: root/scala.html.markdown
diff options
context:
space:
mode:
authorGeoff Liu <cangming.liu@gmail.com>2014-11-10 19:38:48 -0700
committerGeoff Liu <cangming.liu@gmail.com>2014-11-10 19:38:48 -0700
commit761f150b4bb0374e71aa16a2323a96a89603aaf7 (patch)
tree79854927e0c8f8b87bb2af33b328594671e21f96 /scala.html.markdown
parent57c9f704170cd4d3adf49623c9f1b7a7a9670925 (diff)
Clean up of section 1
Diffstat (limited to 'scala.html.markdown')
-rw-r--r--scala.html.markdown73
1 files changed, 43 insertions, 30 deletions
diff --git a/scala.html.markdown b/scala.html.markdown
index a55e1f0e..a0983bdb 100644
--- a/scala.html.markdown
+++ b/scala.html.markdown
@@ -32,23 +32,36 @@ Scala - the scalable language
## 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
println("Hello world!")
+println(10)
+
// Printing, without forcing a new line on next print
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
// a good thing.
val x = 10 // x is now 10
x = 20 // error: reassignment to val
-var x = 10
-x = 20 // x is now 20
+var y = 10
+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
true
@@ -65,9 +78,11 @@ true == false // false
2 - 1 // 1
5 * 3 // 15
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
@@ -79,48 +94,46 @@ true == false // false
This means the result of evaluating 1 + 7 is an object of type Int with a
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.
*/
-
-// Strings
-
-"Scala strings are surrounded by double quotes" //
+"Scala strings are surrounded by double quotes"
'a' // A Scala Char
'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)
-println("ABCDEF".substring(2, 6))
-println("ABCDEF".replace("C", "3"))
+// They also have some extra Scala methods. See also: scala.collection.immutable.StringOps
+"hello world".take(5)
-// String interpolation
+// String interpolation: notice the prefix "s"
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
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."
-println(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"My second daughter is ${a(0) - a(2)} years old." // => "My second daughter is 5 years old."
+s"We have double the amount of ${n / 2.0} in apples." // => "We have double the amount of 22.5 in apples."
+s"Power of 2: ${math.pow(2, 2)}" // => "Power of 2: 4"
-// Formatting with interpolated strings (note the prefixed f)
-println(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"
+// Formatting with interpolated strings with the prefix "f"
+f"Power of 5: ${math.pow(5, 2)}%1.0f" // "Power of 5: 25"
+f"Square root of 122: ${math.sqrt(122)}%1.4f" // "Square root of 122"
-// Ignoring special characters.
-println(raw"New line feed: \n. Carriage return: \r.") // => "New line feed: \n. Carriage return: \r."
+// Raw strings, ignoring special characters.
+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:
-val a = "They stood outside the \"Rose and Crown\"" // => "They stood outside the "Rose and Crown""
+// Some characters need to be "escaped", e.g. a double quote inside a string:
+"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
val html = """<form id="daform">
<p>Press belo', Joe</p>
- | <input type="submit">
+ <input type="submit">
</form>"""