如何在 Scala 中编写聚合模式?
假设我有 Iterator[A]
(大小无限),并且我想从中获取 Iterator[B]
,其中一些后续类型值A 已聚合。
例子: 我有字符串列表:
Iterator(
"START",
"DATA1",
"DATA2",
"DATA3",
"START",
"DATA1",
"DATA2",
//.. 10^10 more records
)
我想连接从 START 到 NEXT START 的字符串(排除)。即编写解析器。
Iterator(
"START DATA1 DATA2 DATA3",
"START DATA1 DATA2",
//.. 10^10 / 5 more records
)
我知道如何强制执行此操作,但我想使用 scala 高阶函数来完成它。有什么想法吗?
PS EIP 聚合http://camel.apache.org/aggregator2.html。
Suppose I have Iterator[A]
(size is infinite) and I want to get Iterator[B]
from it where some subsequent values of type A are aggregated.
Example:
I have list of strings:
Iterator(
"START",
"DATA1",
"DATA2",
"DATA3",
"START",
"DATA1",
"DATA2",
//.. 10^10 more records
)
I want to join strings from START to NEXT START excluding. I.e. write parser.
Iterator(
"START DATA1 DATA2 DATA3",
"START DATA1 DATA2",
//.. 10^10 / 5 more records
)
I know how to do this imperatively, but I want to accomplish it with scala higher order functions. Any ideas?
PS EIP Aggregate http://camel.apache.org/aggregator2.html.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想要一个函数式解决方案,您应该使用 Streams 而不是迭代器(流是不可变的)。这是一种可能的方法:
它按预期工作:
If you want a functional solution, you should use Streams rather than iterators (streams are immutable). Here's one possible approach:
It works as expected:
嗯,无限的流会极大地改变事情。假设我了解您的其他情况,这应该有效:
这样您就可以:
Well, an infinite stream changes things rather dramatically. Assuming I understand the rest of your situation, this should work:
So that you can:
你可以尝试一下折叠:
You could try it with a fold:
使用流:
With Streams: