scala中变长数组到元组的转换
我需要创建一个元组,但这个元组的大小需要在运行时根据变量值进行更改。在scala我真的做不到。所以我创建了一个数组:scala中变长数组到元组的转换,scala,Scala,我需要创建一个元组,但这个元组的大小需要在运行时根据变量值进行更改。在scala我真的做不到。所以我创建了一个数组: val temp:Array[String] = new Array[String](x) 如何将数组转换为元组。这可能吗?我是scala的新手 要创建元组,您必须知道预期的大小。假设你有,那么你可以这样做: val temp = Array("1", "2") val tup = temp match { case Array(a,b) => (a,b) } // tu
val temp:Array[String] = new Array[String](x)
如何将数组转换为元组。这可能吗?我是scala的新手 要创建元组,您必须知道预期的大小。假设你有,那么你可以这样做:
val temp = Array("1", "2")
val tup = temp match { case Array(a,b) => (a,b) }
// tup: (String, String) = (1,2)
def expectsTuple(x: (String,String)) = x._1 + x._2
expectsTuple(tup)
这允许您将元组传递给任何函数所期望的
如果您想变得更有趣,可以定义
.toTuple
方法:
implicit class Enriched_toTuple_Array[A](val seq: Array[A]) extends AnyVal {
def toTuple2 = seq match { case Array(a, b) => (a, b); case x => throw new AssertionError(s"Cannot convert array of length ${seq.size} into Tuple2: Array(${x.mkString(", ")})") }
def toTuple3 = seq match { case Array(a, b, c) => (a, b, c); case x => throw new AssertionError(s"Cannot convert array of length ${seq.size} into Tuple3: Array(${x.mkString(", ")})") }
def toTuple4 = seq match { case Array(a, b, c, d) => (a, b, c, d); case x => throw new AssertionError(s"Cannot convert array of length ${seq.size} into Tuple4: Array(${x.mkString(", ")})") }
def toTuple5 = seq match { case Array(a, b, c, d, e) => (a, b, c, d, e); case x => throw new AssertionError(s"Cannot convert array of length ${seq.size} into Tuple5: Array(${x.mkString(", ")})") }
}
这使您可以执行以下操作:
val tup = temp.toTuple2
// tup: (String, String) = (1,2)
为了创建元组,您必须知道预期的大小。假设你有,那么你可以这样做:
val temp = Array("1", "2")
val tup = temp match { case Array(a,b) => (a,b) }
// tup: (String, String) = (1,2)
def expectsTuple(x: (String,String)) = x._1 + x._2
expectsTuple(tup)
这允许您将元组传递给任何函数所期望的
如果您想变得更有趣,可以定义
.toTuple
方法:
implicit class Enriched_toTuple_Array[A](val seq: Array[A]) extends AnyVal {
def toTuple2 = seq match { case Array(a, b) => (a, b); case x => throw new AssertionError(s"Cannot convert array of length ${seq.size} into Tuple2: Array(${x.mkString(", ")})") }
def toTuple3 = seq match { case Array(a, b, c) => (a, b, c); case x => throw new AssertionError(s"Cannot convert array of length ${seq.size} into Tuple3: Array(${x.mkString(", ")})") }
def toTuple4 = seq match { case Array(a, b, c, d) => (a, b, c, d); case x => throw new AssertionError(s"Cannot convert array of length ${seq.size} into Tuple4: Array(${x.mkString(", ")})") }
def toTuple5 = seq match { case Array(a, b, c, d, e) => (a, b, c, d, e); case x => throw new AssertionError(s"Cannot convert array of length ${seq.size} into Tuple5: Array(${x.mkString(", ")})") }
}
这使您可以执行以下操作:
val tup = temp.toTuple2
// tup: (String, String) = (1,2)
遇到了类似的问题。 我希望这有帮助
(0 :: 10 :: 50 :: Nil).sliding(2, 1).map( l => (l(0), l(1)))
结果:
List[(Int, Int)] = List((0,10), (10,50))
遇到了类似的问题。 我希望这有帮助
(0 :: 10 :: 50 :: Nil).sliding(2, 1).map( l => (l(0), l(1)))
结果:
List[(Int, Int)] = List((0,10), (10,50))
大小是运行时唯一会变化的东西吗?元组中项目的类型如何?它是否总是像你的例子所建议的那样是
String
?根据定义,元组是一种异构元素固定的算术事物。如果需要动态数量的字符串,可以使用任何类型的集合。为什么要将其转换为“元组”?我需要一个元组,因为我需要将一个元组提供给另一个类的方法,而我无法控制该方法。是的,元组的类型是StringSize,运行时唯一会变化的是大小吗?元组中项目的类型如何?它是否总是像你的例子所建议的那样是String
?根据定义,元组是一种异构元素固定的算术事物。如果需要动态数量的字符串,可以使用任何类型的集合。为什么要将其转换为“元组”?我需要一个元组,因为我需要将一个元组提供给另一个类的方法,而我无法控制该方法。是的,元组是字符串类型