Commit 7e2ddc63 authored by Jim Newton's avatar Jim Newton

updated during lecture

parent e0d2dbfb
......@@ -28,6 +28,5 @@ object Greeting {
def main(argv:Array[String]):Unit = {
}
}
......@@ -31,6 +31,7 @@ object Recursion {
}
def sumIntegersByTailRecursion(ints: List[Int]): Int = {
@scala.annotation.tailrec
def sumRest(acc: Int, rest: List[Int]): Int = {
rest match {
case List() => acc
......@@ -50,7 +51,7 @@ object Recursion {
}
def main(argv:Array[String]):Unit = {
val longList = List.tabulate(12)(n => 0)
val longList = List.tabulate(12)(n => 0) // List.fill(...)(...)
println(s"sum = ${sumIntegersBySimpleRecursion(longList)}")
}
}
......@@ -45,7 +45,7 @@ object Theg {
@scala.annotation.tailrec
def recur(edges: List[(Int, Int)], adj: Array[Set[Int]]): Array[Set[Int]] = {
if (edges.isEmpty)
if (edges.isEmpty) // if (edges == List())
adj
else {
val (src, dst) = edges.head
......@@ -185,7 +185,7 @@ object Theg {
// uses + to extend Map
// uses + to extend Set
// omits initialization of Map, just uses Map()
// uses .withDefaultValue rather than .getOrElse
// * uses .withDefaultValue rather than .getOrElse
def makeAdj_8(edges: List[(Int, Int)]): Map[Int, Set[Int]] = {
edges.foldLeft(Map[Int, Set[Int]]().withDefaultValue(Set())) {
case (adj, (src, dst)) =>
......
......@@ -2,3 +2,18 @@ import lecture.Recursion._
sumIntegersBySimpleRecursion(List(1,2,3,4))
import lecture.MetroData
// example of fold with input type same as
// output type
// example of foldLeft with input type different
// than output type
MetroData.stationPositions
MetroData.stationPositions
1.min(2)
import lecture.Greeting
1 + 2 + 3
val x = 100
val y = 200
val z = x - y
Set(1,2,3,4,2,3,4,-1,-2,4,5).fold(1)(_ * _)
import lecture.MetroData
((x:Int) => 0)
((x:Int) => x)
((x:Int,y:Int) => 0)
def f(x:Int)(y:String)(z:String):Double = {
....
}
val y = f(3)
y("hello")
import lecture.Theg._
import lecture.MetroData
makeAdj_1(3, List((0, 1), (1, 2)))
val adj = makeAdj_1(3, List((0, 1), (0,2), (1, 2)))
adj(0)
adj(2)
makeAdj_1(3, List((0, 1), (0,1), (1, 2)))
makeAdj_4(3, List((0, 1), (0,1), (1, 2)))
(1,2)
(1 -> 2)
1 -> 2
List(1->2, 2->3, 3->4)
val ar = Array(10,20,10,20,30)
val li = List(20,30,20,30,40,50)
val se = Set(10,20,40, 10,20,30,20,30)
val ma = Map("a" -> 30,
"b" -> 20)
ma + ("c" -> 40)
ma.contains("c")
ma.get("c")
ma.get("a")
ma("a")
ma.getOrElse("c",42)
ma.getOrElse("a",42)
val ma3 = ma.withDefaultValue(42)
ma3("c")
ma.get("a") match {
case None => 42
case Some(v) => v
}
li match {
case h :: t => h
case List() => 42
}
val ma2 = Map("a" ->true,
"b" ->false)
ma2("b")
ar(2)
ar(3)
ar(0)
li(3)
ma("a")
ma("b")
se + 15
se
se ++ Set(15,16,20,17)
se
ar.updated(1,100)
ar
li
100::li
li
li ++ li
ar ++ ar
ar
se - 10
se
se -- Set(10,30)
se.diff(Set(10,30))
//MetroData.stationPositions
//MetroData.stationPositions.toList
//MetroData.stationPositions.toSet
//MetroData.stationPositions.map{
// case (name,x,y) => name -> (x,y)
//}.toMap
//MetroData.stationPositions.groupBy(_._1)
//MetroData.stationPositions.groupBy(_._1).map{
// case (name,triple) => (name -> triple.size)
//}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment