英文:
How to union two 2D arrays
问题
Kotlin或Java标准库中是否有内置函数可以“合并”两个2D数组?通过合并,我指的是以下内容:
val a = arrayOf(
arrayOf(1, 0),
arrayOf(0, 0)
)
val b = arrayOf(
arrayOf(1, 0),
arrayOf(0, 1)
)
val result = a.union(b) { da, db -> da + db // 决定如何合并值 }
val shouldBe = arrayOf(
arrayOf(2, 0),
arrayOf(0, 1)
)
assert(result == shouldBe)
结果数组的形状应该与调用union的数组相同。
我可以自己编写这个功能,但想知道是否已经存在?
英文:
Is there a built in function in the Kotlin or Java standard library to "union" two 2D arrays? By union, I mean the following:
val a = arrayOf(
arrayOf(1, 0),
arrayOf(0, 0)
)
val b = arrayOf(
arrayOf(1, 0),
arrayOf(0, 1)
)
val result = a.union(b) { da, db -> da + db // Decide how to merge the values }
val shouldBe = arrayOf(
arrayOf(2, 0),
arrayOf(0, 1)
)
assert(result == shouldBe)
The shape of the resulting array should be that of the array which union is called on.
I can obviously write this myself, but wondering if it already exists?
答案1
得分: 2
答案是否定的,没有内置的方法。对于Kotlin,您可以查看JetBrains开发的这个库:viktor。我没有使用过它,只是在第一个版本发布时玩弄过(所以我不能真正说什么)。
如果您只有包含2D整数矩阵的简单情况,那么您可以尝试以下方法:
fun Array<Array<Int>>.matrixSum(b: Array<Array<Int>>): Array<Array<Int>> {
return mapIndexed { indexA, valueA ->
valueA.mapIndexed { indexB, valueB ->
valueB + b[indexA][indexB]
}.toTypedArray()
}.toTypedArray()
}
val a = arrayOf(arrayOf(1, 0), arrayOf(0, 0))
val b = arrayOf(arrayOf(1, 0), arrayOf(0, 1))
val result = a.matrixSum(b)
或者您可以将操作作为参数传递给matrixOp
函数:
fun Array<Array<Int>>.matrixOp(b: Array<Array<Int>>, op: (Int, Int) -> Int): Array<Array<Int>> {
return mapIndexed { indexA, valueA ->
valueA.mapIndexed { indexB, valueB ->
op(valueB, b[indexA][indexB])
}.toTypedArray()
}.toTypedArray()
}
val a = arrayOf(arrayOf(1, 0), arrayOf(0, 0))
val b = arrayOf(arrayOf(1, 0), arrayOf(0, 1))
val summed = a.matrixOp(b) { i1, i2 -> i1 + i2 }
val multiplied = a.matrixOp(b) { i1, i2 -> i1 * i2 }
英文:
The answer is no, there is nothing built-in. For Kotlin you could have a look at this library by JetBrains: viktor. I've not used it, just toyed around with it when the first version was published (so I can't really say anything about it).
If you have only simple cases with 2D-Int-matrices, then you could try something along this:
fun Array<Array<Int>>.matrixSum(b: Array<Array<Int>>): Array<Array<Int>> {
return mapIndexed { indexA, valueA ->
valueA.mapIndexed { indexB, valueB ->
valueB + b[indexA][indexB]
}.toTypedArray()
}.toTypedArray()
}
val a = arrayOf(arrayOf(1, 0), arrayOf(0, 0))
val b = arrayOf(arrayOf(1, 0), arrayOf(0, 1))
val result = a.matrixSum(b)
Or you hand over the operation to apply as argument:
fun Array<Array<Int>>.matrixOp(b: Array<Array<Int>>, op: (Int, Int) -> Int): Array<Array<Int>> {
return mapIndexed { indexA, valueA ->
valueA.mapIndexed { indexB, valueB ->
op(valueB, b[indexA][indexB])
}.toTypedArray()
}.toTypedArray()
}
val a = arrayOf(arrayOf(1, 0), arrayOf(0, 0))
val b = arrayOf(arrayOf(1, 0), arrayOf(0, 1))
val summed = a.matrixOp(b) { i1, i2 -> i1 + i2 }
val multiplied = a.matrixOp(b) { i1, i2 -> i1 * i2 }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论