From 04e50a75fa253f71cf5de0f45d423f06dd9933d2 Mon Sep 17 00:00:00 2001 From: George Petrov Date: Mon, 29 Jul 2013 13:53:55 +0100 Subject: 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 + -- cgit v1.2.3