Created
March 22, 2016 22:08
-
-
Save pierangeloc/7953aa429ba9905475ec to your computer and use it in GitHub Desktop.
REPL after the Gentle Scala Introduction @ Sytac DevJam
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* For the first part I had to rely on the repl history as the console buffer was gone after the stack overflow of factorial | |
*/ | |
val i = 10 | |
:type i | |
val i: Int = 10 | |
val s = "Ciao" | |
i = 4 | |
var j = 20 | |
j = 10 | |
val i: Int = 5 | |
i = 6 | |
var j: Int = 6 | |
j = 8 | |
val k = 8 | |
val s = "Piero" | |
s == "Piero" | |
1 + 2 | |
1.+(2) | |
def add(x: Int, y: Int) = x + y | |
def add(x: Int, y: Int) = { | |
x = 0 | |
x + y | |
} | |
val x = 1 | |
x + 5 | |
x.+(5) | |
class Point(x: Double, y: Double) | |
class Point(x: Double, y: Double) { | |
def +(other: Point): Point = new Point(x + other.x, y + other.y) | |
} | |
class Point(val x: Double, val y: Double) { | |
def +(other: Point): Point = new Point(x + other.x, y + other.y) | |
} | |
val p1 = new Point(4,5) | |
val p2 = new Point(3,4) | |
p1.+(p2) | |
class Point(val x: Double, val y: Double) { | |
def +(other: Point): Point = new Point(x + other.x, y + other.y) | |
override def toString() = "Point(" + x + "," + y + ")" | |
} | |
val p1 = new Point(4,5) | |
val p2 = new Point(3,4) | |
p1.+(p2) | |
p1 + p2 | |
val mul = (x: Int, y: Int) => x * y | |
:type mul | |
mul(4,5) | |
def mul2(x: Int, y: Int) = x * 2 | |
def mul2(x: Int, y: Int) = x * y | |
mul2(4,5) | |
val add4 = new Function[Int, Int] { | |
def apply(x: Int): Int = x + 4 | |
} | |
add4.apply(8) | |
add4(8) | |
val environment = "prod" | |
val dbUrl = if (environment == "prod") "jdbc...prod" else "jbdc...test" | |
val add2 = (x: Int) => x + 2 | |
val mul5 = (x: Int) => x * 5 | |
val comp = add2.compose(mul5) | |
comp(2) | |
val comp = add2 compose mul5 | |
val comp2 = mul5 andThen add2 | |
def fact(n: BigInt): BigInt = { | |
if (n == 1) n | |
else n * fact(n - 1) | |
} | |
fact(3) | |
fact(10) | |
fact(20000) // BOOM! | |
scala> def fact(n: BigInt): BigInt = { | |
| else n * fact(n - 1) | |
<console>:2: error: illegal start of statement | |
else n * fact(n - 1) | |
^ | |
scala> def fact(n: BigInt): BigInt = { | |
| if (n == 1) n | |
| else n * fact(n - 1) | |
| } | |
fact: (n: BigInt)BigInt | |
scala> def fact2(n: BigInt): BigInt = { | |
| def go(acc: BigInt, k: BigInt) = { | |
| if (k == 1) acc else go(k * acc, k - 1) | |
| } | |
| | |
| | |
You typed two blank lines. Starting a new command. | |
scala> def fact2(n: BigInt): BigInt = { | |
| def go(acc: BigInt, k: BigInt): BigInt = { | |
| if (k == 1) acc else go(k * acc, k - 1) | |
| } | |
| go(1, n) | |
| } | |
fact2: (n: BigInt)BigInt | |
scala> fact2(3) | |
res16: BigInt = 6 | |
scala> fact2(5) | |
res17: BigInt = 120 | |
scala> fact2(20000) | |
res18: BigInt = 1819206320230345134827641756866458766071609901478752648918062218634569461038557534453836095827758724739177502384189912041671245383926576865723475464524930275709204625357875789525900491823414542718036070392521085047283383729296861580052404638841199526622379791066223341463393474395925629787482113163418647665455586660436184087223951946276615548561632208981651545091872218436294406051877306392783358445931186417148398260080400146928105895643738692767105374763186090336511780868599512519843617384918221869858019078196339128759665295841157777813491489409939230578103105905988720691493952696699343574983659137962578903705716902337854828253406978492916531166704020223408974723378025728021785188391510797310297013143732956206337141728256129448819212285111707757412048942243223685263626707... | |
scala> val numbers = List(4,2,6,8,9,4) | |
numbers: List[Int] = List(4, 2, 6, 8, 9, 4) | |
scala> val strings: List[String] = List("Piero", "Carlo", "Davide") | |
strings: List[String] = List(Piero, Carlo, Davide) | |
scala> val strings: List[String] = List(1,2,3) | |
<console>:10: error: type mismatch; | |
found : Int(1) | |
required: String | |
val strings: List[String] = List(1,2,3) | |
^ | |
<console>:10: error: type mismatch; | |
found : Int(2) | |
required: String | |
val strings: List[String] = List(1,2,3) | |
^ | |
<console>:10: error: type mismatch; | |
found : Int(3) | |
required: String | |
val strings: List[String] = List(1,2,3) | |
^ | |
scala> | |
scala> | |
scala> def sum(ns: List[Int]): Int = { | |
| var res = 0 | |
| for { | |
| i <- ns | |
| } res + 1 | |
| res | |
| } | |
sum: (ns: List[Int])Int | |
scala> sum(numbers) | |
res19: Int = 0 | |
scala> def sum(ns: List[Int]): Int = { | |
| var res = 0 | |
| for { | |
| i <- ns | |
| } res = res + i | |
| res | |
| } | |
sum: (ns: List[Int])Int | |
scala> sum(numbers) | |
res20: Int = 33 | |
scala> def sumRec(ns: List[Int]): Int = { | |
| if (ns.isEmpty()) 0 | |
| else ns(0) + sumRec(ns.tail) | |
| } | |
<console>:11: error: Boolean does not take parameters | |
if (ns.isEmpty()) 0 | |
^ | |
scala> def sumRec(ns: List[Int]): Int = { | |
| if (ns.isEmpty) 0 | |
| else ns(0) + sumRec(ns.tail) | |
| } | |
sumRec: (ns: List[Int])Int | |
scala> sumRec(numbers) | |
res21: Int = 33 | |
scala> def sumFold(ns: List[Int]): Int = { | |
| ns.foldLeft(0)((x: Int, y: Int) => x + y) | |
| } | |
sumFold: (ns: List[Int])Int | |
scala> sumFold(numbers) | |
res22: Int = 33 | |
scala> def sumFoldR(ns: List[Int]): Int = ns.foldRight(0)(_ + _) | |
sumFoldR: (ns: List[Int])Int | |
scala> sumFoldR(numbers) | |
res23: Int = 33 | |
scala> sumFoldR(List()) | |
res24: Int = 0 | |
scala> numbers.foldRight(5)(_ + _) | |
res25: Int = 38 | |
scala> numbers.foldRight(5)(_ - _) | |
res26: Int = 10 | |
scala> numbers.foldLeft(5)(_ - _) | |
res27: Int = -28 | |
scala> numbers | |
res28: List[Int] = List(4, 2, 6, 8, 9, 4) | |
scala> numbers.foldLeft("")((x, y) => x + "," + y) | |
res29: String = ,4,2,6,8,9,4 | |
scala> val empty = Nil | |
empty: scala.collection.immutable.Nil.type = List() | |
scala> List() | |
res30: List[Nothing] = List() | |
scala> val oneVal = List(1) | |
oneVal: List[Int] = List(1) | |
scala> val oneVal = 1 :: Nil | |
oneVal: List[Int] = List(1) | |
scala> numbers | |
res31: List[Int] = List(4, 2, 6, 8, 9, 4) | |
scala> 4 :: 2 :: 6 :: 8 :: 9 :: 4 :: Nil | |
res32: List[Int] = List(4, 2, 6, 8, 9, 4) | |
scala> :type :: | |
collection.immutable.::.type | |
scala> :type ::_ | |
() => collection.immutable.::.type | |
scala> :type ::[Int]_ | |
(Int, List[Int]) => scala.collection.immutable.::[Int] | |
scala> def sumPM(nrs: List[Int]) = nrs match { | |
| case Nil => 0 | |
| case n :: ns => n + sumPM(ns) | |
| } | |
<console>:12: error: recursive method sumPM needs result type | |
case n :: ns => n + sumPM(ns) | |
^ | |
scala> def sumPM(nrs: List[Int]): Int = nrs match { | |
| case Nil => 0 | |
| case n :: ns => n + sumPM(ns) | |
| } | |
sumPM: (nrs: List[Int])Int | |
scala> sumPM(numbers) | |
res33: Int = 33 | |
scala> def addFirst2(nrs: List[Int]): Int = nrs match { | |
| case n0 :: n1 :: ns => n0 + n1 | |
| case _ => 0 | |
| } | |
addFirst2: (nrs: List[Int])Int | |
scala> addFirst2(List(1,2,3,4)) | |
res34: Int = 3 | |
scala> case class Point(x: Double, y: Double) | |
defined class Point | |
scala> val p1 = Point(3,4) | |
p1: Point = Point(3.0,4.0) | |
scala> val p2 = Point(5,6) | |
p2: Point = Point(5.0,6.0) | |
scala> | |
scala> p1.x | |
res35: Double = 3.0 | |
scala> val points = List(Point(3,4), Point(3,6), Point(8,7)) | |
points: List[Point] = List(Point(3.0,4.0), Point(3.0,6.0), Point(8.0,7.0)) | |
scala> point.filter { case Point(3, _) => true | |
| case _ => false | |
| } | |
<console>:13: error: not found: value point | |
point.filter { case Point(3, _) => true | |
^ | |
scala> points.filter { case Point(3, _) => true | |
| case _ => false | |
| } | |
res37: List[Point] = List(Point(3.0,4.0), Point(3.0,6.0)) | |
scala> object Factory { | |
| def giveString = "something" | |
| } | |
defined object Factory | |
scala> Factory.giveString | |
res38: String = something | |
scala> val pair = (1, "Scala") | |
pair: (Int, String) = (1,Scala) | |
scala> def getIfEven(pair: (Int, String)) = pair match { | |
| case (i, s) if i % 2 == 0 => s | |
| case _ => "unmatched" | |
| } | |
getIfEven: (pair: (Int, String))String | |
scala> getIfEven((2, "Java")) | |
res39: String = Java | |
scala> getIfEven((1, "Scala")) | |
res40: String = unmatched | |
scala> List("Scala", "Java", "Clojure", "C++", "Lisp") | |
res41: List[String] = List(Scala, Java, Clojure, C++, Lisp) | |
scala> val languages = List("Scala", "Java", "Clojure", "C++", "Lisp") | |
languages: List[String] = List(Scala, Java, Clojure, C++, Lisp) | |
scala> languages.groupBy(_.length) | |
res42: scala.collection.immutable.Map[Int,List[String]] = Map(5 -> List(Scala), 4 -> List(Java, Lisp), 7 -> List(Clojure), 3 -> List(C++)) | |
scala> languages.filter(_.lenght < 5) | |
<console>:12: error: value lenght is not a member of String | |
languages.filter(_.lenght < 5) | |
^ | |
scala> languages.filter(_.length < 5) | |
res44: List[String] = List(Java, C++, Lisp) | |
scala> languages.filter(_.length >= 5) | |
res45: List[String] = List(Scala, Clojure) | |
scala> languages.partition(_.length < 5) | |
res46: (List[String], List[String]) = (List(Java, C++, Lisp),List(Scala, Clojure)) | |
/** | |
* From this moment we turned into intellij Scala console | |
*/ | |
/usr/lib/jvm/java-8-oracle/bin/java -Djline.terminal=NONE -Didea.launcher.port=7533 -Didea.launcher.bin.path=/home/pierangeloc/tools/idea-IU-143.1821.5/bin -Dfile.encoding=UTF-8 -classpath /home/pierangeloc/platforms/activator-dist-1.3.7/repository/org.scala-lang/scala-compiler/2.11.6/jars/scala-compiler.jar:/home/pierangeloc/platforms/activator-dist-1.3.7/repository/org.scala-lang/scala-library/2.11.6/jars/scala-library.jar:/home/pierangeloc/platforms/activator-dist-1.3.7/repository/org.scala-lang/scala-reflect/2.11.6/jars/scala-reflect.jar:/home/pierangeloc/.IntelliJIdea15/config/plugins/Scala/lib/scala-plugin-runners.jar:/usr/lib/jvm/java-8-oracle/jre/lib/charsets.jar:/usr/lib/jvm/java-8-oracle/jre/lib/deploy.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-oracle/jre/lib/javaws.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfr.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfxswt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jsse.jar:/usr/lib/jvm/java-8-oracle/jre/lib/management-agent.jar:/usr/lib/jvm/java-8-oracle/jre/lib/plugin.jar:/usr/lib/jvm/java-8-oracle/jre/lib/resources.jar:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar:/home/pierangeloc/Documents/projects/scala/presentations/scala-gentle-intro/target/scala-2.11/classes:/home/pierangeloc/.ivy2/cache/com.typesafe/config/bundles/config-1.2.1.jar:/home/pierangeloc/platforms/activator-dist-1.3.7/repository/com.typesafe.akka/akka-actor_2.11/2.3.11/jars/akka-actor_2.11.jar:/home/pierangeloc/platforms/activator-dist-1.3.7/repository/com.typesafe.akka/akka-testkit_2.11/2.3.11/jars/akka-testkit_2.11.jar:/home/pierangeloc/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.11.2.jar:/home/pierangeloc/.ivy2/cache/org.scala-lang.modules/scala-xml_2.11/bundles/scala-xml_2.11-1.0.2.jar:/home/pierangeloc/.ivy2/cache/org.scalatest/scalatest_2.11/bundles/scalatest_2.11-2.2.4.jar:/home/pierangeloc/tools/idea-IU-143.1821.5/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain org.jetbrains.plugins.scala.compiler.rt.ConsoleRunner -usejavacp | |
Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_66). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> | |
class Person(val name: String, val surname: String) { | |
assert(name.length > 5) | |
override def toString = name + " " + surname | |
} | |
| | | | defined class Person | |
scala> new Person("Richard", "Feynman") | |
res0: Person = Richard Feynman | |
scala> new Person("Rick", "Feynman") | |
java.lang.AssertionError: assertion failed | |
at scala.Predef$.assert(Predef.scala:151) | |
... 36 elided | |
scala> | |
asdf | |
<console>:8: error: not found: value asdf | |
asdf | |
^ | |
scala> val languages = List("Scala", "Java", "Clojure", "C++", "Lisp") | |
languages: List[String] = List(Scala, Java, Clojure, C++, Lisp) | |
scala> languages.partition(_.length > 5) | |
res3: (List[String], List[String]) = (List(Clojure),List(Scala, Java, C++, Lisp)) | |
scala> val lengthOf = (s: String) => s.length | |
lengthOf: String => Int = <function1> | |
scala> lengthOf("Scala") | |
res4: Int = 5 | |
scala> | |
languages.map(lengthOf) | |
res5: List[Int] = List(5, 4, 7, 3, 4) | |
scala> case class Point(x: Double, y: Double) | |
defined class Point | |
scala> val points = List(Point(3,4), Point(4,5), Point(9,8)) | |
points: List[Point] = List(Point(3.0,4.0), Point(4.0,5.0), Point(9.0,8.0)) | |
scala> points.map(p => p.x) | |
res6: List[Double] = List(3.0, 4.0, 9.0) | |
scala> points.map(_.x) | |
res7: List[Double] = List(3.0, 4.0, 9.0) | |
scala> val numbers = List(3,4,7,3,1,8,89,4) | |
numbers: List[Int] = List(3, 4, 7, 3, 1, 8, 89, 4) | |
scala> numbers.take(3) | |
res8: List[Int] = List(3, 4, 7) | |
scala> numbers.drop(3) | |
res9: List[Int] = List(3, 1, 8, 89, 4) | |
scala> numbers.takeWhile(_ < 10) | |
res10: List[Int] = List(3, 4, 7, 3, 1, 8) | |
scala> numbers.dropWhile(_ < 10) | |
res11: List[Int] = List(89, 4) | |
scala> numbers.filter( _ < 5) | |
res12: List[Int] = List(3, 4, 3, 1, 4) | |
scala> | |
numbers.sum | |
res13: Int = 119 | |
scala> val names = List("Richard", "Albert", "Alice") | |
names: List[String] = List(Richard, Albert, Alice) | |
scala> names.sum | |
<console>:9: error: could not find implicit value for parameter num: Numeric[String] | |
names.sum | |
^ | |
scala> val interval = 1 to 10 | |
interval: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
scala> val interval = 1 until 10 | |
interval: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9) | |
scala> val interval = 1 until 10 toList | |
warning: there was one feature warning; re-run with -feature for details | |
interval: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9) | |
scala> interval.map(n => (1 to n).toList) | |
res15: List[List[Int]] = List(List(1), List(1, 2), List(1, 2, 3), List(1, 2, 3, 4), List(1, 2, 3, 4, 5), List(1, 2, 3, 4, 5, 6), List(1, 2, 3, 4, 5, 6, 7), List(1, 2, 3, 4, 5, 6, 7, 8), List(1, 2, 3, 4, 5, 6, 7, 8, 9)) | |
scala> interval.map(n => (1 to n).toList.map(k => (n, k))) | |
res16: List[List[(Int, Int)]] = List(List((1,1)), List((2,1), (2,2)), List((3,1), (3,2), (3,3)), List((4,1), (4,2), (4,3), (4,4)), List((5,1), (5,2), (5,3), (5,4), (5,5)), List((6,1), (6,2), (6,3), (6,4), (6,5), (6,6)), List((7,1), (7,2), (7,3), (7,4), (7,5), (7,6), (7,7)), List((8,1), (8,2), (8,3), (8,4), (8,5), (8,6), (8,7), (8,8)), List((9,1), (9,2), (9,3), (9,4), (9,5), (9,6), (9,7), (9,8), (9,9))) | |
scala> interval.map(n => (1 to n).toList.map(k => (n, k))).flatten | |
res17: List[(Int, Int)] = List((1,1), (2,1), (2,2), (3,1), (3,2), (3,3), (4,1), (4,2), (4,3), (4,4), (5,1), (5,2), (5,3), (5,4), (5,5), (6,1), (6,2), (6,3), (6,4), (6,5), (6,6), (7,1), (7,2), (7,3), (7,4), (7,5), (7,6), (7,7), (8,1), (8,2), (8,3), (8,4), (8,5), (8,6), (8,7), (8,8), (9,1), (9,2), (9,3), (9,4), (9,5), (9,6), (9,7), (9,8), (9,9)) | |
scala> interval.flaMap(n => (1 to n).toList.map(k => (n, k))) | |
<console>:9: error: value flaMap is not a member of List[Int] | |
interval.flaMap(n => (1 to n).toList.map(k => (n, k))) | |
^ | |
scala> interval.flatMap(n => (1 to n).toList.map(k => (n, k))) | |
res19: List[(Int, Int)] = List((1,1), (2,1), (2,2), (3,1), (3,2), (3,3), (4,1), (4,2), (4,3), (4,4), (5,1), (5,2), (5,3), (5,4), (5,5), (6,1), (6,2), (6,3), (6,4), (6,5), (6,6), (7,1), (7,2), (7,3), (7,4), (7,5), (7,6), (7,7), (8,1), (8,2), (8,3), (8,4), (8,5), (8,6), (8,7), (8,8), (9,1), (9,2), (9,3), (9,4), (9,5), (9,6), (9,7), (9,8), (9,9)) | |
scala> interval | |
res20: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9) | |
scala> def around(n: Int) = List(n -1 , n, n + 1) | |
around: (n: Int)List[Int] | |
scala> around(3) | |
res21: List[Int] = List(2, 3, 4) | |
scala> interval.map(around) | |
res22: List[List[Int]] = List(List(0, 1, 2), List(1, 2, 3), List(2, 3, 4), List(3, 4, 5), List(4, 5, 6), List(5, 6, 7), List(6, 7, 8), List(7, 8, 9), List(8, 9, 10)) | |
scala> interval.flatMap(around) | |
res23: List[Int] = List(0, 1, 2, 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7, 6, 7, 8, 7, 8, 9, 8, 9, 10) | |
scala> interval.map(_ * 2) | |
res24: List[Int] = List(2, 4, 6, 8, 10, 12, 14, 16, 18) | |
scala> interval.flatMap(x => List(x * 2)) | |
res25: List[Int] = List(2, 4, 6, 8, 10, 12, 14, 16, 18) | |
scala> interval.first | |
<console>:9: error: value first is not a member of List[Int] | |
interval.first | |
^ | |
scala> interval.first | |
<console>:9: error: value first is not a member of List[Int] | |
interval.first | |
^ | |
scala> interval.head | |
res28: Int = 1 | |
scala> interval = 1 to 10 tolist | |
<console>:8: error: reassignment to val | |
interval = 1 to 10 tolist | |
^ | |
scala> interval = 1 to 10 toList | |
<console>:8: error: reassignment to val | |
interval = 1 to 10 toList | |
^ | |
scala> val interval = 1 to 10 toList | |
warning: there was one feature warning; re-run with -feature for details | |
interval: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
scala> interval.headOption | |
res29: Option[Int] = Some(1) | |
scala> List().headOption | |
res30: Option[Nothing] = None | |
scala> List().head | |
java.util.NoSuchElementException: head of empty list | |
at scala.collection.immutable.Nil$.head(List.scala:420) | |
at scala.collection.immutable.Nil$.head(List.scala:417) | |
... 35 elided | |
scala> | |
val capitals = Map("France" -> "Paris", "Italy" -> "Rome", "Spain" -> "Madrid") | |
capitals: scala.collection.immutable.Map[String,String] = Map(France -> Paris, Italy -> Rome, Spain -> Madrid) | |
scala> capitals.get("France") | |
res32: Option[String] = Some(Paris) | |
scala> capitals.get("UK") | |
res33: Option[String] = None | |
scala> capitals.get("France").map(_.toUpperCase()) | |
res34: Option[String] = Some(PARIS) | |
scala> capitals.get("UK").map(_.toUpperCase()) | |
res35: Option[String] = None | |
scala> capitals.get("UK").map(_.length) | |
res36: Option[Int] = None | |
scala> capitals.get("France").map(_.length) | |
res37: Option[Int] = Some(5) | |
scala> val populations = Map("Paris" -> 15000000, "Rome" -> 4000000, "London" -> 10000000) | |
populations: scala.collection.immutable.Map[String,Int] = Map(Paris -> 15000000, Rome -> 4000000, London -> 10000000) | |
scala> populations("Paris") | |
res38: Int = 15000000 | |
scala> populations("Madrid") | |
java.util.NoSuchElementException: key not found: Madrid | |
at scala.collection.MapLike$class.default(MapLike.scala:228) | |
at scala.collection.AbstractMap.default(Map.scala:59) | |
at scala.collection.MapLike$class.apply(MapLike.scala:141) | |
at scala.collection.AbstractMap.apply(Map.scala:59) | |
... 35 elided | |
scala> populations.get("Madrid") | |
res40: Option[Int] = None | |
scala> def getPopulationOfCapital(country: String): Option[Int] = { | |
capitals.get(country).flatMap(capital => populations.get(capital)) | |
} | |
| | getPopulationOfCapital: (country: String)Option[Int] | |
scala> getPopulationOfCapital("France") | |
res41: Option[Int] = Some(15000000) | |
scala> getPopulationOfCapital("UK") | |
res42: Option[Int] = None | |
scala> getPopulationOfCapital("Spain") | |
res43: Option[Int] = None | |
scala> def getPopulationOfCapital2(country: String): Option[Int] = { | |
for { | |
capital <- capitals.get(country) | |
population <- populations.get(capital) | |
} yield population | |
} | |
| | | | | getPopulationOfCapital2: (country: String)Option[Int] | |
scala> def getPopulationOfCapital3(country: String): Option[String] = { | |
for { | |
capital <- capitals.get(country) | |
population <- populations.get(capital) | |
} yield s"The population of the capital of $country is $population" | |
} | |
| | | | | getPopulationOfCapital3: (country: String)Option[String] | |
scala> getPopulationOfCapital3("France") | |
res44: Option[String] = Some(The population of the capital of France is 15000000) | |
scala> getPopulationOfCapital3("France") match { | |
case Some(x) => x | |
case None => "Not found" | |
} | |
| | | res45: String = The population of the capital of France is 15000000 | |
scala> val numbers = 1 to 4 | |
numbers: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4) | |
scala> val numbers = 1 to 4.toList | |
<console>:7: error: value toList is not a member of Int | |
val numbers = 1 to 4.toList | |
^ | |
scala> val numbers = 1 to 4 toList | |
warning: there was one feature warning; re-run with -feature for details | |
numbers: List[Int] = List(1, 2, 3, 4) | |
scala> for { | |
i <- numbers | |
j <- numbers.map(_ * 2) | |
} yield (i,j) | |
| | | res46: List[(Int, Int)] = List((1,2), (1,4), (1,6), (1,8), (2,2), (2,4), (2,6), (2,8), (3,2), (3,4), (3,6), (3,8), (4,2), (4,4), (4,6), (4,8)) | |
scala> :quit | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment