在 Akka Streams Doku 中,给出了一个包含 null 的列表的 Scala 示例。此列表转换为源,并像示例中一样减少。不幸的是,在 Java/Kotlin 中这不会输出任何内容。
使用推文链接到 Scala 示例:https://doc.akka.io/docs/akka/current/stream/stream-quickstart.html#first-steps
这是我的 Kotlin 翻译
val system = ActorSystem.create("reactive-tweets")
val ints: Source<Int, NotUsed> = Source.from(listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, null))
ints
.filter {
it != null
}
.map {
it * 2
}
.reduce { arg1, arg2 ->
arg1 + arg2
}
.runWith(Sink.foreach { a -> println("sum: " + a)}, system)
我也尝试了 Source<Int?, NotUsed>
并没有改变任何东西。
我很想知道后端是否发生错误或发生了什么,流没有到达打印语句。
最佳答案
Akka Streams 不允许 null
元素。这记录在 https://doc.akka.io/docs/akka/current/stream/stream-flows-and-basics.html#illegal-stream-elements
您在该示例中看到的不是 null
作为流元素,而是 Nil
用于使用 ::
构建列表运算符(operator)。在 Scala 中,Nil
是一个包含空列表的常量,::
将元素添加到列表中。本例中使用这些来构造一个链表,然后将其转换为 Source
。 .
关于Java/Kotlin- Akka Stream Source.reduce 在 Source 中为 null 时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62198179/