scala 在Scala中将两个元组组合成一个新的更大元组的干净方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9028459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
a clean way to combine two tuples into a new larger tuple in scala?
提问by jxstanford
Let's say I have the following tuples:
假设我有以下元组:
scala> val t1 = Tuple2("abcd", "efg")
t1: (java.lang.String, java.lang.String) = (abcd,efg)
scala> val t2 = Tuple2(1234, "lmnop")
t2: (Int, java.lang.String) = (1234,lmnop)
scala> val t3 = Tuple3("qrs", "tuv", "wxyz")
t3: (java.lang.String, java.lang.String, java.lang.String) = (qrs,tuv,wxyz)
Is there a friendly way to combine them (in two steps if necessary) into a Tuple7? I'm really looking for a general answer for combining tuples of arbitrary size, and realize that there will be limitations due to the capped maximum tuple size. I am specifically looking for a tuple result, not a collection.
是否有一种友好的方式将它们(如有必要,分两步)组合成 Tuple7?我真的在寻找组合任意大小的元组的一般答案,并意识到由于有上限的最大元组大小而存在限制。我专门寻找元组结果,而不是集合。
采纳答案by huynhjl
Shapelessrequires dependent method types (-Ydependent-method-types) and I wish there was a downloadable binary for 2.9.1 so that I can simply try it out but it's really seems elegant. Based on this unit testit would apply to your case like this:
Shapeless需要依赖方法类型 ( -Ydependent-method-types),我希望 2.9.1 有一个可下载的二进制文件,这样我就可以简单地尝试一下,但它确实看起来很优雅。基于此单元测试,它将适用于您的情况,如下所示:
import shapeless.Tuples._
import shapeless.HList._
val t7 = (t1.hlisted ::: t2.hlisted ::: t3.hlisted).tupled
Although Miles indicates there is not guarantee of support, it actually has unit tests and the source is on github with an open source license so at least it's not just an experiment in a blog post.
尽管 Miles 表示不能保证支持,但它实际上有单元测试,并且源代码在 github 上并具有开源许可证,因此至少它不仅仅是博客文章中的实验。
Edit: works as advertized - took some time to compile and I had to add -Xss1mto sbt:
编辑:按广告方式工作 - 编译花了一些时间,我不得不添加-Xss1m到 sbt:
$ scala -Ydependent-method-types -cp target/scala-2.9.1/shapeless_2.9.1-1.1.0.jar
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) Client VM, Java 1.7.0).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import shapeless.Tuples._
import shapeless.Tuples._
scala> import shapeless.HList._
import shapeless.HList._
scala> val t1 = Tuple2("abcd", "efg")
t1: (java.lang.String, java.lang.String) = (abcd,efg)
scala> val t2 = Tuple2(1234, "lmnop")
t2: (Int, java.lang.String) = (1234,lmnop)
scala> val t3 = Tuple3("qrs", "tuv", "wxyz")
t3: (java.lang.String, java.lang.String, java.lang.String) = (qrs,tuv,wxyz)
scala> (t1.hlisted ::: t2.hlisted ::: t3.hlisted).tupled
res0: (java.lang.String, java.lang.String, Int, java.lang.String, java.lang.String,
java.lang.String, java.lang.String) = (abcd,efg,1234,lmnop,qrs,tuv,wxyz)
回答by Luigi Plinge
You really need to be using collections here, especially if all elements are the same type. You can combine tuples into a Listwithout much difficulty:
你真的需要在这里使用集合,特别是如果所有元素都是相同的类型。您可以List毫不费力地将元组组合成一个:
def combine(xss: Product*) = xss.toList.flatten(_.productIterator)
Using your example:
使用您的示例:
scala> combine(t1, t2, t3)
res1: List[Any] = List(abcd, efg, hijk, lmnop, qrs, tuv, wxyz)
Trying to turn this back into tuples isn't going to work because your conversion method (e.g. with pattern matching) won't be able to return the specific tuple type (what's the return type of the method?), and the type information of each element has been lost.
试图将其转回元组是行不通的,因为您的转换方法(例如使用模式匹配)将无法返回特定的元组类型(该方法的返回类型是什么?),以及每个元素都丢失了。

