基于:source code
我不明白为什么 Source.fromIterator
的参数是 Function0[Iterator[T]]
而不是 Iterator[T]
.
这有实际的原因吗?我们能不能把签名改成def fromIterator(iterator: => Iterator[T])
反而 ? (避免做 Source.fromIterator( () => myIterator)
)
最佳答案
根据 the docs :
The iterator will be created anew for each materialization, which is the reason the method takes a function rather than an iterator directly.
流阶段应该是可重用的,因此您可以将它们实现不止一个。但是,给定的迭代器(通常)只能使用一次。如
fromIterator
创建了一个引用现有迭代器(无论是通过名称还是引用传递)的 Source 再次尝试实现它可能会失败,因为底层迭代器将被耗尽。为了解决这个问题,源需要能够实例化一个新的迭代器,所以
fromIterator
允许您提供必要的逻辑来执行此操作作为供应商功能。这是我们不希望发生的事情的示例:
implicit val system = akka.actor.ActorSystem.create("test")
implicit val mat = akka.stream.ActorMaterializer(system)
val iter = Iterator.range(0, 2)
// pretend we pass the iterator directly...
val src = Source.fromIterator(() => iter)
Await.result(src.runForEach(println), 2.seconds)
// 0
// 1
// res0: akka.Done = Done
Await.result(src.runForEach(println), 2.seconds)
// res1: akka.Done = Done
// No results???
这很糟糕,因为来源
src
不可重复使用,因为它不会在后续运行中提供相同的输出。但是,如果我们懒惰地创建迭代器,它会起作用:val iterFunc = () => Iterator.range(0, 2)
val src = Source.fromIterator(iterFunc)
Await.result(src.runForEach(println), 2.seconds)
// 0
// 1
// res0: akka.Done = Done
Await.result(src.runForEach(println), 2.seconds)
// 0
// 1
// res1: akka.Done = Done
关于scala - 为什么 Source.fromIterator 需要 Function0[Iterator[T]] 作为参数而不是 Iterator[T]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40683078/