updates in matrix

This commit is contained in:
InsanusMokrassar 2021-02-11 15:05:11 +06:00
parent a3cabd7a9e
commit d41c3c2de4
4 changed files with 55 additions and 16 deletions

View File

@ -5,6 +5,9 @@ class MatrixBuilder<T> {
val matrix: Matrix<T>
get() = mutMatrix
fun add(t: List<T>) = mutMatrix.add(t)
operator fun plus(t: List<T>) = add(t)
fun row(t: List<T>) = mutMatrix.add(t)
operator fun List<T>.unaryPlus() = row(this)
}
fun <T> MatrixBuilder<T>.row(block: RowBuilder<T>.() -> Unit) = +RowBuilder<T>().also(block).row
fun <T> MatrixBuilder<T>.row(vararg elements: T) = +elements.toList()

View File

@ -15,17 +15,3 @@ fun <T> flatMatrix(vararg elements: T): Matrix<T> {
row { elements.forEach { +it } }
}.matrix
}
fun <T> row(block: RowBuilder<T>.() -> Unit): List<T> {
return RowBuilder<T>().also(block).row
}
fun <T> MatrixBuilder<T>.row(block: RowBuilder<T>.() -> Unit) {
add(RowBuilder<T>().also(block).row)
}
fun <T> MatrixBuilder<T>.row(vararg elements: T) {
add(elements.toList())
}
operator fun <T> RowBuilder<T>.plus(t: T) = add(t)

View File

@ -8,3 +8,8 @@ class RowBuilder<T> {
fun add(t: T) = mutRow.add(t)
operator fun T.unaryPlus() = add(this)
}
fun <T> row(block: RowBuilder<T>.() -> Unit): List<T> = RowBuilder<T>().also(block).row
fun <T> RowBuilder<T>.column(element: T) = +element
fun <T> RowBuilder<T>.columns(elements: List<T>) = elements.forEach(::column)
fun <T> RowBuilder<T>.columns(vararg elements: T) = elements.forEach(::column)

View File

@ -0,0 +1,45 @@
package dev.inmo.micro_utils.matrix
import kotlin.test.Test
import kotlin.test.assertEquals
class SimpleTest {
@Test
fun simpleTest() {
val expected = listOf(
listOf(1, 2, 3),
listOf(4, 5, 6)
)
val fromMatrixWithVarargs = matrix<Int> {
row(1, 2, 3)
row(4, 5, 6)
}
val fromMatrixWithColumnsVarargs = matrix<Int> {
row {
columns(1, 2, 3)
}
row {
columns(4, 5, 6)
}
}
val fromMatrixWithSimpleAdd = matrix<Int> {
row {
add(1)
add(2)
add(3)
}
row {
add(4)
add(5)
add(6)
}
}
assertEquals(expected, fromMatrixWithVarargs)
assertEquals(expected, fromMatrixWithColumnsVarargs)
assertEquals(expected, fromMatrixWithSimpleAdd)
}
}