Giovanni, hi!
With some piece of stubbornness :) I (guess) have found a way with
Source.last():
def sinkFoldM[U, T](zero: U)(f: (U, T) ⇒ Future[U]): Sink[T, Future[U]] =
Sink.fromGraph {
val last = Sink.last[U]
GraphDSL.create(last) { implicit b => last =>
import GraphDSL.Implicits._
val zip = b.add(ZipWith(f))
val bcast = b.add(Broadcast[U](2))
val merge = b.add(Merge[U](2))
Source.single(zero) ~> merge ~> bcast ~> last
bcast ~> zip.in0
zip.out.mapAsync(1)(identity) ~> merge
SinkShape(zip.in1)
}
}
And in hands I have got a task with needing to fold starting with future
zero. So, I have tried:
def sinkFoldMM[U, T](zeroFu: Future[U])(f: (U, T) ⇒ Future[U]): Sink[T,
Future[U]] = Sink.fromGraph {
val last = Sink.last[U]
GraphDSL.create(last) { implicit b => last =>
import GraphDSL.Implicits._
val zip = b.add(ZipWith(f))
val bcast = b.add(Broadcast[U](2))
val merge = b.add(Merge[U](2))
Source.fromFuture(zeroFu) ~> merge ~> bcast ~> last
bcast ~> zip.in0
zip.out.mapAsync(1)(identity) ~> merge
SinkShape(zip.in1)
}
}
By some reason this test
val upTo = 20000000;
//val upTo = 0;
val result = Source(0 until upTo).runWith(sinkFoldMM(Future(0))((a, x) =>
Future(a + x)))
println(Await.result(result, 10.minutes))
sys.exit(0)
takes two times more CPU time in comparison with sinkFoldM (~50 second vs.
~25 second).
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.