为分页 API 创建 Scala 迭代器
我正在编写一个 Scala 库,以便更轻松地查询分页 JSON API。每个 API 调用都会返回一个如下所示的对象:
{
"count": 100,
"current_page": 1,
"total_pages": 2,
"records": [
...
]
}
我想要一个返回某种迭代器(例如 MyIterator[Record])的函数。在 Scala 世界中是否有任何标准方法可以做到这一点,甚至标准库中的构造可以帮助我?
我通常使用 lift-json 进行 JSON 解析,如果这有帮助的话。
谢谢。
I'm writing a Scala library to make querying an paginated JSON API easier. Each API call returns an object that looks something like this:
{
"count": 100,
"current_page": 1,
"total_pages": 2,
"records": [
...
]
}
I'd like to have a function that returned some sort of iterator like MyIterator[Record]. Are there any standard ways to do this in the Scala world, perhaps even constructs in the standard library that could help me?
I'm using lift-json generally for my JSON parsing, if that's helpful.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有一个
Iterator[Iterator[A]]
,您可以使用flatten
生成一个Iterator[A]
,它将所有嵌套的迭代器链接在一起:与迭代器上的工厂方法之一结合 伴随对象< /a>,您可能可以将其变成单行:
如果您只能通过检索页面来获取页数...则可能需要更复杂一点...类似:
If you have an
Iterator[Iterator[A]]
you can useflatten
to produce anIterator[A]
which chains all the nested Iterators together:Combined with one of the factory methods on the Iterator companion object, you could probably turn this into a one-liner:
It might need to be a bit more complex if you can only get the page count by retrieving a page... something like: