From d41c3c2de48a9d4d00ce20b213840e5989be6ec0 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 11 Feb 2021 15:05:11 +0600 Subject: [PATCH] updates in matrix --- .../inmo/micro_utils/matrix/MatrixBuilder.kt | 7 ++- .../matrix/{Utils.kt => MatrixFactories.kt} | 14 ------ .../dev/inmo/micro_utils/matrix/RowBuilder.kt | 5 +++ .../dev/inmo/micro_utils/matrix/SimpleTest.kt | 45 +++++++++++++++++++ 4 files changed, 55 insertions(+), 16 deletions(-) rename matrix/src/commonMain/kotlin/dev/inmo/micro_utils/matrix/{Utils.kt => MatrixFactories.kt} (56%) create mode 100644 matrix/src/commonTest/kotlin/dev/inmo/micro_utils/matrix/SimpleTest.kt diff --git a/matrix/src/commonMain/kotlin/dev/inmo/micro_utils/matrix/MatrixBuilder.kt b/matrix/src/commonMain/kotlin/dev/inmo/micro_utils/matrix/MatrixBuilder.kt index 5e7ea1d9d16..6c5292f47ee 100644 --- a/matrix/src/commonMain/kotlin/dev/inmo/micro_utils/matrix/MatrixBuilder.kt +++ b/matrix/src/commonMain/kotlin/dev/inmo/micro_utils/matrix/MatrixBuilder.kt @@ -5,6 +5,9 @@ class MatrixBuilder { val matrix: Matrix get() = mutMatrix - fun add(t: List) = mutMatrix.add(t) - operator fun plus(t: List) = add(t) + fun row(t: List) = mutMatrix.add(t) + operator fun List.unaryPlus() = row(this) } + +fun MatrixBuilder.row(block: RowBuilder.() -> Unit) = +RowBuilder().also(block).row +fun MatrixBuilder.row(vararg elements: T) = +elements.toList() diff --git a/matrix/src/commonMain/kotlin/dev/inmo/micro_utils/matrix/Utils.kt b/matrix/src/commonMain/kotlin/dev/inmo/micro_utils/matrix/MatrixFactories.kt similarity index 56% rename from matrix/src/commonMain/kotlin/dev/inmo/micro_utils/matrix/Utils.kt rename to matrix/src/commonMain/kotlin/dev/inmo/micro_utils/matrix/MatrixFactories.kt index ea5c444bf55..045da40645d 100644 --- a/matrix/src/commonMain/kotlin/dev/inmo/micro_utils/matrix/Utils.kt +++ b/matrix/src/commonMain/kotlin/dev/inmo/micro_utils/matrix/MatrixFactories.kt @@ -15,17 +15,3 @@ fun flatMatrix(vararg elements: T): Matrix { row { elements.forEach { +it } } }.matrix } - -fun row(block: RowBuilder.() -> Unit): List { - return RowBuilder().also(block).row -} - -fun MatrixBuilder.row(block: RowBuilder.() -> Unit) { - add(RowBuilder().also(block).row) -} - -fun MatrixBuilder.row(vararg elements: T) { - add(elements.toList()) -} - -operator fun RowBuilder.plus(t: T) = add(t) diff --git a/matrix/src/commonMain/kotlin/dev/inmo/micro_utils/matrix/RowBuilder.kt b/matrix/src/commonMain/kotlin/dev/inmo/micro_utils/matrix/RowBuilder.kt index 197c63037e7..22851e58c30 100644 --- a/matrix/src/commonMain/kotlin/dev/inmo/micro_utils/matrix/RowBuilder.kt +++ b/matrix/src/commonMain/kotlin/dev/inmo/micro_utils/matrix/RowBuilder.kt @@ -8,3 +8,8 @@ class RowBuilder { fun add(t: T) = mutRow.add(t) operator fun T.unaryPlus() = add(this) } + +fun row(block: RowBuilder.() -> Unit): List = RowBuilder().also(block).row +fun RowBuilder.column(element: T) = +element +fun RowBuilder.columns(elements: List) = elements.forEach(::column) +fun RowBuilder.columns(vararg elements: T) = elements.forEach(::column) diff --git a/matrix/src/commonTest/kotlin/dev/inmo/micro_utils/matrix/SimpleTest.kt b/matrix/src/commonTest/kotlin/dev/inmo/micro_utils/matrix/SimpleTest.kt new file mode 100644 index 00000000000..3a8b00e0be1 --- /dev/null +++ b/matrix/src/commonTest/kotlin/dev/inmo/micro_utils/matrix/SimpleTest.kt @@ -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 { + row(1, 2, 3) + row(4, 5, 6) + } + + val fromMatrixWithColumnsVarargs = matrix { + row { + columns(1, 2, 3) + } + row { + columns(4, 5, 6) + } + } + + val fromMatrixWithSimpleAdd = matrix { + row { + add(1) + add(2) + add(3) + } + row { + add(4) + add(5) + add(6) + } + } + + assertEquals(expected, fromMatrixWithVarargs) + assertEquals(expected, fromMatrixWithColumnsVarargs) + assertEquals(expected, fromMatrixWithSimpleAdd) + } +} \ No newline at end of file