summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--scala.html.markdown86
1 files changed, 53 insertions, 33 deletions
diff --git a/scala.html.markdown b/scala.html.markdown
index 8e00f135..687914ef 100644
--- a/scala.html.markdown
+++ b/scala.html.markdown
@@ -8,9 +8,7 @@ filename: learn.scala
Scala - the scalable language
-```c
-
-
+```scala
/*
Set yourself up:
@@ -21,7 +19,8 @@ Scala - the scalable language
scala>
- This is the so called REPL. You can run commands in the REPL. Let's do just that:
+ This is the so called REPL. You can run commands in the REPL. Let's do just
+ that:
*/
println(10) // prints the integer 10
@@ -37,7 +36,8 @@ println("Hello world!")
print("Hello world")
// Declaring values is done using either var or val
-// val declarations are immutable, whereas var's are mutable. Immutablility is a good thing.
+// val declarations are immutable, whereas var's are mutable. Immutablility is
+// a good thing.
val x = 10 // x is now 10
x = 20 // error: reassignment to val
var x = 10
@@ -74,7 +74,8 @@ true == false // false
scala> 1 + 7
res29: Int = 8
- This means the result of evaluating 1 + 7 is an object of type Int with a value of 8
+ 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
*/
@@ -94,14 +95,16 @@ val sq = (x:Int) => x * x
sq: Int => Int = <function1>
- Which means that this time we gave an explicit name to the value - sq is a function that take an Int and returns Int.
+ Which means that this time we gave an explicit name to the value - sq is a
+ function that take an Int and returns Int.
sq can be executed as follows:
*/
-sq(10) // Gives you this: res33: Int = 100. The result is the Int with a value 100
+sq(10) // Gives you this: res33: Int = 100.
-// Scala allows methods and functions to return, or take as parameters, other functions or methods.
+// Scala allows methods and functions to return, or take as parameters, other
+// functions or methods.
val add10: Int => Int = _ + 10 // A function taking an Int and returning an Int
List(1, 2, 3) map add10 // List(11, 12, 13) - add10 is applied to each element
@@ -109,10 +112,12 @@ List(1, 2, 3) map add10 // List(11, 12, 13) - add10 is applied to each element
// Anonymous functions can be used instead of named functions:
List(1, 2, 3) map (x => x + 10)
-// And the underscore symbol, can be used if there is just one argument to the anonymous function. It gets bound as the variable
+// And the underscore symbol, can be used if there is just one argument to the
+// anonymous function. It gets bound as the variable
List(1, 2, 3) map (_ + 10)
-TODO // If the anonymous block AND the function you are applying both take one argument, you can even omit the underscore
+// If the anonymous block AND the function you are applying both take one
+// argument, you can even omit the underscore
List("Dom", "Bob", "Natalia") foreach println
@@ -136,7 +141,8 @@ val s = Set(1, 3, 7)
s(0)
s(1)
-/* Look up the documentation of map here - http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Map
+/* Look up the documentation of map here -
+ * http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Map
* and make sure you can read it
*/
@@ -156,7 +162,8 @@ val divideInts = (x:Int, y:Int) => (x / y, x % y)
divideInts(10,3) // The function divideInts gives you the result and the remainder
-// To access the elements of a tuple, use _._n where n is the 1-based index of the element
+// To access the elements of a tuple, use _._n where n is the 1-based index of
+// the element
val d = divideInts(10,3)
d._1
@@ -175,7 +182,8 @@ sSquared.filter(_ < 10)
sSquared.reduce (_+_)
-// The filter function takes a predicate (a function from A -> Boolean) and selects all elements which satisfy the predicate
+// The filter function takes a predicate (a function from A -> Boolean) and
+// selects all elements which satisfy the predicate
List(1, 2, 3) filter (_ > 2) // List(3)
List(
Person(name = "Dom", age = 23),
@@ -183,7 +191,8 @@ List(
).filter(_.age > 25) // List(Person("Bob", 30))
-// Scala a foreach method defined on certain collections that takes a type returning Unit (a void method)
+// Scala a foreach method defined on certain collections that takes a type
+// returning Unit (a void method)
aListOfNumbers foreach (x => println(x))
aListOfNumbers foreach println
@@ -200,8 +209,8 @@ for { n <- nSquared2 if n < 10 } yield n
for { n <- s; nSquared = n * n if nSquared < 10} yield nSquared
-/* NB Those were not for loops. The semantics of a for loop is 'repeat', whereas a for-comprehension
- defines a relationship between two sets of data. Research this further */
+/* NB Those were not for loops. The semantics of a for loop is 'repeat', whereas
+ a for-comprehension defines a relationship between two sets of data. */
@@ -212,8 +221,8 @@ val r = 1 to 5
r.foreach( println )
r foreach println
-// NB: Scala is quite lenien when it comes to dots and brackets - study the rules separately. This
-// helps write DSLs and APIs that read like English
+// NB: Scala is quite lenient when it comes to dots and brackets - study the
+// rules separately. This helps write DSLs and APIs that read like English
(5 to 1 by -1) foreach ( println )
@@ -223,20 +232,25 @@ while (i < 10) { println("i " + i); i+=1 }
while (i < 10) { println("i " + i); i+=1 } // Yes, again. What happened? Why?
-i // Show the value of i. Note that while is a loop in the classical sense - it executes
- // sequentially while changing the loop variable. while is very fast, faster that Java
- // loops, but using the combinators and comprehensions above is easier to understand
- // and parallelize
+i // Show the value of i. Note that while is a loop in the classical sense -
+ // it executes sequentially while changing the loop variable. while is very
+ // fast, faster that Java // loops, but using the combinators and
+ // comprehensions above is easier to understand and parallelize
// A do while loop
-do {
+do {
println("x is still less then 10");
x += 1
} while (x < 10)
-// Tail recursion is an idiomatic way of doing recurring things in Scala. Recursive functions need an
-// explicit return type, the compiler can't infer it. Here it's Unit.
-def showNumbersInRange(a:Int, b:Int):Unit = { print(a); if (a < b) showNumbersInRange(a+1, b) }
+// Tail recursion is an idiomatic way of doing recurring things in Scala.
+// Recursive functions need an explicit return type, the compiler can't infer it.
+// Here it's Unit.
+def showNumbersInRange(a:Int, b:Int):Unit = {
+ print(a)
+ if (a < b)
+ showNumbersInRange(a + 1, b)
+}
@@ -268,7 +282,8 @@ class Dog {
}
}
-// Classes can contain nearly any other construct, including other classes, functions, methods, objects, case classes, traits etc.
+// Classes can contain nearly any other construct, including other classes,
+// functions, methods, objects, case classes, traits etc.
@@ -285,7 +300,8 @@ Person("George", "1234") == Person("Kate", "1236")
val me = Person("George", "1234")
-me match { case Person(name, number) => "We matched someone : " + name + ", phone : " + number }
+me match { case Person(name, number) => {
+ "We matched someone : " + name + ", phone : " + number }}
me match { case Person(name, number) => "Match : " + name; case _ => "Hm..." }
@@ -303,7 +319,7 @@ kate match { case Person("Kate", _) => "Girl"; case Person("George", _) => "Boy"
// Regular expressions
-val email = "(.*)@(.*)".r // The suffix .r invokes method r on String, which makes it a Regex
+val email = "(.*)@(.*)".r // Invoking r on String makes it a Regex
val email(user, domain) = "henry@zkpr.com"
@@ -319,7 +335,9 @@ val email(user, domain) = "henry@zkpr.com"
'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 // See scala.collection.immutable.StringOps
+"They also have some extra Scala methods.".reverse
+
+// Seealso: scala.collection.immutable.StringOps
println("ABCDEF".length)
println("ABCDEF".substring(2, 6))
@@ -334,7 +352,8 @@ println(s"My second daughter is ${a(2-1)} years old")
// Some characters need to be 'escaped', e.g. a double quote inside a string:
val a = "They stood outside the \"Rose and Crown\""
-// Triple double-quotes allow for strings to span multiple rows and contain funny characters
+// Triple double-quotes let strings span multiple rows and contain quotes
+
val html = """<form id="daform">
<p>Press belo', Joe</p>
| <input type="submit">
@@ -359,7 +378,8 @@ import scala.collection.immutable{ List => ImmutableList }
// Import all classes, except some. The following excludes Map and Set:
import scala.collection.immutable.{Map => _, Set => _, _}
-// Your programs entry point is defined in an scala file using an object, with a single method, main:
+// Your programs entry point is defined in an scala file using an object, with a
+// single method, main:
object Application {
def main(args: Array[String]): Unit = {
// stuff goes here.