From 3cb8d2bcc56332a814d0ae69492782ee4ddbd473 Mon Sep 17 00:00:00 2001 From: "a.vandijk" Date: Fri, 8 Aug 2014 15:19:14 +0200 Subject: - [ADD] PrintWriter example from Java - [CHANGE] Extended regex matching with more examples with switch / case - [CHANGE] String interpolation with formatting and expressions explained. --- scala.html.markdown | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 432933c2..4dc87a94 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -299,7 +299,6 @@ Person("George", "1234") == Person("Kate", "1236") - // Pattern matching val me = Person("George", "1234") @@ -322,15 +321,22 @@ kate match { case Person("Kate", _) => "Girl"; case Person("George", _) => "Boy" // Regular expressions - val email = "(.*)@(.*)".r // Invoking r on String makes it a Regex - -val email(user, domain) = "henry@zkpr.com" - -"mrbean@pyahoo.com" match { - case email(name, domain) => "I know your name, " + name +val serialKey = """(\d{5})-(\d{5})-(\d{5})-(\d{5})""".r // Using multiline string syntax + +val matcher = (value: String) => { + println(value match { + case email(name, domain) => s"It was an email: $name" + case serialKey(p1, p2, p3, p4) => s"Serial key: $p1, $p2, $p3, $p4" + case _ => s"No match on '$value'" // default if no match found + }) } +matcher("mrbean@pyahoo.com") +matcher("nope..") +matcher("52917") +matcher("52752-16432-22178-47917") + // Strings @@ -347,17 +353,27 @@ println("ABCDEF".length) println("ABCDEF".substring(2, 6)) println("ABCDEF".replace("C", "3")) +// String interpolation val n = 45 println(s"We have $n apples") +// Expressions inside interpolated strings are also possible val a = Array(11, 9, 6) -println(s"My second daughter is ${a(2-1)} years old") +println(s"My second daughter is ${a(0) - a(2)} years old") +println(s"We have double the amount of ${n / 2.0} in apples.") +println(s"Power of 2: ${math.pow(2, 2)}") // Power of 2: 4.0 + +// 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 + +// Ignoring special characters. +println(raw"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\"" // Triple double-quotes let strings span multiple rows and contain quotes - val html = """

Press belo', Joe

| @@ -403,7 +419,10 @@ for(line <- Source.fromFile("myfile.txt").getLines()) println(line) // To write a file use Java's PrintWriter - +val writer = new PrintWriter("myfile.txt") +writer.write("Writing line for line" + util.Properties.lineSeparator) +writer.write("Another line here" + util.Properties.lineSeparator) +writer.close() ``` -- cgit v1.2.3 From 07b6229ee608ae37283a827112b5eab4c58a5333 Mon Sep 17 00:00:00 2001 From: "a.vandijk" Date: Fri, 8 Aug 2014 15:23:46 +0200 Subject: [CHANGE] Comment match on serial key now mentions verbatim (multiline) --- scala.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 4dc87a94..619c8aae 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -322,7 +322,7 @@ kate match { case Person("Kate", _) => "Girl"; case Person("George", _) => "Boy" // Regular expressions val email = "(.*)@(.*)".r // Invoking r on String makes it a Regex -val serialKey = """(\d{5})-(\d{5})-(\d{5})-(\d{5})""".r // Using multiline string syntax +val serialKey = """(\d{5})-(\d{5})-(\d{5})-(\d{5})""".r // Using verbatim (multiline) syntax val matcher = (value: String) => { println(value match { -- cgit v1.2.3 From d2cf0062822ec364a21afa6550d4d0538857a7fe Mon Sep 17 00:00:00 2001 From: "a.vandijk" Date: Fri, 15 Aug 2014 15:47:12 +0200 Subject: [CHANGE] Output examples added to matcher part and string interpolation part. --- scala.html.markdown | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index 619c8aae..eae39abb 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -332,11 +332,10 @@ val matcher = (value: String) => { }) } -matcher("mrbean@pyahoo.com") -matcher("nope..") -matcher("52917") -matcher("52752-16432-22178-47917") - +matcher("mrbean@pyahoo.com") // => "It was an email: mrbean" +matcher("nope..") // => "No match on 'nope..'" +matcher("52917") // => "No match on '52917'" +matcher("52752-16432-22178-47917") // => "Serial key: 52752, 16432, 22178, 47917" // Strings @@ -359,13 +358,13 @@ println(s"We have $n 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") -println(s"We have double the amount of ${n / 2.0} in apples.") -println(s"Power of 2: ${math.pow(2, 2)}") // Power of 2: 4.0 +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" // 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 +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" // Ignoring special characters. println(raw"New line feed: \n. Carriage return: \r.") -- cgit v1.2.3 From 775c7faa65e84b8030478f4476995935ac8c2813 Mon Sep 17 00:00:00 2001 From: "a.vandijk" Date: Sat, 16 Aug 2014 18:21:49 +0200 Subject: [ADDED] Output to additional println's. --- scala.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'scala.html.markdown') diff --git a/scala.html.markdown b/scala.html.markdown index eae39abb..6b398b4b 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -354,11 +354,11 @@ println("ABCDEF".replace("C", "3")) // String interpolation val n = 45 -println(s"We have $n apples") +println(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"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" @@ -367,10 +367,10 @@ 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" // Ignoring special characters. -println(raw"New line feed: \n. Carriage return: \r.") +println(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\"" +val a = "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 = """ -- cgit v1.2.3