Mor*_*ive 1 scala tuples pattern-matching
我正在尝试编写一个函数来比较相似类型的元组.
def compareTuples(tuple1: (String, String, Int), tuple2: (String, String, Int)): (String, String, Int) = {
// if tuple1.Int < tuple2.Int return tuple1 else tuple2.
}
Run Code Online (Sandbox Code Playgroud)
如何访问每个元组中的第三个元素或int?
谢谢
要在一个元组访问的值t,你可以使用t._1,t._2等等.
对你而言会导致
def compareTuples(tuple1: (String, String, Int), tuple2: (String, String, Int)): (String, String, Int) = {
if (tuple1._3 < tuple2._3) tuple1 else tuple2
}
Run Code Online (Sandbox Code Playgroud)