【发布时间】:2016-03-15 23:28:06
【问题描述】:
我不能设计一个函数来做任何数字类型:
def array_add_Int(x: WrappedArray[Int], y: WrappedArray[Int]): WrappedArray[Int] = {
require(x.length == y.length, "ERROR: cannot operate on arrays of different ranges of dimensions.")
x.zipAll(y,0,0).map(pair=>pair._1+pair._2)
}
def array_add_Long(x: WrappedArray[Long], y: WrappedArray[Long]): WrappedArray[Long] = {
require(x.length == y.length, "ERROR: cannot operate on arrays of different ranges of dimensions.")
x.zipAll(y,0,0).map(pair => pair._1.asInstanceOf[Number].longValue() + pair._2.asInstanceOf[Number].longValue())
}
我尝试附加两个函数的返回如下:
def array_add[T](itemX:Traversable[T], itemY:Traversable[T])(implicit n:Numeric[T]) = {
require(itemX.size == itemY.size, "ERROR: cannot operate on arrays of different ranges of dimensions.")
itemX.toSeq.zipAll(itemY.toSeq, 0, 0).map(pair => pair._1 + pair._2)
}
它只适用于 Int 类型。有什么想法吗?
【问题讨论】:
-
附带说明,这与 spark 或 udfs 没有任何关系。这只是一个普通的 Scala 问题。
-
@AngeloGenovese 我认为它确实如此。这只是从here 开始的一系列问题的一部分。
-
可能,我对这个世界不是很熟悉。我想创建此功能以在 SQL Spark 中注册,但它不起作用。
-
@zero323 可能是他打算在 Spark 中使用这个函数来定义一个 UDF,但是当他问这个问题时,他似乎遇到的直接问题是 base Scala。
标签: scala apache-spark user-defined-functions apache-spark-sql