altavir
06/06/2019, 4:51 PMVirtualMatrix should not be used without good reason, because it calls a lambda, which could not be inlined on each call and is general a performance sink. Also if you implement operations with it, you can get nested lambdas, which are event worse.
The operations, you propose already exist. But they are not top-level, you should use MatrixContext.real.run{...} to access them inside the braces. It is done so by design so you could interpret a Matrix which is just a 2d-structure type-alias in different ways depending on the context. You can create a shortcut for context wrapper or run your functions with MatrixContext receiver to avoid duplication of that line.
In order to create new operations, you can just extend `MatrixContext`:
kotlin
fun <T: Any> MatrixContext<T>.appendBottom(other: Matrix<T>) = produce(rowNum + other.rowNum, colNum) { row, col ->
when {
row < this@appendBottom.rowNum -> this@appendBottom[row,col]
else -> other[row - this@appendBottom.rowNum, col]
}
}