mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-18 04:21:25 +02:00
Merge
This commit is contained in:
@@ -100,7 +100,7 @@ true == false // false
|
||||
|
||||
"Scala strings are surrounded by double quotes"
|
||||
'a' // A Scala Char
|
||||
'Single quote strings don't exist' // Error
|
||||
// 'Single quote strings don't exist' <= This causes an error
|
||||
|
||||
// Strings have the usual Java methods defined on them
|
||||
"hello world".length
|
||||
@@ -204,6 +204,7 @@ def showNumbersInRange(a:Int, b:Int):Unit = {
|
||||
if (a < b)
|
||||
showNumbersInRange(a + 1, b)
|
||||
}
|
||||
showNumbersInRange(1,14)
|
||||
|
||||
|
||||
// Conditionals
|
||||
@@ -218,9 +219,6 @@ if (x == 11) println ("yeah") else println("nay")
|
||||
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 }
|
||||
|
||||
|
||||
#################################################
|
||||
## 4. Data Structures
|
||||
@@ -292,7 +290,8 @@ d._2
|
||||
And now we will explain what these are.
|
||||
*/
|
||||
|
||||
class Dog {
|
||||
class Dog(br: String) {
|
||||
var breed: String = br
|
||||
//A method called bark, returning a String
|
||||
def bark: String = {
|
||||
// the body of the method
|
||||
@@ -300,6 +299,11 @@ class Dog {
|
||||
}
|
||||
}
|
||||
|
||||
val mydog = new Dog("greyhound")
|
||||
println(mydog.breed) // => "greyhound"
|
||||
println(mydog.bark) // => "Woof, woof!"
|
||||
|
||||
|
||||
// Classes can contain nearly any other construct, including other classes,
|
||||
// functions, methods, objects, case classes, traits etc.
|
||||
|
||||
@@ -388,6 +392,7 @@ sSquared.reduce (_+_)
|
||||
// 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)
|
||||
case class Person(name:String, phoneNumber:String)
|
||||
List(
|
||||
Person(name = "Dom", age = 23),
|
||||
Person(name = "Bob", age = 30)
|
||||
@@ -396,6 +401,7 @@ List(
|
||||
|
||||
// Scala a foreach method defined on certain collections that takes a type
|
||||
// returning Unit (a void method)
|
||||
val aListOfNumbers = List(1, 2, 3, 4, 10, 20, 100)
|
||||
aListOfNumbers foreach (x => println(x))
|
||||
aListOfNumbers foreach println
|
||||
|
||||
|
Reference in New Issue
Block a user