迭代列表,返回当前元素、下一个元素以及当前元素之前的元素
我在以 scala 式且优雅的方式编写特定应用程序时遇到问题。我已经尝试了一段时间了,但我找不到解决此问题的“好的”解决方案:
鉴于我有以下列表:
List("foo", "bar", "baz", "blah")
我想迭代此列表,不仅为我提供每次迭代的当前元素,还为我提供当前元素之前和之后的元素。这可能是一个 Tuple3,但不是必需的。这可能是元组签名:
(Option[T], T, Option[T])
为了澄清我的意思,这是在 List[String]
上每次迭代的建议元组,在第四次之后结束。
迭代 1:(None, "foo", Some("bar"))
迭代 2:(Some("foo"),"bar",Some("baz"))< /code>
迭代 3: (Some("bar"), "baz", Some("blah"))
迭代 4: (Some("baz"), "blah",无)
我怎样才能达到这样的结果?再次强调:我不受 Tuple3 的约束,任何其他解决方案也非常感谢!
谢谢!
I have problems with writing an specific application in a scala-esque and elegant way. I tried this for some time now, but I cannot find a "good" solution to this Problem:
Given that I have the following List:
List("foo", "bar", "baz", "blah")
I want to Iterate over this list, not only giving me the current element for each Iteration but also the element before and after the current element. This might be a Tuple3 but is not required to. This could be the Tuple signature:
(Option[T], T, Option[T])
To clarify what i mean, this is the proposed Tuple for each iteration over a List[String]
, ending after the fourth.
Iteration 1: (None, "foo", Some("bar"))
Iteration 2: (Some("foo"), "bar", Some("baz"))
Iteration 3: (Some("bar"), "baz", Some("blah"))
Iteration 4: (Some("baz"), "blah", None)
How could I achieve such a result? Again: I am not bound to the Tuple3, any other solution is also very appreciated!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一种方法。它使用新的 Scala 2.8 集合方法
sliding
。更新:这是适用于 Streams 的版本。
Here's one approach. It uses a new Scala 2.8 collection method
sliding
.Update: Heres a version that works for Streams.
如果您使用的是 2.8,Retronym 的答案效果很好。如果您使用的是 2.7.x,则没有很好的库存解决方案,但您可以轻松构建自己的解决方案。例如,如果您只想存在之前和之后的三元组,则可以执行以下操作:
如果您更愿意允许之前和之后保留选项,(编辑:我并没有真正给出完整或无错误的一组之前更改)然后改为使用
Retronym's answer works well if you're using 2.8. If you're using 2.7.x, there isn't a great stock solution, but you can build your own easily. For example, if you want only triples where before and after exist, you can do something like this:
If you prefer to allow before and after to stay Options, (edit: I didn't really give a complete or error-free set of changes before) then instead use
更好地使用 Scala 2.8 和 retronym 解决方案,但这是我针对 Scala 2.7 的解决方案:
Better use Scala 2.8 and retronym's solution, of course, but here is my solution for Scala 2.7: