[英]scala: from seq to tuple
It is very easy to convert a tuple into a List/Seq by doing: 通过执行以下操作将元组转换为List / Seq非常容易:
myTuple.productIterator.toSeq
But what about the reverse operation (just out of curiosity). 但是如何进行反向操作(出于好奇)。 The code below works, but is quite ugly... 下面的代码有效,但是非常丑陋。
def arrayToTuple(a: Seq[Any]) = a.size match {
case 1 => (a(0))
case 2 => (a(0), a(1))
case 3 => (a(0), a(1), a(2))
case 4 => (a(0), a(1), a(2), a(3))
case 5 => (a(0), a(1), a(2), a(3), a(4))
case 6 => (a(0), a(1), a(2), a(3), a(4), a(5))
case 7 => (a(0), a(1), a(2), a(3), a(4), a(5), a(6))
case 8 => (a(0), a(1), a(2), a(3), a(4), a(5), a(6), a(7))
case 9 => (a(0), a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8))
case 10 => (a(0), a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9))
case 11 => (a(0), a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9), a(10))
case 12 => (a(0), a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9), a(10), a(11))
case 13 => (a(0), a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9), a(10), a(11), a(12))
case 14 => (a(0), a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9), a(10), a(11), a(12), a(13))
case 15 => (a(0), a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9), a(10), a(11), a(12), a(13), a(14))
case 16 => (a(0), a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9), a(10), a(11), a(12), a(13), a(14), a(15))
case 17 => (a(0), a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9), a(10), a(11), a(12), a(13), a(14), a(15), a(16))
case 18 => (a(0), a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9), a(10), a(11), a(12), a(13), a(14), a(15), a(16), a(17))
case 19 => (a(0), a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9), a(10), a(11), a(12), a(13), a(14), a(15), a(16), a(17), a(18))
case 20 => (a(0), a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9), a(10), a(11), a(12), a(13), a(14), a(15), a(16), a(17), a(18), a(19))
case 21 => (a(0), a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9), a(10), a(11), a(12), a(13), a(14), a(15), a(16), a(17), a(18), a(19), a(20))
case 22 => (a(0), a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9), a(10), a(11), a(12), a(13), a(14), a(15), a(16), a(17), a(18), a(19), a(20), a(21))
case _ => (0)
}
This is not possible in Scala 2.X because each length of tuple is a different type and each element of a tuple has a different type. 在Scala 2.X中这是不可能的,因为元组的每个长度都是不同的类型,并且元组的每个元素都具有不同的类型。 So it would be difficult to write a function to append a value of any type to a tuple of any type. 因此,很难编写一个将任何类型的值附加到任何类型的元组的函数。
But to satisfy your curiosity, this is a pending feature of Scala 3: 但是,为了满足您的好奇心,这是Scala 3的一项待定功能:
Tuples with arbitrary numbers of elements are treated as sequences of nested pairs. 具有任意数量元素的元组被视为嵌套对的序列。 Eg
(a, b, c)
is shorthand for(a, (b, (c, ())))
. 例如(a, b, c)
是(a, (b, (c, ())))
(a, b, c)
的简写。 This lets us drop the current limit of 22 for maximal tuple length and it allows generic programs over tuples analogous to what is currently done forHList
. 这使我们可以将最大元HList
的当前限制降低到22,并允许在元组上进行泛型程序,类似于HList
当前操作。
See http://dotty.epfl.ch/docs/reference/overview.html 参见http://dotty.epfl.ch/docs/reference/overview.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.