Add sample for filterNot and improve sample for filter

This commit is contained in:
Ilya Gorbunov
2020-03-31 00:21:19 +03:00
parent f887a29279
commit dd2aa3de28
7 changed files with 71 additions and 6 deletions
@@ -3591,7 +3591,7 @@ public inline fun CharArray.dropWhile(predicate: (Char) -> Boolean): List<Char>
/** /**
* Returns a list containing only elements matching the given [predicate]. * Returns a list containing only elements matching the given [predicate].
* *
* @sample samples.collections.Collections.Filtering.filter * @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun <T> Array<out T>.filter(predicate: (T) -> Boolean): List<T> { public inline fun <T> Array<out T>.filter(predicate: (T) -> Boolean): List<T> {
return filterTo(ArrayList<T>(), predicate) return filterTo(ArrayList<T>(), predicate)
@@ -3599,6 +3599,8 @@ public inline fun <T> Array<out T>.filter(predicate: (T) -> Boolean): List<T> {
/** /**
* Returns a list containing only elements matching the given [predicate]. * Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun ByteArray.filter(predicate: (Byte) -> Boolean): List<Byte> { public inline fun ByteArray.filter(predicate: (Byte) -> Boolean): List<Byte> {
return filterTo(ArrayList<Byte>(), predicate) return filterTo(ArrayList<Byte>(), predicate)
@@ -3606,6 +3608,8 @@ public inline fun ByteArray.filter(predicate: (Byte) -> Boolean): List<Byte> {
/** /**
* Returns a list containing only elements matching the given [predicate]. * Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun ShortArray.filter(predicate: (Short) -> Boolean): List<Short> { public inline fun ShortArray.filter(predicate: (Short) -> Boolean): List<Short> {
return filterTo(ArrayList<Short>(), predicate) return filterTo(ArrayList<Short>(), predicate)
@@ -3613,6 +3617,8 @@ public inline fun ShortArray.filter(predicate: (Short) -> Boolean): List<Short>
/** /**
* Returns a list containing only elements matching the given [predicate]. * Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun IntArray.filter(predicate: (Int) -> Boolean): List<Int> { public inline fun IntArray.filter(predicate: (Int) -> Boolean): List<Int> {
return filterTo(ArrayList<Int>(), predicate) return filterTo(ArrayList<Int>(), predicate)
@@ -3620,6 +3626,8 @@ public inline fun IntArray.filter(predicate: (Int) -> Boolean): List<Int> {
/** /**
* Returns a list containing only elements matching the given [predicate]. * Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun LongArray.filter(predicate: (Long) -> Boolean): List<Long> { public inline fun LongArray.filter(predicate: (Long) -> Boolean): List<Long> {
return filterTo(ArrayList<Long>(), predicate) return filterTo(ArrayList<Long>(), predicate)
@@ -3627,6 +3635,8 @@ public inline fun LongArray.filter(predicate: (Long) -> Boolean): List<Long> {
/** /**
* Returns a list containing only elements matching the given [predicate]. * Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun FloatArray.filter(predicate: (Float) -> Boolean): List<Float> { public inline fun FloatArray.filter(predicate: (Float) -> Boolean): List<Float> {
return filterTo(ArrayList<Float>(), predicate) return filterTo(ArrayList<Float>(), predicate)
@@ -3634,6 +3644,8 @@ public inline fun FloatArray.filter(predicate: (Float) -> Boolean): List<Float>
/** /**
* Returns a list containing only elements matching the given [predicate]. * Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun DoubleArray.filter(predicate: (Double) -> Boolean): List<Double> { public inline fun DoubleArray.filter(predicate: (Double) -> Boolean): List<Double> {
return filterTo(ArrayList<Double>(), predicate) return filterTo(ArrayList<Double>(), predicate)
@@ -3641,6 +3653,8 @@ public inline fun DoubleArray.filter(predicate: (Double) -> Boolean): List<Doubl
/** /**
* Returns a list containing only elements matching the given [predicate]. * Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun BooleanArray.filter(predicate: (Boolean) -> Boolean): List<Boolean> { public inline fun BooleanArray.filter(predicate: (Boolean) -> Boolean): List<Boolean> {
return filterTo(ArrayList<Boolean>(), predicate) return filterTo(ArrayList<Boolean>(), predicate)
@@ -3648,6 +3662,8 @@ public inline fun BooleanArray.filter(predicate: (Boolean) -> Boolean): List<Boo
/** /**
* Returns a list containing only elements matching the given [predicate]. * Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun CharArray.filter(predicate: (Char) -> Boolean): List<Char> { public inline fun CharArray.filter(predicate: (Char) -> Boolean): List<Char> {
return filterTo(ArrayList<Char>(), predicate) return filterTo(ArrayList<Char>(), predicate)
@@ -3859,6 +3875,8 @@ public inline fun <reified R, C : MutableCollection<in R>> Array<*>.filterIsInst
/** /**
* Returns a list containing all elements not matching the given [predicate]. * Returns a list containing all elements not matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun <T> Array<out T>.filterNot(predicate: (T) -> Boolean): List<T> { public inline fun <T> Array<out T>.filterNot(predicate: (T) -> Boolean): List<T> {
return filterNotTo(ArrayList<T>(), predicate) return filterNotTo(ArrayList<T>(), predicate)
@@ -3866,6 +3884,8 @@ public inline fun <T> Array<out T>.filterNot(predicate: (T) -> Boolean): List<T>
/** /**
* Returns a list containing all elements not matching the given [predicate]. * Returns a list containing all elements not matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun ByteArray.filterNot(predicate: (Byte) -> Boolean): List<Byte> { public inline fun ByteArray.filterNot(predicate: (Byte) -> Boolean): List<Byte> {
return filterNotTo(ArrayList<Byte>(), predicate) return filterNotTo(ArrayList<Byte>(), predicate)
@@ -3873,6 +3893,8 @@ public inline fun ByteArray.filterNot(predicate: (Byte) -> Boolean): List<Byte>
/** /**
* Returns a list containing all elements not matching the given [predicate]. * Returns a list containing all elements not matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun ShortArray.filterNot(predicate: (Short) -> Boolean): List<Short> { public inline fun ShortArray.filterNot(predicate: (Short) -> Boolean): List<Short> {
return filterNotTo(ArrayList<Short>(), predicate) return filterNotTo(ArrayList<Short>(), predicate)
@@ -3880,6 +3902,8 @@ public inline fun ShortArray.filterNot(predicate: (Short) -> Boolean): List<Shor
/** /**
* Returns a list containing all elements not matching the given [predicate]. * Returns a list containing all elements not matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun IntArray.filterNot(predicate: (Int) -> Boolean): List<Int> { public inline fun IntArray.filterNot(predicate: (Int) -> Boolean): List<Int> {
return filterNotTo(ArrayList<Int>(), predicate) return filterNotTo(ArrayList<Int>(), predicate)
@@ -3887,6 +3911,8 @@ public inline fun IntArray.filterNot(predicate: (Int) -> Boolean): List<Int> {
/** /**
* Returns a list containing all elements not matching the given [predicate]. * Returns a list containing all elements not matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun LongArray.filterNot(predicate: (Long) -> Boolean): List<Long> { public inline fun LongArray.filterNot(predicate: (Long) -> Boolean): List<Long> {
return filterNotTo(ArrayList<Long>(), predicate) return filterNotTo(ArrayList<Long>(), predicate)
@@ -3894,6 +3920,8 @@ public inline fun LongArray.filterNot(predicate: (Long) -> Boolean): List<Long>
/** /**
* Returns a list containing all elements not matching the given [predicate]. * Returns a list containing all elements not matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun FloatArray.filterNot(predicate: (Float) -> Boolean): List<Float> { public inline fun FloatArray.filterNot(predicate: (Float) -> Boolean): List<Float> {
return filterNotTo(ArrayList<Float>(), predicate) return filterNotTo(ArrayList<Float>(), predicate)
@@ -3901,6 +3929,8 @@ public inline fun FloatArray.filterNot(predicate: (Float) -> Boolean): List<Floa
/** /**
* Returns a list containing all elements not matching the given [predicate]. * Returns a list containing all elements not matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun DoubleArray.filterNot(predicate: (Double) -> Boolean): List<Double> { public inline fun DoubleArray.filterNot(predicate: (Double) -> Boolean): List<Double> {
return filterNotTo(ArrayList<Double>(), predicate) return filterNotTo(ArrayList<Double>(), predicate)
@@ -3908,6 +3938,8 @@ public inline fun DoubleArray.filterNot(predicate: (Double) -> Boolean): List<Do
/** /**
* Returns a list containing all elements not matching the given [predicate]. * Returns a list containing all elements not matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun BooleanArray.filterNot(predicate: (Boolean) -> Boolean): List<Boolean> { public inline fun BooleanArray.filterNot(predicate: (Boolean) -> Boolean): List<Boolean> {
return filterNotTo(ArrayList<Boolean>(), predicate) return filterNotTo(ArrayList<Boolean>(), predicate)
@@ -3915,6 +3947,8 @@ public inline fun BooleanArray.filterNot(predicate: (Boolean) -> Boolean): List<
/** /**
* Returns a list containing all elements not matching the given [predicate]. * Returns a list containing all elements not matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun CharArray.filterNot(predicate: (Char) -> Boolean): List<Char> { public inline fun CharArray.filterNot(predicate: (Char) -> Boolean): List<Char> {
return filterNotTo(ArrayList<Char>(), predicate) return filterNotTo(ArrayList<Char>(), predicate)
@@ -744,6 +744,8 @@ public inline fun <reified R, C : MutableCollection<in R>> Iterable<*>.filterIsI
/** /**
* Returns a list containing all elements not matching the given [predicate]. * Returns a list containing all elements not matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun <T> Iterable<T>.filterNot(predicate: (T) -> Boolean): List<T> { public inline fun <T> Iterable<T>.filterNot(predicate: (T) -> Boolean): List<T> {
return filterNotTo(ArrayList<T>(), predicate) return filterNotTo(ArrayList<T>(), predicate)
@@ -375,8 +375,8 @@ public fun <T> Sequence<T>.dropWhile(predicate: (T) -> Boolean): Sequence<T> {
* Returns a sequence containing only elements matching the given [predicate]. * Returns a sequence containing only elements matching the given [predicate].
* *
* The operation is _intermediate_ and _stateless_. * The operation is _intermediate_ and _stateless_.
* *
* @sample samples.collections.Collections.Filtering.filter * @sample samples.collections.Collections.Filtering.filter
*/ */
public fun <T> Sequence<T>.filter(predicate: (T) -> Boolean): Sequence<T> { public fun <T> Sequence<T>.filter(predicate: (T) -> Boolean): Sequence<T> {
return FilteringSequence(this, true, predicate) return FilteringSequence(this, true, predicate)
@@ -432,6 +432,8 @@ public inline fun <reified R, C : MutableCollection<in R>> Sequence<*>.filterIsI
* Returns a sequence containing all elements not matching the given [predicate]. * Returns a sequence containing all elements not matching the given [predicate].
* *
* The operation is _intermediate_ and _stateless_. * The operation is _intermediate_ and _stateless_.
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public fun <T> Sequence<T>.filterNot(predicate: (T) -> Boolean): Sequence<T> { public fun <T> Sequence<T>.filterNot(predicate: (T) -> Boolean): Sequence<T> {
return FilteringSequence(this, false, predicate) return FilteringSequence(this, false, predicate)
@@ -366,6 +366,8 @@ public inline fun String.dropWhile(predicate: (Char) -> Boolean): String {
/** /**
* Returns a char sequence containing only those characters from the original char sequence that match the given [predicate]. * Returns a char sequence containing only those characters from the original char sequence that match the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun CharSequence.filter(predicate: (Char) -> Boolean): CharSequence { public inline fun CharSequence.filter(predicate: (Char) -> Boolean): CharSequence {
return filterTo(StringBuilder(), predicate) return filterTo(StringBuilder(), predicate)
@@ -373,6 +375,8 @@ public inline fun CharSequence.filter(predicate: (Char) -> Boolean): CharSequenc
/** /**
* Returns a string containing only those characters from the original string that match the given [predicate]. * Returns a string containing only those characters from the original string that match the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun String.filter(predicate: (Char) -> Boolean): String { public inline fun String.filter(predicate: (Char) -> Boolean): String {
return filterTo(StringBuilder(), predicate).toString() return filterTo(StringBuilder(), predicate).toString()
@@ -410,6 +414,8 @@ public inline fun <C : Appendable> CharSequence.filterIndexedTo(destination: C,
/** /**
* Returns a char sequence containing only those characters from the original char sequence that do not match the given [predicate]. * Returns a char sequence containing only those characters from the original char sequence that do not match the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun CharSequence.filterNot(predicate: (Char) -> Boolean): CharSequence { public inline fun CharSequence.filterNot(predicate: (Char) -> Boolean): CharSequence {
return filterNotTo(StringBuilder(), predicate) return filterNotTo(StringBuilder(), predicate)
@@ -417,6 +423,8 @@ public inline fun CharSequence.filterNot(predicate: (Char) -> Boolean): CharSequ
/** /**
* Returns a string containing only those characters from the original string that do not match the given [predicate]. * Returns a string containing only those characters from the original string that do not match the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
public inline fun String.filterNot(predicate: (Char) -> Boolean): String { public inline fun String.filterNot(predicate: (Char) -> Boolean): String {
return filterNotTo(StringBuilder(), predicate).toString() return filterNotTo(StringBuilder(), predicate).toString()
@@ -1792,6 +1792,8 @@ public inline fun UShortArray.dropWhile(predicate: (UShort) -> Boolean): List<US
/** /**
* Returns a list containing only elements matching the given [predicate]. * Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
@@ -1802,6 +1804,8 @@ public inline fun UIntArray.filter(predicate: (UInt) -> Boolean): List<UInt> {
/** /**
* Returns a list containing only elements matching the given [predicate]. * Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
@@ -1812,6 +1816,8 @@ public inline fun ULongArray.filter(predicate: (ULong) -> Boolean): List<ULong>
/** /**
* Returns a list containing only elements matching the given [predicate]. * Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
@@ -1822,6 +1828,8 @@ public inline fun UByteArray.filter(predicate: (UByte) -> Boolean): List<UByte>
/** /**
* Returns a list containing only elements matching the given [predicate]. * Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
@@ -1940,6 +1948,8 @@ public inline fun <C : MutableCollection<in UShort>> UShortArray.filterIndexedTo
/** /**
* Returns a list containing all elements not matching the given [predicate]. * Returns a list containing all elements not matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
@@ -1950,6 +1960,8 @@ public inline fun UIntArray.filterNot(predicate: (UInt) -> Boolean): List<UInt>
/** /**
* Returns a list containing all elements not matching the given [predicate]. * Returns a list containing all elements not matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
@@ -1960,6 +1972,8 @@ public inline fun ULongArray.filterNot(predicate: (ULong) -> Boolean): List<ULon
/** /**
* Returns a list containing all elements not matching the given [predicate]. * Returns a list containing all elements not matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
@@ -1970,6 +1984,8 @@ public inline fun UByteArray.filterNot(predicate: (UByte) -> Boolean): List<UByt
/** /**
* Returns a list containing all elements not matching the given [predicate]. * Returns a list containing all elements not matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/ */
@SinceKotlin("1.3") @SinceKotlin("1.3")
@ExperimentalUnsignedTypes @ExperimentalUnsignedTypes
@@ -806,10 +806,12 @@ class Collections {
@Sample @Sample
fun filter() { fun filter() {
val numbers: List<Int> = listOf(1, 2, 3, 4) val numbers: List<Int> = listOf(1, 2, 3, 4, 5, 6, 7)
val evenNumbers = numbers.filter { number -> number.rem(2) == 0 } val evenNumbers = numbers.filter { it % 2 == 0 }
val notMultiplesOf3 = numbers.filterNot { number -> number % 3 == 0 }
assertPrints(evenNumbers, "[2, 4]") assertPrints(evenNumbers, "[2, 4, 6]")
assertPrints(notMultiplesOf3, "[1, 2, 4, 5, 7]")
} }
@Sample @Sample
@@ -677,6 +677,7 @@ object Filtering : TemplateGroupBase() {
specialFor(ArraysOfUnsigned) { inlineOnly() } specialFor(ArraysOfUnsigned) { inlineOnly() }
doc { "Returns a list containing all elements not matching the given [predicate]." } doc { "Returns a list containing all elements not matching the given [predicate]." }
sample("samples.collections.Collections.Filtering.filter")
returns("List<T>") returns("List<T>")
body { body {
""" """