Slice functions for arrays and lists.
This commit is contained in:
committed by
Andrey Breslav
parent
aee199b5b0
commit
1db035a0aa
@@ -835,6 +835,126 @@ public inline fun <T, C: MutableCollection<in T>> Stream<T>.filterTo(collection:
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list containing elements at specified positions
|
||||||
|
*/
|
||||||
|
public fun <T> Array<out T>.slice(indices: Iterable<Int>) : List<T> {
|
||||||
|
val list = ArrayList<T>()
|
||||||
|
for (index in indices) {
|
||||||
|
list.add(get(index))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list containing elements at specified positions
|
||||||
|
*/
|
||||||
|
public fun BooleanArray.slice(indices: Iterable<Int>) : List<Boolean> {
|
||||||
|
val list = ArrayList<Boolean>()
|
||||||
|
for (index in indices) {
|
||||||
|
list.add(get(index))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list containing elements at specified positions
|
||||||
|
*/
|
||||||
|
public fun ByteArray.slice(indices: Iterable<Int>) : List<Byte> {
|
||||||
|
val list = ArrayList<Byte>()
|
||||||
|
for (index in indices) {
|
||||||
|
list.add(get(index))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list containing elements at specified positions
|
||||||
|
*/
|
||||||
|
public fun CharArray.slice(indices: Iterable<Int>) : List<Char> {
|
||||||
|
val list = ArrayList<Char>()
|
||||||
|
for (index in indices) {
|
||||||
|
list.add(get(index))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list containing elements at specified positions
|
||||||
|
*/
|
||||||
|
public fun DoubleArray.slice(indices: Iterable<Int>) : List<Double> {
|
||||||
|
val list = ArrayList<Double>()
|
||||||
|
for (index in indices) {
|
||||||
|
list.add(get(index))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list containing elements at specified positions
|
||||||
|
*/
|
||||||
|
public fun FloatArray.slice(indices: Iterable<Int>) : List<Float> {
|
||||||
|
val list = ArrayList<Float>()
|
||||||
|
for (index in indices) {
|
||||||
|
list.add(get(index))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list containing elements at specified positions
|
||||||
|
*/
|
||||||
|
public fun IntArray.slice(indices: Iterable<Int>) : List<Int> {
|
||||||
|
val list = ArrayList<Int>()
|
||||||
|
for (index in indices) {
|
||||||
|
list.add(get(index))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list containing elements at specified positions
|
||||||
|
*/
|
||||||
|
public fun LongArray.slice(indices: Iterable<Int>) : List<Long> {
|
||||||
|
val list = ArrayList<Long>()
|
||||||
|
for (index in indices) {
|
||||||
|
list.add(get(index))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list containing elements at specified positions
|
||||||
|
*/
|
||||||
|
public fun ShortArray.slice(indices: Iterable<Int>) : List<Short> {
|
||||||
|
val list = ArrayList<Short>()
|
||||||
|
for (index in indices) {
|
||||||
|
list.add(get(index))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list containing elements at specified positions
|
||||||
|
*/
|
||||||
|
public fun <T> List<T>.slice(indices: Iterable<Int>) : List<T> {
|
||||||
|
val list = ArrayList<T>()
|
||||||
|
for (index in indices) {
|
||||||
|
list.add(get(index))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list containing first *n* elements
|
* Returns a list containing first *n* elements
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -186,6 +186,22 @@ class ArraysTest {
|
|||||||
assertTrue("0" !in array("1","2","3","4"))
|
assertTrue("0" !in array("1","2","3","4"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test fun slice() {
|
||||||
|
val iter = listOf(3, 1, 2)
|
||||||
|
|
||||||
|
assertEquals(listOf("B"), array("A", "B", "C").slice(1..1))
|
||||||
|
assertEquals(listOf('E', 'B', 'C'), array('A', 'B', 'C', 'E').slice(iter))
|
||||||
|
|
||||||
|
assertEquals(listOf<Int>(), array<Int>().slice(5..4))
|
||||||
|
assertEquals(listOf<Int>(), array(1, 2, 3).slice(5..1))
|
||||||
|
assertEquals(listOf(2, 3, 9), array(2, 3, 9, 2, 3, 9).slice(iter))
|
||||||
|
assertEquals(listOf(2.0, 3.0), array(2.0, 3.0, 9.0).slice(0..1))
|
||||||
|
assertEquals(listOf(2f, 3f), array(2f, 3f, 9f).slice(0..1))
|
||||||
|
assertEquals(listOf<Byte>(127, 100), array<Byte>(50, 100, 127).slice(2 downTo 1))
|
||||||
|
assertEquals(listOf<Short>(200, 100), array<Short>(50, 100, 200).slice(2 downTo 1))
|
||||||
|
assertEquals(listOf(100L, 200L, 30L), array(50L, 100L, 200L, 30L).slice(1..3))
|
||||||
|
assertEquals(listOf(true, false, true), array(true, false, true, true).slice(iter))
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
|
|
||||||
TODO FIXME ASAP: These currently fail on JS due to missing upto() method on numbers
|
TODO FIXME ASAP: These currently fail on JS due to missing upto() method on numbers
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package test.collections
|
package test.collections
|
||||||
|
|
||||||
import java.util.ArrayList
|
|
||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
@@ -19,6 +18,17 @@ class ListSpecificTest {
|
|||||||
assertEquals(expected, actual)
|
assertEquals(expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Test fun slice() {
|
||||||
|
val list = listOf('A', 'B', 'C', 'D')
|
||||||
|
// ABCD
|
||||||
|
// 0123
|
||||||
|
assertEquals(listOf('B', 'C', 'D'), list.slice(1..3))
|
||||||
|
assertEquals(listOf('D', 'C', 'B'), list.slice(3 downTo 1))
|
||||||
|
|
||||||
|
val iter = listOf(2, 0, 3)
|
||||||
|
assertEquals(listOf('C', 'A', 'D'), list.slice(iter))
|
||||||
|
}
|
||||||
|
|
||||||
Test fun utils() {
|
Test fun utils() {
|
||||||
assertNull(empty.head)
|
assertNull(empty.head)
|
||||||
assertNull(empty.first)
|
assertNull(empty.first)
|
||||||
|
|||||||
@@ -264,5 +264,20 @@ fun filtering(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
templates add f("slice(indices: Iterable<Int>)") {
|
||||||
|
only(Lists, ArraysOfPrimitives, ArraysOfObjects)
|
||||||
|
doc { "Returns a list containing elements at specified positions" }
|
||||||
|
returns("List<T>")
|
||||||
|
body {
|
||||||
|
"""
|
||||||
|
val list = ArrayList<T>()
|
||||||
|
for (index in indices) {
|
||||||
|
list.add(get(index))
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return templates
|
return templates
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user