First-class support for operations on 2D-Arrays in Kotlin
I just noticed that flatMap
is a far more flexible and powerful solution, it makes this library completely redundant. in fact, don't even continue reading this file, head over to flatMap
's documentation instead.
examples from 2048-OOP Game:
- matrix(row: Int, col: Int, itemFactory: (i: Int, j: Int) -> T): Array<Array>
val emptyMatrix = matrix<Tile>(4, 4) { i, j ->
EmptyTile.apply { position = Position(i, j) }
}
- Array<Array<*>>#asSequence2D:
private val filledPositions: MutableList<Position>
get() = grid.asSequence2D()
.filter { it !== EmptyTile }
.map { it.position }
.toMutableList()
- Array<Array<*>>#withIndex2D:
private val emptySpaces: List<Position>
get() = grid.withIndex2D()
.asSequence()
.filter { it.value === EmptyTile }
.map { Position(it.x, it.y) }
.toList()
- Other supported operations/functions:
- forEach2D(action: (T) -> Unit)
- forEachIndexed2D(action: (i: Int, j: Int, T) -> Unit)
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency:
dependencies {
implementation 'com.github.mhashim6:Kotlin2DArrays:1.0.1'
}