mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
update Data Structures section to show outputs
This commit is contained in:
parent
82deb66194
commit
069e127ff8
@ -278,21 +278,21 @@ val text = if (x == 10) "yeah" else "nope"
|
|||||||
/////////////////////////////////////////////////
|
/////////////////////////////////////////////////
|
||||||
|
|
||||||
val a = Array(1, 2, 3, 5, 8, 13)
|
val a = Array(1, 2, 3, 5, 8, 13)
|
||||||
a(0)
|
a(0) // Int = 1
|
||||||
a(3)
|
a(3) // Int = 5
|
||||||
a(21) // Throws an exception
|
a(21) // Throws an exception
|
||||||
|
|
||||||
val m = Map("fork" -> "tenedor", "spoon" -> "cuchara", "knife" -> "cuchillo")
|
val m = Map("fork" -> "tenedor", "spoon" -> "cuchara", "knife" -> "cuchillo")
|
||||||
m("fork")
|
m("fork") // java.lang.String = tenedor
|
||||||
m("spoon")
|
m("spoon") // java.lang.String = cuchara
|
||||||
m("bottle") // Throws an exception
|
m("bottle") // Throws an exception
|
||||||
|
|
||||||
val safeM = m.withDefaultValue("no lo se")
|
val safeM = m.withDefaultValue("no lo se")
|
||||||
safeM("bottle")
|
safeM("bottle") // java.lang.String = no lo se
|
||||||
|
|
||||||
val s = Set(1, 3, 7)
|
val s = Set(1, 3, 7)
|
||||||
s(0)
|
s(0) // Boolean = false
|
||||||
s(1)
|
s(1) // Boolean = true
|
||||||
|
|
||||||
/* Look up the documentation of map here -
|
/* Look up the documentation of map here -
|
||||||
* http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Map
|
* http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Map
|
||||||
@ -313,15 +313,16 @@ s(1)
|
|||||||
// Why have this?
|
// Why have this?
|
||||||
val divideInts = (x: Int, y: Int) => (x / y, x % y)
|
val divideInts = (x: Int, y: Int) => (x / y, x % y)
|
||||||
|
|
||||||
divideInts(10, 3) // The function divideInts gives you the result and the remainder
|
// The function divideInts gives you the result and the remainder
|
||||||
|
divideInts(10, 3) // (Int, Int) = (3,1)
|
||||||
|
|
||||||
// To access the elements of a tuple, use _._n where n is the 1-based index of
|
// To access the elements of a tuple, use _._n where n is the 1-based index of
|
||||||
// the element
|
// the element
|
||||||
val d = divideInts(10, 3)
|
val d = divideInts(10, 3) // (Int, Int) = (3,1)
|
||||||
|
|
||||||
d._1
|
d._1 // Int = 3
|
||||||
|
|
||||||
d._2
|
d._2 // Int = 1
|
||||||
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////
|
/////////////////////////////////////////////////
|
||||||
|
Loading…
Reference in New Issue
Block a user