将嵌套的Product / Tuple分配给Array [Numeric]

时间:2011-05-19 05:00:11

标签: scala

我正在寻找一种快速的方法将产品分配给类型为Numeric的数组。例如,我想用填充数组元素:

// not restricted to be 4-by-2, can be abritary
// Double will be later replaced by type T : Numeric
var elements = new Array[Double](4*2) 
// can contain other Numemrics as Int
var values = ((11.0,12.0),(21.0,22.0),(31.0,32.0), (4.0,1.0)) 

到目前为止,我的方法是

var i = 0;
var itRow = values.productIterator.asInstanceOf[Iterator[Product]]
while(itRow.hasNext){
    var itCol = itRow.next.productIterator.asInstanceOf[Iterator[Double]]
        while(itCol.hasNext){
            elements(i) = itCol.next.asInstanceOf[Double]
            i = i + 1
        }
}

如果值中的所有条目都是Double,则它可以工作,但是对于abritary Numerics来说这也很好。 第二,是否有更优雅,更快捷的方式来做到这一点?也许最好通过

来压扁元组
def flatProduct(t: Product): Iterator[Any] = t.productIterator.flatMap {
    case p: Product => flatProduct(p)
    case x => Iterator(x)
}  

// edit: I think this is better, 
// but still the problematic if there are Int-types in values
flatProduct(values).asInstanceOf[Iterator[Dobule]].copyToArray(elements)
你怎么想?

非常感谢!

1 个答案:

答案 0 :(得分:1)

选择:

scala> values.productIterator.map{case (x: Double, y: Double) => Array(x, y)}.flatten.toList
res17: List[Double] = List(11.0, 12.0, 21.0, 22.0, 31.0, 32.0, 4.0, 1.0)

scala> values.productIterator.asInstanceOf[Iterator[(Double,Double)]].foldLeft(List[Double]()){(l, i) => i._2 :: i._1 :: l}.reverse
res18: List[Double] = List(11.0, 12.0, 21.0, 22.0, 31.0, 32.0, 4.0, 1.0)

scala> values.productIterator.foreach{var i = 0; {case (x: Double, y: Double) => elements(i) = x; elements(i + 1) = y; i += 2}}

scala> elements
res19: Array[Double] = Array(11.0, 12.0, 21.0, 22.0, 31.0, 32.0, 4.0, 1.0)