example for some sink operators #25468

Examples for takeLast, last and lastOption operators
This commit is contained in:
mohammad mehdi 2018-10-25 17:10:36 +03:30 committed by Johan Andrén
parent 91b0c387be
commit ba996f8c5f
6 changed files with 124 additions and 19 deletions

View file

@ -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))