From 4f06e456a973a99e15a232cee60a53c9f0bd8ace Mon Sep 17 00:00:00 2001 From: Dominic Bou-Samra Date: Sun, 30 Jun 2013 17:52:21 +1000 Subject: [PATCH 01/19] Started on Scala --- scala.html.markdown | 178 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 scala.html.markdown diff --git a/scala.html.markdown b/scala.html.markdown new file mode 100644 index 00000000..e8cde611 --- /dev/null +++ b/scala.html.markdown @@ -0,0 +1,178 @@ +--- +language: scala +author: Dominic Bou-Samra +author_url: http://dbousamra.github.com +filename: learnscala.scala +--- + +Scala is a + +```scala + +/////////////////////////////////////// +// Basic syntax +/////////////////////////////////////// + +// Single line comments start with two forward slashes +/* +Multi line comments look like this. +*/ + +// Import packages +import scala.collection.immutable.List +// Import all "sub packages" +import scala.collection.immutable._ +// Import multiple classes in one statement +import scala.collection.immutable.{List, Map} +// Rename an import using '=>' +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: +object Application { + def main(args: Array[String]): Unit = { + // stuff goes here. + } +} + +// Printing, and forcing a new line on the next print +println("Hello world!") +// Printing, without forcing a new line on next print +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 x = 10 // x is now 10 +x = 20 // error: reassignment to val +var x = 10 +x = 20 // x is now 20 + +/////////////////////////////////////// +// Types +/////////////////////////////////////// + +// Almost all types are objects. + +// You have numbers +3 //3 + +// Math is as per usual +1 + 1 // 2 +2 - 1 // 1 +5 * 3 // 15 +6 / 2 // 3 + +// Boolean values +true +false + +// Boolean operations +!true // false +!false // true +true == false // false +10 > 5 // true + +// Strings and characters +"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 // See scala.collection.immutable.StringOps + +/////////////////////////////////////// +// Basic control constructs +/////////////////////////////////////// + +// if statements (else statements are optional) +if (10 > 5) println("10 is greater than 5") +// an else +if (x > 5) println("x is greater than 5") +else println("No it's not.") + +// Iteration + +// A while loop +while (x < 10) { + println("x is still less then 10") + x += 1 +} + +// A do while loop +do { + println("x is still less then 10"); + x += 1 +} while (x < 10) + +// A for loop +for (x <- 0 until 10) { + println(x) +} + +// Any object implementing the map/filter/flatMap methods allows the use of a for loop: +val aListOfNumbers: List[Int] = List(1, 2, 3) +for (x <- aListOfNumbers) { + println(x) +} + +// Pattern matching (see respective section) +x match { + case 5 => println("x is 5") + case 10 => println("x is 10") + case _ => println("default case") +} + +/////////////////////////////////////// +// Functions, methods and classes +/////////////////////////////////////// + +// Scala has classes + +// classname is Dog +class Dog { + //A method called bark, returning a String + def bark: String = { + // the body of the method + "Woof, woof!" + } +} + +// They can contain nearly any other construct, including other classes, functions, methods, objects, case classes, traits etc. + +/////////////////////////////////////// +// Higher-order functions +/////////////////////////////////////// + +// 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 + +// 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 +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 +List("Dom", "Bob", "Natalia") foreach println + + +// Scala collections have rich higher-order functions defined on them. Some examples: + +// The map function takes a function/method, and applies it to each element in the structure +List(1, 2, 3) map (number => number.toString) + +// 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), + Person(name = "Bob", age = 30) +).filter(_.age > 25) // List(Person("Bob", 30)) + + +// 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 + + From 2e7c3ba085da91a198f7b339d57bdf1fd8c8b49c Mon Sep 17 00:00:00 2001 From: George Petrov Date: Sun, 28 Jul 2013 20:52:32 +0100 Subject: [PATCH 02/19] Starting a Scala one --- scala.html.markdown | 53 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 scala.html.markdown diff --git a/scala.html.markdown b/scala.html.markdown new file mode 100644 index 00000000..f087881a --- /dev/null +++ b/scala.html.markdown @@ -0,0 +1,53 @@ +/* + Set yourself up: + + 1) Download Scala - http://www.scala-lang.org/downloads + 2) unzip/untar in your favourite location and put the bin subdir on the path + 3) Start a scala REPL by typing scala. You should see the prompt: + + scala> + + This is the so called REPL. You can run commands in the REPL. Let do just that: +*/ + +println(10) // prints the integer 10 + +println("Boo!") // printlns the string Boo! + + +// Evaluating a command gives you the type and value of the result + +1 + 7 + +/* The above line results in: + + 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 + + 1+7 will give you the same result +*/ + + +// Everything is an object, including a function type these in the repl: + +7 // results in res30: Int = 7 (res30 is just a generated var name for the result) + +// The next line gives you a function that takes an Int and returns it squared +(x:Int) => x * x + +// You can assign this function to an identifier, like this: +val sq = (x:Int) => x * x + +/* The above says this + + sq: Int => 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 + From 7673ae8a6dc79fdc8f555d8e0c64834af17403d1 Mon Sep 17 00:00:00 2001 From: George Petrov Date: Mon, 29 Jul 2013 09:29:14 +0100 Subject: [PATCH 03/19] Added arrays and maps --- scala.html.markdown | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/scala.html.markdown b/scala.html.markdown index f087881a..82bf9db2 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -51,3 +51,16 @@ val sq = (x:Int) => x * x sq(10) // Gives you this: res33: Int = 100. The result is the Int with a value 100 + + +// Data structures + +val a = Array(1, 2, 3, 5, 8, 13) +a(0) +a(3) +a(21) // Throws an exception + +val m = Map("fork" -> "tenedor", "spoon" -> "cuchara", "knife" -> "cuchillo") +m("fork") +m("spoon") +m("bottle") // Throws an exception \ No newline at end of file From 04e50a75fa253f71cf5de0f45d423f06dd9933d2 Mon Sep 17 00:00:00 2001 From: George Petrov Date: Mon, 29 Jul 2013 13:53:55 +0100 Subject: [PATCH 04/19] Added for comprehensions and conditionals --- scala.html.markdown | 48 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/scala.html.markdown b/scala.html.markdown index 82bf9db2..783c7ae6 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -63,4 +63,50 @@ a(21) // Throws an exception val m = Map("fork" -> "tenedor", "spoon" -> "cuchara", "knife" -> "cuchillo") m("fork") m("spoon") -m("bottle") // Throws an exception \ No newline at end of file +m("bottle") // Throws an exception + +val safeM = m.withDefaultValue("no lo se") +safeM("bottle") + +val s = Set(1, 3, 7) +s(0) +s(1) + + +// Tuples + + +// Combinators + +s.map(sq) + +val sSquared = s. map(sq) + +sSquared.filter(_ < 10) + +sSquared.reduce (_+_) + + +// For comprehensions + +for { n <- s } yield sq(n) + +val nSquared2 = for { n <- s } yield sq(n) + +for { n <- nSquared2 if n < 10 } yield n + +for { n <- s; nSquared = n * n if nSquared < 10} yield nSquared + + + +// Conditionals + +val x = 10 + +if (x == 1) println("yeah") +if (x == 10) println("yeah") +if (x == 11) println("yeah") + + +// Object oriented features + From 3cc4c64fb75581a21a2d8d4c2a28f3e83b5cf2f7 Mon Sep 17 00:00:00 2001 From: George Petrov Date: Mon, 29 Jul 2013 13:57:44 +0100 Subject: [PATCH 05/19] Added header --- scala.html.markdown | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/scala.html.markdown b/scala.html.markdown index 783c7ae6..173d271d 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -1,3 +1,9 @@ +--- +language: scala +author: George Petrov +author_url: http://www.georgepetrov.com +--- + /* Set yourself up: @@ -106,6 +112,11 @@ val x = 10 if (x == 1) println("yeah") if (x == 10) println("yeah") if (x == 11) println("yeah") +if (x == 11) println ("yeah") else println("nope") + +println(if (x == 10) "yeah" else "nope") +val text = if (x == 10) "yeah" else "nope" + // Object oriented features From d346342ad608849bdd1bcae7973bbff571225ba1 Mon Sep 17 00:00:00 2001 From: George Petrov Date: Mon, 29 Jul 2013 14:09:11 +0100 Subject: [PATCH 06/19] Added some pattern matching --- scala.html.markdown | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/scala.html.markdown b/scala.html.markdown index 173d271d..d6524a8f 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -121,3 +121,25 @@ val text = if (x == 10) "yeah" else "nope" // Object oriented features +class Person + + + +// Case classes + +case class Person(name:String, phoneNumber:String) + +Person("George", "1234") == Person("Kate", "1236") + + + +// Pattern matching + + +// Regular expressions + + +// Strings + + +// Input and output \ No newline at end of file From 33f3d4d6953cce6c45f2a7f4e8afc12571193c73 Mon Sep 17 00:00:00 2001 From: George Petrov Date: Mon, 29 Jul 2013 15:14:37 +0100 Subject: [PATCH 07/19] Fixing formatting --- scala.html.markdown | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/scala.html.markdown b/scala.html.markdown index d6524a8f..3aedc88a 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -1,9 +1,16 @@ --- language: scala +filename: learn.scala author: George Petrov -author_url: http://www.georgepetrov.com +author_url: http://www.github.com/petrovg --- +Scala - the scalable language + +```c + + + /* Set yourself up: From 99b16c2672a2fc0b1dbb063c0b6e066158922099 Mon Sep 17 00:00:00 2001 From: George Petrov Date: Mon, 29 Jul 2013 15:18:24 +0100 Subject: [PATCH 08/19] Now really adding some pattern matching --- scala.html.markdown | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/scala.html.markdown b/scala.html.markdown index d6524a8f..d0187667 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -135,10 +135,29 @@ Person("George", "1234") == Person("Kate", "1236") // Pattern matching +val me = Person("George", "1234") + +me match { case Person(name, number) => "We matched someone : " + name + ", phone : " + number } + +me match { case Person(name, number) => "Match : " + name; case _ => "Hm..." } + +me match { case Person("George", number) => "Match"; case _ => "Hm..." } + +me match { case Person("Kate", number) => "Match"; case _ => "Hm..." } + +me match { case Person("Kate", _) => "Girl"; case Person("George", _) => "Boy" } + +val kate = Person("Kate", "1234") + +kate match { case Person("Kate", _) => "Girl"; case Person("George", _) => "Boy" } + + // Regular expressions + + // Strings From 3f2922d87a1c947eb6e20551846fd1b307984680 Mon Sep 17 00:00:00 2001 From: George Petrov Date: Mon, 29 Jul 2013 16:03:49 +0100 Subject: [PATCH 09/19] Adding some strings and docs links --- scala.html.markdown | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/scala.html.markdown b/scala.html.markdown index 8c000177..a1396b4d 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -85,6 +85,10 @@ 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 + * and make sure you can read it + */ + // Tuples @@ -128,9 +132,6 @@ val text = if (x == 10) "yeah" else "nope" // Object oriented features -class Person - - // Case classes @@ -162,10 +163,32 @@ kate match { case Person("Kate", _) => "Girl"; case Person("George", _) => "Boy" // Regular expressions - +// TODO // Strings +println("ABCDEF".length) +println("ABCDEF".substring(2, 6)) +println("ABCDEF".replace("C", "3")) -// Input and output \ No newline at end of file +val n = 45 +println(s"We have $n apples") + +val a = Array(11, 9, 6) +println(s"My second daughter is ${a(2-1)} years old") + +// Input and output + + +``` + +## Further resources + +[Scala for the impatient](http://horstmann.com/scala/) + +[Twitter Scala school(http://twitter.github.io/scala_school/) + +[The scala documentation] + +Join the [Scala user group](https://groups.google.com/forum/#!forum/scala-user) \ No newline at end of file From a6ec23d414ba495b26a4028f9a6795d18a02cdac Mon Sep 17 00:00:00 2001 From: George Petrov Date: Mon, 29 Jul 2013 18:33:03 +0100 Subject: [PATCH 10/19] Added loops, iterators and regex --- scala.html.markdown | 56 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/scala.html.markdown b/scala.html.markdown index a1396b4d..8bcbc27f 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -93,6 +93,7 @@ s(1) // Tuples + // Combinators s.map(sq) @@ -114,6 +115,37 @@ 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 */ + + + +// Loops and iteration + +1 to 5 +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 + +(5 to 1 by -1) foreach ( println ) + +var i = 0 +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 + +// Tail recursion is an idiomatic way of doing things in Scala. Recursive functions need an +// explicit return type, the compile can't infer it. Here it's Unit. +def showNumbersInRange(a:Int, b:Int):Unit = { print(a); if (a < b) showNumbersInRange(a+1, b) } + // Conditionals @@ -128,11 +160,13 @@ if (x == 11) println ("yeah") else println("nope") println(if (x == 10) "yeah" else "nope") val text = if (x == 10) "yeah" else "nope" - +var i = 0 +while (i < 10) { println("i " + i); i+=1 } // Object oriented features + // Case classes case class Person(name:String, phoneNumber:String) @@ -141,6 +175,7 @@ Person("George", "1234") == Person("Kate", "1236") + // Pattern matching val me = Person("George", "1234") @@ -163,7 +198,14 @@ kate match { case Person("Kate", _) => "Girl"; case Person("George", _) => "Boy" // Regular expressions -// TODO +val email = "(.*)@(.*)".r // The suffix .r invokes method r on String, which makes it a Regex + +val email(user, domain) = "henry@zkpr.com" + +"mrbean@pyahoo.com" match { + case email(name, domain) => "I know your name, " + name +} + // Strings @@ -178,6 +220,16 @@ println(s"We have $n apples") val a = Array(11, 9, 6) 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 +val html = """
+

Press belo', Joe

+ | +
""" + + // Input and output From 8235ddc50fce389356bc9ebb9e1b7249fa8bd5e7 Mon Sep 17 00:00:00 2001 From: George Petrov Date: Mon, 29 Jul 2013 22:19:15 +0100 Subject: [PATCH 11/19] Fixing a typo --- scala.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scala.html.markdown b/scala.html.markdown index 8bcbc27f..57706c1f 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -20,7 +20,7 @@ Scala - the scalable language scala> - This is the so called REPL. You can run commands in the REPL. Let 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 From 82a5e4c8f0b3d79a2797fa0b5cdaf962bd7b3bf4 Mon Sep 17 00:00:00 2001 From: George Petrov Date: Mon, 29 Jul 2013 23:59:12 +0100 Subject: [PATCH 12/19] Fixed header --- scala.html.markdown | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/scala.html.markdown b/scala.html.markdown index 57706c1f..e7a432f6 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -1,8 +1,7 @@ --- -language: scala +language: Scala filename: learn.scala -author: George Petrov -author_url: http://www.github.com/petrovg +contributors:["George Petrov", "http://github.com/petrovg"] --- Scala - the scalable language @@ -43,7 +42,7 @@ println("Boo!") // printlns the string Boo! */ -// Everything is an object, including a function type these in the repl: +// Everything is an object, including a function. Type these in the REPL: 7 // results in res30: Int = 7 (res30 is just a generated var name for the result) From 11edf04311e33fad32c0bc943980482433b63460 Mon Sep 17 00:00:00 2001 From: George Petrov Date: Mon, 29 Jul 2013 23:59:58 +0100 Subject: [PATCH 13/19] Tweaking the header --- scala.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scala.html.markdown b/scala.html.markdown index e7a432f6..49c7bb14 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -1,7 +1,7 @@ --- language: Scala filename: learn.scala -contributors:["George Petrov", "http://github.com/petrovg"] +contributors: ["George Petrov", "http://github.com/petrovg"] --- Scala - the scalable language From 49ee527a79adbe3fde50b71effb4a9340b7ab7dc Mon Sep 17 00:00:00 2001 From: George Petrov Date: Tue, 30 Jul 2013 00:03:28 +0100 Subject: [PATCH 14/19] Header messages --- scala.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scala.html.markdown b/scala.html.markdown index 49c7bb14..b22ba15b 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -1,7 +1,8 @@ --- language: Scala +contributors: + - ["George Petrov", "http://github.com/petrovg"] filename: learn.scala -contributors: ["George Petrov", "http://github.com/petrovg"] --- Scala - the scalable language From 38bd9a18fda39e70060cd64a49a0095ba4901e26 Mon Sep 17 00:00:00 2001 From: George Petrov Date: Wed, 31 Jul 2013 20:52:59 +0100 Subject: [PATCH 15/19] Added tuples --- scala.html.markdown | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/scala.html.markdown b/scala.html.markdown index b22ba15b..8bcb5975 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -92,6 +92,26 @@ s(1) // Tuples +(1, 2) + +(4, 3, 2) + +(1, 2, "three") + +(a, 2, "three") + +// Why have this? +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 +val d = divideInts(10,3) + +d._1 + +d._2 + // Combinators From 906c7164d0975d14da0391ff47b5c384f0dd1c47 Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 2 Aug 2013 09:39:30 -0700 Subject: [PATCH 16/19] Updated scala for line lengths --- scala.html.markdown | 86 ++++++++++++++++++++++++++++----------------- 1 file 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 = - 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 = """

Press belo', Joe

| @@ -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. From 18a5e58b16e6e0b172c010fd3bba07e81701222a Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 2 Aug 2013 09:40:22 -0700 Subject: [PATCH 17/19] Fixed php and removed old VB file --- Visual Basic | 248 ---------------------------------------------- php.html.markdown | 5 +- 2 files changed, 3 insertions(+), 250 deletions(-) delete mode 100644 Visual Basic diff --git a/Visual Basic b/Visual Basic deleted file mode 100644 index 73430633..00000000 --- a/Visual Basic +++ /dev/null @@ -1,248 +0,0 @@ ---- -language: Visual Basic Console Application -contributors: - - ["Brian Martin", "http://brianmartin.biz"] -filename: learnvisualbasic.vb - -Module Module1 - - Sub Main() - 'A Quick Overview of Visual Basic Console Applications before we dive in to the deep end. - 'Apostrophe starts comments. - 'To Navigate this tutorial within the Visual Basic Complier, I've put together a navigation system. - 'This navigation system is explained however as we go deeper into this tutorial, you'll understand what it all means. - Console.Title = ("Learn X in Y Minutes") - Console.WriteLine("NAVIGATION") 'Display - Console.WriteLine("") - Console.ForegroundColor = ConsoleColor.Green - Console.WriteLine("1. Hello World Output") - Console.WriteLine("2. Hello World Input") - Console.WriteLine("3. Calculating Whole Numbers") - Console.WriteLine("4. Calculating Decimal Numbers") - Console.WriteLine("5. Working Calculator") - Console.WriteLine("6. Using Do While Loops") - Console.WriteLine("7. Using For While Loops") - Console.WriteLine("8. Conditional Statements") - Console.WriteLine("9. Select A Drink") - Console.WriteLine("50. About") - Console.WriteLine("Please Choose A Number From The Above List") - Dim selection As String = Console.ReadLine - Select Case selection - Case "1" 'HelloWorld Output - Console.Clear() 'Clears the application and opens the private sub - HelloWorldOutput() 'Name Private Sub, Opens Private Sub - Case "2" 'Hello Input - Console.Clear() - HelloWorldInput() - Case "3" 'Calculating Whole Numbers - Console.Clear() - CalculatingWholeNumbers() - Case "4" 'Calculting Decimal Numbers - Console.Clear() - CalculatingDecimalNumbers() - Case "5" 'Working Calcculator - Console.Clear() - WorkingCalculator() - Case "6" 'Using Do While Loops - Console.Clear() - UsingDoWhileLoops() - Case "7" 'Using For While Loops - Console.Clear() - UsingForLoops() - Case "8" 'Conditional Statements - Console.Clear() - ConditionalStatement() - Case "9" 'If/Else Statement - Console.Clear() - IfElseStatement() 'Select a drink - Case "50" 'About msg box - Console.Clear() - Console.Title = ("Learn X in Y Minutes :: About") - MsgBox("Learn X in Y Minutes is a creation of Adam Bard (@adambard) This particular program tutorial is by Brian Martin (@BrianMartinn") - Console.Clear() - Main() - Console.ReadLine() - - End Select - End Sub - - 'One - I'm using numbers to help with the above navigation when I come back later to build it. - Private Sub HelloWorldOutput() 'We use private subs to seperate different sections of the program. - Console.Title = "Hello World Ouput | Learn X in Y Minutes" 'Title of Console Application - 'Use Console.Write("") or Console.WriteLine("") to print outputs. - 'Followed by Console.Read() alternatively Console.Readline() - 'Console.ReadLine() prints the output to the console. - Console.WriteLine("Hello World") - Console.ReadLine() - End Sub - 'Two - Private Sub HelloWorldInput() 'We use private subs to seperate different sections of the program. - Console.Title = "Hello World YourName | Learn X in Y Minutes" 'Title of Console Application - 'Variables - 'Data entered by a user needs to be stored. - 'Variables also start with a Dim and end with an As VariableType. - Dim username As String 'In this tutorial, we want to know what your name, and make the program respond to what is said. - 'We use string as string is a text based variable. - Console.WriteLine("Hello, What is your name? ") 'Ask the user their name. - username = Console.ReadLine() 'Stores the users name. - Console.WriteLine("Hello " + username) 'Output is Hello 'Their name' - Console.ReadLine() 'Outsputs the above. - 'The above will ask you a question followed by printing your answer. - 'Other variables include Integer and we use Integer for whole numbers. - End Sub - 'Three - Private Sub CalculatingWholeNumbers() 'We use private subs to seperate different sections of the program. - Console.Title = "Calculating Whole Numbers | Learn X in Y Minutes" 'Title of Console Application - Console.Write("First number: ") 'Enter a whole number, 1, 2, 50, 104 ect - Dim a As Integer = Console.ReadLine() - Console.Write("Second number: ") 'Enter second whole number. - Dim b As Integer = Console.ReadLine() - Dim c As Integer = a + b - Console.WriteLine(c) - Console.ReadLine() - 'The above is a simple calculator - End Sub - 'Four - Private Sub CalculatingDecimalNumbers() - Console.Title = "Calculating with Double | Learn X in Y Minutes" 'Title of Console Application - 'Of course we would like to be able to add up decimals. - 'Therefore we could change the above from Integer to Double. - Console.Write("First number: ") 'Enter a whole number, 1.2, 2.4, 50.1, 104.9 ect - Dim a As Double = Console.ReadLine - Console.Write("Second number: ") 'Enter second whole number. - Dim b As Double = Console.ReadLine - Dim c As Double = a + b - Console.WriteLine(c) - Console.ReadLine() - 'Therefore the above program can add up 1.1 - 2.2 - End Sub - 'Five - Private Sub WorkingCalculator() - Console.Title = "The Working Calculator| Learn X in Y Minutes" 'Title of Console Application - 'However if you'd like the calculator to subtract, divide, multiple and add up. - 'Copy and paste the above again. - Console.Write("First number: ") 'Enter a whole number, 1.2, 2.4, 50.1, 104.9 ect - Dim a As Double = Console.ReadLine - Console.Write("Second number: ") 'Enter second whole number. - Dim b As Integer = Console.ReadLine - Dim c As Integer = a + b - Dim d As Integer = a * b - Dim e As Integer = a - b - Dim f As Integer = a / b - 'By adding the below lines we are able to calculate the subtract, multply as well as divide the a and b values - Console.Write(a.ToString() + " + " + b.ToString()) - Console.WriteLine(" = " + c.ToString.PadLeft(3)) 'We want to pad the answers to the left by 3 spaces. - Console.Write(a.ToString() + " * " + b.ToString()) - Console.WriteLine(" = " + d.ToString.PadLeft(3)) 'We want to pad the answers to the left by 3 spaces. - Console.Write(a.ToString() + " - " + b.ToString()) - Console.WriteLine(" = " + e.ToString.PadLeft(3)) 'We want to pad the answers to the left by 3 spaces. - Console.Write(a.ToString() + " / " + b.ToString()) - Console.WriteLine(" = " + e.ToString.PadLeft(3)) 'We want to pad the answers to the left by 3 spaces. - Console.ReadLine() - - End Sub - 'Six - Private Sub UsingDoWhileLoops() - 'Just as the previous private sub - 'This Time We Ask If The User Wishes To Continue (Yes or No?) - 'We're using Do While Loop as we're unsure if the user wants to use the program more than once. - Console.Title = "UsingDoWhileLoops | Learn X in Y Minutes" - Dim answer As String 'We use the variable "String" as the answer is text - Do 'We start the program with - Console.Write("First number: ") - Dim a As Double = Console.ReadLine - Console.Write("Second number: ") - Dim b As Integer = Console.ReadLine - Dim c As Integer = a + b - Dim d As Integer = a * b - Dim e As Integer = a - b - Dim f As Integer = a / b - - Console.Write(a.ToString() + " + " + b.ToString()) - Console.WriteLine(" = " + c.ToString.PadLeft(3)) - Console.Write(a.ToString() + " * " + b.ToString()) - Console.WriteLine(" = " + d.ToString.PadLeft(3)) - Console.Write(a.ToString() + " - " + b.ToString()) - Console.WriteLine(" = " + e.ToString.PadLeft(3)) - Console.Write(a.ToString() + " / " + b.ToString()) - Console.WriteLine(" = " + e.ToString.PadLeft(3)) - Console.ReadLine() - 'Ask the question, does the user wish to continue? Unfortunately it is case sensitive. - Console.Write("Would you like to continue? (yes / no)") - answer = Console.ReadLine 'The program grabs the variable and prints and starts again. - Loop While answer = "yes" 'The command for the variable to work would be in this case "yes" - - End Sub - 'Seven - Private Sub UsingForLoops() - 'Sometimes the program only needs to run once. - 'In this program we'll be counting down from 10. - - Console.Title = "Using For Loops | Learn X in Y Minutes" - For i As Integer = 10 To 0 Step -1 'Declare Vairable and what number it should count down in Step -1, Step -2, Step -3 ect. - Console.WriteLine(i.ToString) 'Print the value of the counter variable - Next i 'Calculate new value - Console.WriteLine("Start") 'Lets start the program baby!! - Console.ReadLine() 'POW!! - Perhaps I got a little excited then :) - End Sub - 'Eight - Private Sub ConditionalStatement() - Console.Title = "Conditional Statements | Learn X in Y Minutes" - Dim userName As String = Console.ReadLine - Console.WriteLine("Hello, What is your name? ") 'Ask the user their name. - userName = Console.ReadLine() 'Stores the users name. - If userName = "Adam" Then 'Hey, if Adam uses this program, kudos where kudos is due, right? - Console.WriteLine("Hello Adam") - Console.WriteLine("Thanks for creating the useful tutorial site www.learnxinyminutes.com!") - Console.ReadLine() - Else - Console.WriteLine("Hello " + userName) 'prints the username of the user - Console.WriteLine("Hope all is well have you checked out www.learnxinyminutes.com") 'Prints a message to the user - Console.ReadLine() 'Ends and prints the above statement. - End If - End Sub - 'Nine - Private Sub IfElseStatement() - Console.Title = "If / Else Statement | Learn X in Y Minutes" - 'Sometimes its important to consider more than two alternatives. Sometimes there are a good few others. - 'When this is the case, more than one if statement would be required. - 'An if statement is great for vending machines. Where the user enters a code. - 'A1, A2, A3, ect to select an item. - 'All choices can be combined into a single if statement. - - Dim selection As String = Console.ReadLine 'Value for selection - Console.WriteLine("A1. for 7Up") - Console.WriteLine("A2. for Fanta") - Console.WriteLine("A3. for Dr. Pepper") - Console.WriteLine("A4. for Diet Coke") - Console.ReadLine() - If selection = "A1" Then - Console.WriteLine("7up") - Console.ReadLine() - ElseIf selection = "A2" Then - Console.WriteLine("fanta") - Console.ReadLine() - ElseIf selection = "A3" Then - Console.WriteLine("dr. pepper") - Console.ReadLine() - ElseIf selection = "A4" Then - Console.WriteLine("diet coke") - Console.ReadLine() - Else - Console.WriteLine("Please select a product") - Console.ReadLine() - End If - - End Sub - -End Module - - -``` -## References - -I learnt Visual Basic in the console application. It allowed me to understand the principles of computer programming to go on to learn other programming languages easily. - -I created a more indepth Visual Basic tutorial for those who would like to learn more. - -The entire syntax is valid. Copy the and paste in to the Visual Basic complier and run (F5) the program. diff --git a/php.html.markdown b/php.html.markdown index 5f5a4b54..ce228870 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -9,7 +9,7 @@ filename: learnphp.php This document describes PHP 5+. ```php - tags +Hello World Again! +?> +Hello World Again! Date: Fri, 2 Aug 2013 09:43:54 -0700 Subject: [PATCH 18/19] corrected scala --- scala.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scala.html.markdown b/scala.html.markdown index 687914ef..a5920aa8 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -1,8 +1,9 @@ --- language: Scala +filename: learnscala.scala contributors: - ["George Petrov", "http://github.com/petrovg"] - - ["Dominic Bou-Samra, "http://dbousamra.github.com"] + - ["Dominic Bou-Samra", "http://dbousamra.github.com"] filename: learn.scala --- From 22f1469dd8092ff46a73e27b56363f7c922ac875 Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 2 Aug 2013 09:47:13 -0700 Subject: [PATCH 19/19] Used c syntax highlighting for scala for now --- scala.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scala.html.markdown b/scala.html.markdown index a5920aa8..fef09404 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -9,7 +9,7 @@ filename: learn.scala Scala - the scalable language -```scala +```cpp /* Set yourself up: