example for some sink operators #25468
Examples for takeLast, last and lastOption operators
This commit is contained in:
parent
91b0c387be
commit
ba996f8c5f
6 changed files with 124 additions and 19 deletions
|
|
@ -25,6 +25,36 @@ class TakeLastSinkSpec extends StreamSpec {
|
|||
result should be(Seq(4, 5, 6))
|
||||
}
|
||||
|
||||
"return top three student based on GPA correctly" in {
|
||||
implicit val ex = system.dispatcher
|
||||
//#takeLast-operator-example
|
||||
case class Student(name: String, gpa: Double)
|
||||
|
||||
val students = List(Student("Alison", 4.7), Student("Adrian", 3.1), Student("Alexis", 4),
|
||||
Student("Benita", 2.1), Student("Kendra", 4.2), Student("Jerrie", 4.3)).sortBy(_.gpa)
|
||||
|
||||
val sourceOfStudents = Source(students)
|
||||
|
||||
val result: Future[Seq[Student]] = sourceOfStudents.runWith(Sink.takeLast(3))
|
||||
|
||||
result.foreach { topThree ⇒
|
||||
println("#### Top students ####")
|
||||
topThree.reverse foreach { s ⇒
|
||||
println(s"Name: ${s.name}, GPA: ${s.gpa}")
|
||||
}
|
||||
}
|
||||
/*
|
||||
#### Top students ####
|
||||
Name: Alison, GPA: 4.7
|
||||
Name: Jerrie, GPA: 4.3
|
||||
Name: Kendra, GPA: 4.2
|
||||
*/
|
||||
|
||||
//#takeLast-operator-example
|
||||
|
||||
result.futureValue shouldEqual students.takeRight(3)
|
||||
}
|
||||
|
||||
"return the number of elements taken when the stream completes" in {
|
||||
val input = 1 to 4
|
||||
val future: Future[immutable.Seq[Int]] = Source(input).runWith(Sink.takeLast(5))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue