Introduce T.addTo(MutableCollection<T>), mapNotNull and mapIndexedNotNull extensions.

#KT-4410 Fixed
This commit is contained in:
Ilya Gorbunov
2015-11-07 06:50:19 +03:00
parent 008a8059cf
commit 9323f61eaf
10 changed files with 398 additions and 145 deletions
@@ -763,6 +763,27 @@ class ArraysTest {
expect(listOf("a"), { arrayOf("a", null).filterNotNull() })
}
@test fun map() {
assertEquals(listOf(1, 2, 4), arrayOf("a", "bc", "test").map { it.length })
assertEquals(listOf('a', 'b', 'c'), intArrayOf(1, 2, 3).map { 'a' + it - 1 })
assertEquals(listOf(1, 2, 3), longArrayOf(1000, 2000, 3000).map { (it / 1000).toInt() })
assertEquals(listOf(1.0, 0.5, 0.4, 0.2, 0.1), doubleArrayOf(1.0, 2.0, 2.5, 5.0, 10.0).map { 1 / it })
}
@test fun mapIndexed() {
assertEquals(listOf(1, 1, 2), arrayOf("a", "bc", "test").mapIndexed { index, s -> s.length - index })
assertEquals(listOf(0, 2, 2), intArrayOf(3, 2, 1).mapIndexed { index, i -> i * index })
assertEquals(listOf("0;20", "1;21", "2;22"), longArrayOf(20, 21, 22).mapIndexed { index, it -> "$index;$it" })
}
@test fun mapNotNull() {
assertEquals(listOf(2, 3), arrayOf("", "bc", "def").mapNotNull { if (it.isEmpty()) null else it.length })
}
@test fun mapIndexedNotNull() {
assertEquals(listOf(2), arrayOf("a", null, "test").mapIndexedNotNull { index, it -> it?.run { if (index != 0) length / index else null } })
}
@test fun asListPrimitives() {
// Array of primitives
val arr = intArrayOf(1, 2, 3, 4, 2, 5)
@@ -244,6 +244,16 @@ abstract class IterableTests<T : Iterable<String>>(val data: T, val empty: T) {
assertEquals(listOf("f", "ba"), indexed)
}
@Test
fun mapNotNull() {
assertEquals(listOf('o'), data.mapNotNull { it.firstOrNull { c -> c in "oui" } })
}
@Test
fun mapIndexedNotNull() {
assertEquals(listOf('b'), data.mapIndexedNotNull { index, s -> s.getOrNull(index - 1) })
}
@Test
fun max() {
expect("foo") { data.max() }
@@ -129,6 +129,13 @@ class MapTest {
assertEquals(listOf("beer rocks", "Mells rocks"), list)
}
@test fun mapNotNull() {
val m1 = mapOf("a" to 1, "b" to null)
val list = m1.mapNotNull { it.value?.let { v -> "${it.key}$v" } }
assertEquals(listOf("a1"), list)
}
@test fun mapValues() {
val m1 = mapOf("beverage" to "beer", "location" to "Mells")
val m2 = m1.mapValues { it.value + "2" }
@@ -6,15 +6,8 @@ import java.util.*
fun fibonacci(): Sequence<Int> {
// fibonacci terms
var index = 0;
var a = 0;
var b = 1
return sequence {
when (index++) { 0 -> a; 1 -> b; else -> {
val result = a + b; a = b; b = result; result
}
}
}
// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, ...
return sequence(Pair(0, 1), { Pair(it.second, it.first + it.second) }).map { it.first }
}
public class SequenceTest {
@@ -67,22 +60,22 @@ public class SequenceTest {
assertEquals(listOf("foo", "bar"), filtered.toList())
}
/*
@test fun mapNotNull() {
val data = sequenceOf(null, "foo", null, "bar")
val foo = data.mapNotNull { it.length() }
assertEquals(listOf(3, 3), foo.toList())
assertTrue {
foo is Sequence<Int>
}
}
*/
@test fun mapIndexed() {
assertEquals(listOf(0, 1, 2, 6, 12), fibonacci().mapIndexed { index, value -> index * value }.takeWhile { i: Int -> i < 20 }.toList())
}
@test fun mapNotNull() {
assertEquals(listOf(0, 10, 110, 1220), fibonacci().mapNotNull { if (it % 5 == 0) it * 2 else null }.take(4).toList())
}
@test fun mapIndexedNotNull() {
// find which terms are divisible by their index
assertEquals(listOf("1/1", "5/5", "144/12", "46368/24", "75025/25"),
fibonacci().mapIndexedNotNull { index, value ->
if (index > 0 && (value % index) == 0) "$value/$index" else null
}.take(5).toList())
}
@test fun mapAndJoinToString() {
assertEquals("3, 5, 8", fibonacci().withIndex().filter { it.index > 3 }.take(3).joinToString { it.value.toString() })
@@ -91,7 +84,6 @@ public class SequenceTest {
@test fun withIndex() {
val data = sequenceOf("foo", "bar")
val indexed = data.withIndex().map { it.value.substring(0..it.index) }.toList()
assertEquals(2, indexed.size())
assertEquals(listOf("f", "ba"), indexed)
}