关于scala元组的简单问题
我是 Scala 新手,我正在学习的是 tuple
。
我可以如下定义一个元组,并获取项目:
val tuple = ("Mike", 40, "New York")
println("Name: " + tuple._1)
println("Age: " + tuple._2)
println("City: " + tuple._3)
我的问题是:
- 如何获取元组的长度?
- 元组是可变的吗?我可以修改它的项目吗?
- 我们还可以对元组执行其他有用的操作吗?
提前致谢!
I'm new to scala, and what I'm learning is tuple
.
I can define a tuple as following, and get the items:
val tuple = ("Mike", 40, "New York")
println("Name: " + tuple._1)
println("Age: " + tuple._2)
println("City: " + tuple._3)
My question is:
- How to get the length of a tuple?
- Is tuple mutable? Can I modify its items?
- Is there any other useful operation we can do on a tuple?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
1]
tuple.productArity
2] No.
3] 您可以对元组执行一些有趣的操作:(简短的 REPL 会话)
请参阅 Tuple2,Tuple3 等了解更多。
1]
tuple.productArity
2] No.
3] Some interesting operations you can perform on tuples: (a short REPL session)
See scaladocs of Tuple2, Tuple3 etc for more.
您还可以使用元组做的一件事是使用
match
表达式提取内容:给出:
One thing that you can also do with a tuple is to extract the content using the
match
expression:Gives:
元组是不可变的,但是,像所有案例类一样,它们有一个复制方法,可用于创建带有一些更改元素的新元组:
Tuples
are immutable, but, like all cases classes, they have a copy method that can be used to create a newTuple
with a few changed elements:关于问题 3:
您可以使用元组做的一件有用的事情是存储函数的参数列表:
[编辑]正如兰德尔所说,您最好在“现实生活”中使用类似的东西:
为了从元组中提取值在“集合转换链”的中间,您可以这样写:
注意大小写周围的大括号,圆括号不起作用。
Concerning question 3:
A useful thing you can do with Tuples is to store parameter lists for functions:
[Edit] As Randall remarks, you'd better use something like this in "real life":
In order to extract the values from tuples in the middle of a "collection transformation chain" you can write:
Note the curly braces around the case, parentheses won't work.
另一个不错的技巧广告问题3)(因为1和2已经被其他人回答了)
编辑:事实上,它是模式匹配和案例类的一个功能,元组只是案例类的一个简单示例......
Another nice trick ad question 3) (as 1 and 2 are already answered by others)
Edit: in fact it's rather a feature of pattern matching and case classes, a tuple is just a simple example of a case class...
def f(tup: (Int, Int))
,则您知道tup
的长度为 2,因为(Int) 类型的值, Int)
(又名Tuple2[Int, Int]
)的长度始终为 2。def f(tup: (Int, Int))
, you know the length oftup
is 2 because values of type(Int, Int)
(akaTuple2[Int, Int]
) always have a length of 2.1和2已经回答了。
使用元组的一件非常有用的事情是从方法或函数返回多个值。简单的例子:
1 and 2 have already been answered.
A very useful thing that you can use tuples for is to return more than one value from a method or function. Simple example:
使用 shapeless,您可以轻松获得许多有用的方法,这些方法通常仅在集合上可用:
一切都是类型化的正如人们所期望的那样(即
first
的类型为String
,concat
的类型为(String, Int, Boolean, Double, Double, Long)
等)上面的最后一个方法 (
.to[Collection]
) 应该在下一个版本中可用(截至 2014/07/19)。您还可以“更新”一个元组
,但这将返回一个新元组,而不是改变原始元组。
Using shapeless, you easily get a lot of useful methods, that are usually available only on collections:
Everything is typed as one would expect (that is
first
has typeString
,concat
has type(String, Int, Boolean, Double, Double, Long)
, etc.)The last method above (
.to[Collection]
) should be available in the next release (as of 2014/07/19).You can also "update" a tuple
but that will return a new tuple instead of mutating the original one.