mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
Added for comprehensions and conditionals
This commit is contained in:
parent
7673ae8a6d
commit
04e50a75fa
@ -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
|
||||
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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user