Skip to content

Commit 6661d82

Browse files
author
Muhammad Hashim
committed
added matrix() function.
added a unit test.
1 parent 7b87e51 commit 6661d82

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ repositories {
2424

2525
dependencies {
2626
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
27-
testCompile group: 'junit', name: 'junit', version: '4.12'
27+
testCompile "junit:junit:4.12"
28+
2829
}
2930

3031
compileKotlin {

src/main/kotlin/mhashim6/kotlin/arrays2d/2DArrayExtensions.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package mhashim6.kotlin.arrays2d
22

3+
inline fun <reified T> matrix(row: Int, col: Int, noinline itemFactory: (i: Int, j: Int) -> T): Array<Array<T>> {
4+
return Array(row) { i -> Array(col) { j -> itemFactory(i, j) } }
5+
}
6+
37
inline fun <T> Array<Array<T>>.forEach2D(action: (T) -> Unit) {
48
for (array in this)
59
for (item in array)

src/test/kotlin/Test.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import mhashim6.kotlin.arrays2d.asSequence2D
2+
import mhashim6.kotlin.arrays2d.matrix
3+
import org.junit.Test
4+
5+
class Test {
6+
7+
@Test
8+
fun generalTest() {
9+
var count = 0
10+
val matrix = matrix(4, 4) { i, j -> Pair(i, j) }
11+
matrix.asSequence2D()
12+
.filter { it.second > 1 }
13+
.forEach { count++ }
14+
15+
assert(count == 8)
16+
}
17+
18+
}

0 commit comments

Comments
 (0)