Java/Kotlin- Akka Stream Source.reduce 如果 Source 中为空则不起作用
Java/Kotlin- Akka Stream Source.reduce does not work if null in Source
在 Akka Streams Doku 中,给出了一个包含 null 列表的 Scala 示例。此列表转换为源,并像示例中一样减少。不幸的是,在 Java/Kotlin 这不会输出任何东西。
Link 到带有推文的 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
用于使用 ::
运算符构建列表。在 Scala 中,Nil
是一个包含空列表的常量,而 ::
将一个元素添加到列表中。本例中使用这些来构造一个链表,然后将其转换为 Source
.
在 Akka Streams Doku 中,给出了一个包含 null 列表的 Scala 示例。此列表转换为源,并像示例中一样减少。不幸的是,在 Java/Kotlin 这不会输出任何东西。
Link 到带有推文的 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
用于使用 ::
运算符构建列表。在 Scala 中,Nil
是一个包含空列表的常量,而 ::
将一个元素添加到列表中。本例中使用这些来构造一个链表,然后将其转换为 Source
.