操作元组
有没有办法在不使用临时变量和启动新语句的情况下操作元组的多个值?
假设我有一个返回元组的方法,并且我想对这些内联值执行某些操作。
部分
def backToFront(s: String, n:Int) = s.splitAt(n)...
例如,如果我想在某个点分割一个字符串并反转我可以做的
val (a, b) = s.splitAt(n)
b + a
(需要临时变量和新语句)或
List(s.splitAt(n)).map(i => i._2 + i._1).head
(有效,但似乎有点脏,为此创建一个单个元素列表)或
s.splitAt(n).swap.productIterator.mkString
(适用于这个特定的示例,但只是因为恰好有一个 swap
方法可以完成我想要的操作,所以它不是很通用)。
元组上的 zipped
方法似乎只适用于列表元组。
另一个例子,如何在一个语句中将元组 ('a, 'b, 'c)
转换为 ('b, 'a, 'c)
?
Is there a way to manipulate multiple values of a tuple without using a temporary variable and starting a new statement?
Say I have a method that returns a tuple and I want to do something with those values inline.
e.g. if I want to split a string at a certain point and reverse the pieces
def backToFront(s: String, n:Int) = s.splitAt(n)...
I can do
val (a, b) = s.splitAt(n)
b + a
(requires temporary variables and new statement) or
List(s.splitAt(n)).map(i => i._2 + i._1).head
(works, but seems a bit dirty, creating a single element List just for this) or
s.splitAt(n).swap.productIterator.mkString
(works for this particular example, but only because there happens to be a swap
method that does what I want, so it's not very general).
The zipped
method on tuples seems just to be for tuples of Lists.
As another example, how could you turn the tuple ('a, 'b, 'c)
into ('b, 'a, 'c)
in one statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
元组只是方便的返回类型,并不假设您将使用它进行复杂的操作。 Scala 论坛上也有类似的讨论。
关于最后一个例子,找不到比模式匹配更好的东西了。
Tuples are just convenient return type, and it is not assumed that you will make complicated manipulations with it. Also there was similar discussion on scala forums.
About the last example, couldn't find anything better than pattern-matching.
不幸的是,元组上的内置方法相当有限。
也许您想要在您的个人库中添加类似的内容,
通过适当的隐式转换,您可以这样做,
最后一个表达式的替代方案是“管道”运算符
|>
(从 Scalaz 或这里),这很好,如果没有必要的注释,
Unfortunately, the built-in methods on tuples are pretty limited.
Maybe you want something like these in your personal library,
With the appropriate implicit conversions, you could do,
An alternative to the last expression would be the "pipe" operator
|>
(get it from Scalaz or here),This would be nice, if not for the required annotations,
这个怎么样?
[编辑:刚刚注意到你的函数参数被颠倒了。在这种情况下,您将不得不放弃占位符语法,转而使用:
s.splitAt(n) |> Function.tupled((a, b) => b + a)
]对于您的最后一个示例,想不出比模式匹配更好的东西(如@4e6所示。)
How about this?
[ Edit: Just noticed your arguments to function are reversed. In that case, you will have to give up placeholder syntax and instead go for:
s.splitAt(n) |> Function.tupled((a, b) => b + a)
]For your last example, can't think of anything better than a pattern match (as shown by @4e6.)
使用 shapeless 的当前开发版本,您可以在不解压元组的情况下实现此目的:
我认为您不应该在最后一行方法的语法被清理之前必须等待太长时间(我想说几天),你可以简单地写
With the current development version of shapeless, you can achieve this without unpacking the tuple:
I think you shouldn't have to wait too long (matter of days I'd say) before the syntax of the methods of the last line get cleaned, and you can simply write