This repository was archived by the owner on May 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLista11.scala
More file actions
106 lines (88 loc) · 3.33 KB
/
Lista11.scala
File metadata and controls
106 lines (88 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Arkadiusz Ziobrowski - 229728
import scala.concurrent._
import scala.concurrent.duration._
import ExecutionContext.Implicits.global
import scala.io.Source
import scala.util.{Failure, Success}
object Zad1a extends App {
def pairFut[A, B] (fut1: Future[A], fut2: Future[B]): Future[(A, B)] =
fut1 zip fut2
val f1 = Future { Thread.sleep(2000); 1 }
val f2 = Future { Thread.sleep(1000); "a" }
val result = Await.result(pairFut(f1, f2), 3.seconds)
println(s"Result: $result")
}
object Zad1b extends App {
def pairFut[A, B] (fut1: Future[A], fut2: Future[B]): Future[(A, B)] =
for {
x <- fut1
y <- fut2
} yield (x, y)
val f1 = Future { Thread.sleep(2000); 1 }
val f2 = Future { Thread.sleep(1000); "a" }
val result = Await.result(pairFut(f1, f2), 3.seconds)
println(s"Result: $result")
}
object Zad2a extends App {
implicit class FutureOps[T](val self: Future[T]) {
def exists(p: T => Boolean): Future[Boolean] = {
val prom = Promise[Boolean]
self map p onComplete {
case Success(true) => prom trySuccess true
case _ => prom trySuccess false
}
prom.future
}
}
val f1 = Future { Thread.sleep(1000); 1 } exists(x => true)
val r1 = Await.result(f1, 3.seconds)
println(s"Result: $r1")
val f2 = Future { Thread.sleep(1000); 1 / 0 } exists(_ => true)
val r2 = Await.result(f2, 3.seconds)
println(s"Result: $r2")
val f3 = Future { Thread.sleep(1000); 1 } exists(_ => false)
val r3 = Await.result(f3, 3.seconds)
println(s"Result: $r3")
}
object Zad2b extends App {
implicit class FutureOps[T](val self: Future[T]) {
def exists(p: T => Boolean): Future[Boolean] =
self map p recover {
case _ => false
}
}
val f1 = Future { Thread.sleep(1000); 1 } exists(x => x > 0)
val r1 = Await.result(f1, 3.seconds)
println(s"Result: $r1")
val f2 = Future { Thread.sleep(1000); 1 / 0 } exists(x => x > 0)
val r2 = Await.result(f2, 3.seconds)
println(s"Result: $r2")
val f3 = Future { Thread.sleep(1000); -1 } exists(x => x > 0)
val r3 = Await.result(f3, 3.seconds)
println(s"Result: $r3")
}
object WordCount {
def main(args: Array[String]) {
val path = "/home/arek/Desktop/scala-test/"
val promiseOfFinalResult = Promise[Seq[(String, Int)]]
// Tu oblicz promiseOfFinalResult
promiseOfFinalResult tryCompleteWith (scanFiles(path) flatMap (fileNames => processFiles(fileNames)))
promiseOfFinalResult.future onComplete {
case Success(result) => result.sortBy(_._2) foreach println
case Failure(t) => t.printStackTrace()
}
Thread.sleep(5000)
}
// Oblicza liczbę słów w każdym pliku z sekwencji wejściowej
private def processFiles(fileNames: Seq[String]): Future[Seq[(String, Int)]] =
Future.sequence(for(filename <- fileNames) yield processFile(filename))
// Wskazówka. Wykorzystaj Future.sequence(futures)
// Oblicza liczbę słów w podanym pliku i zwraca parę: (nazwa pliku, liczba słów)
private def processFile(fileName: String): Future[(String, Int)] = Future {
val f = Source.fromFile(fileName)
try { (fileName, f.getLines.mkString("\n").split("\\s+").length) } finally f.close()
}
// Zwraca sekwencję nazw plików (w naszym przypadku Array[String])
private def scanFiles(docRoot: String): Future[Seq[String]] =
Future { new java.io.File(docRoot).list.map(docRoot + _) }
}