mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-19 23:03:49 +00:00
updates in matrix
This commit is contained in:
parent
a3cabd7a9e
commit
d41c3c2de4
@ -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()
|
||||
|
@ -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)
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user