我有一段类似于以下示例的代码:
def main(args: Array[String]): Unit = {
val mutableList = ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val foo = Source
.repeat(mutableList)
.flatMapConcat(myList => Source.fromFuture(Future.successful {
val foo = new Random().nextInt(10)
if (myList.contains(foo)) {
Some(foo)
} else {
None
}
}))
.takeWhile(_.isDefined)
.take(5)
.collect {
case Some(value) => value
}
.alsoTo(Sink.foreach(value => mutableList.remove(mutableList.indexOf(value))))
.runFold(List.empty[Int])((list, value) => value +: list)
println(Await.result(foo, Duration.Inf))
}
基本上,作为流程的一个步骤,我需要更改源发出的内容。
如何使用akka-stream使用可变数据结构(ArryBuffer)来实现相同的目标?