package example
object SetTest extends App {
val colors1 = Set("Blue", "Green", "Red")
println(s"colors1: $colors1")
var colors2 = colors1 + "Black" + "White"
println(s"colors2: $colors2")
println(s"colors1: $colors1")
println(s"colors1 class=${colors1.getClass}")
println(s"colors2 class=${colors2.getClass}")
}
object SetUseTest extends App {
val feeds1 = Set("blog.toolshed.com", "pragdave.me", "blog.agiledeveloper.com")
val feeds2 = Set("blog.toolshed.com", "martinfowler.com/bliki")
val blogFeeds = feeds1 filter(_ contains "blog")
println(s"blog feeds: ${blogFeeds.mkString(", ")}")
val mergeFeeds = feeds1 ++ feeds2
println(s"# of merged feeds: ${mergeFeeds.size}")
val commonFeeds = feeds1 & feeds2
println(s"common feeds: ${commonFeeds.mkString(", ")}")
val urls = feeds1 map ("http://" + _)
println(s"One url: ${urls.head}")
println("Refresh Feeds:")
feeds1 foreach {f=>println(s" Refreshing $f")}
}
object MapUseTest extends App {
val feeds = Map(
"Andy Hunt" -> "blog.toolshed.com",
"Dave Thomas" -> "pragdave.me",
"NFJS" -> "nofluffjuststuff.com/blog"
)
val filterNameStartWithA = feeds filterKeys( _ startsWith "A")
println(s"# of Filtered: ${filterNameStartWithA.size}")
val filterNameStartWithDAndPragprogInFeed = feeds filter {element =>
val (key, value) = element
(key startsWith "D") && (value contains "pragdave")
}
print("# of feeds with auth name D* and pragdave in URL: ")
println(filterNameStartWithDAndPragprogInFeed.size)
println(s"Get Andy's Feed: ${feeds.get("Andy Hunt")}")
println(s"Get Bill's Feed: ${feeds.get("Bill Who")}")
try{
println(s"Get Andy's Feed: ${feeds("Andy Hunt")}")
print("Get Bill's Feed: ")
println(feeds("Bill Who"))
}catch{
case _: java.util.NoSuchElementException => println("Not found")
}
val name = "Venkat Subramaniam"
val value="blog.agiledeveloper.com"
val newFeeds1 = feeds.updated(name, value)
println("Venkat's blog in original feeds: " + feeds.get(name))
println("Venkat's blog in new feeds: " + newFeeds1.get(name))
val mutableFeeds = scala.collection.mutable.Map (
"Scala Book Forum" -> "forums.pragprog.com/forums/87"
)
mutableFeeds("Groovy Book Forum") = "forums.pragprog.com/forums/246"
println(s"Number of forums: ${mutableFeeds.size}")
}
class People(val name: String, val friends: Seq[String]);
object UseFlatMap {
def main(args: Array[String]):Unit = {
val p1 = new People("小红", Seq("朋友1", "朋友2", "朋友3"));
val p2 = new People("小明", Seq("朋友3", "朋友4", "朋友5"));
val seqs = Seq[People](p1, p2)
println("map的形式")
val c = seqs.map(f => f.friends)
println(c)
val d = seqs.flatMap(f => f.friends)
println("flatMap的形式")
println(d)
}
}
object UseListDemo extends App {
val feeds = List("blog.toolshed.com",
"pragdave.me",
"blog.agiledeveloper.com")
println(s"First feed: ${feeds.head}")
println(s"Second feed: ${feeds(1)}")
val prefixedList = "forums.pragprog.com/forums/87" :: feeds
println(s"First Feed In Prefixed: ${prefixedList.head}")
val feedsWithForums =
feeds ::: List(
"forums.prgprog.com/forums/87",
"forums.pragprog.com/forums/246"
)
println(s"First feed in feeds with forum: ${feedsWithForums.head}")
println(s"Last feed in feeds with forum: ${feedsWithForums.last}")
val appenedList = feeds ::: List("agilelearner.com")
println(s"Last feed in Appended: ${appenedList.last}")
println(s"Feeds with blog: ${feeds.filter(_ contains "blog").mkString(", ")}")
println(s"All feeds have com: ${feeds.forall(_ contains "com")}")
println(s"All feeds have dave: ${feeds.forall(_ contains "dave")}")
println(s"Any feeds has dave: ${feeds.exists(_ contains "dave")}")
println(s"Any feeds has bill: ${feeds.exists(_ contains "bill")}")
println(s"Feed url lengths: ${feeds.map(_.length).mkString(", ")}")
val total = feeds.foldLeft(0){(total, feed)=>total+feed.length}
println(s"Total length of feed urls: $total")
}
object UseForDemo extends App{
for( _ <- 1 to 3) { print("ho ")}
val result = for(i<- 1 to 10)
yield i * 2
println()
println(result)
val result2 = (1 to 10).map(_ * 2)
println(result2)
val tripEven = for ( i <- 1 to 10; if i%2 == 0)
yield i * 3
println(tripEven)
val tripEven2 = for {
i <- 1 to 10
if i%2 == 0
} yield i * 3
println(tripEven2)
class Person(val firstName: String, val lastName: String)
object Person {
def apply(firstName: String, lastName: String): Person =
new Person(firstName, lastName)
}
val firends = List(Person("Brian", "Sletten"),
Person("Neal", "Ford"),
Person("Scott", "Davis"),
Person("Stuart", "Halloway")
)
val lastNames =
for( friend <- firends; lastName = friend.lastName) yield lastName
println(lastNames.mkString(", "))
for(i<- 1 to 3; j <- 4 to 6){
print(s"[$i,$j]")
}
println()
}