Skip to content

Commit

Permalink
Add sample for filterNot and improve sample for filter
Browse files Browse the repository at this point in the history
(cherry picked from commit dd2aa3d)
  • Loading branch information
ilya-g committed Apr 10, 2020
1 parent be9ff5e commit 48a6025
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 6 deletions.
36 changes: 35 additions & 1 deletion libraries/stdlib/common/src/generated/_Arrays.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3591,63 +3591,79 @@ public inline fun CharArray.dropWhile(predicate: (Char) -> Boolean): List<Char>
/**
* 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> {
return filterTo(ArrayList<T>(), 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> {
return filterTo(ArrayList<Byte>(), 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> {
return filterTo(ArrayList<Short>(), 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> {
return filterTo(ArrayList<Int>(), 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> {
return filterTo(ArrayList<Long>(), 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> {
return filterTo(ArrayList<Float>(), 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> {
return filterTo(ArrayList<Double>(), 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> {
return filterTo(ArrayList<Boolean>(), 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> {
return filterTo(ArrayList<Char>(), predicate)
Expand Down Expand Up @@ -3859,62 +3875,80 @@ public inline fun <reified R, C : MutableCollection<in R>> Array<*>.filterIsInst

/**
* 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> {
return filterNotTo(ArrayList<T>(), 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> {
return filterNotTo(ArrayList<Byte>(), 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> {
return filterNotTo(ArrayList<Short>(), 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> {
return filterNotTo(ArrayList<Int>(), 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> {
return filterNotTo(ArrayList<Long>(), 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> {
return filterNotTo(ArrayList<Float>(), 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> {
return filterNotTo(ArrayList<Double>(), 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> {
return filterNotTo(ArrayList<Boolean>(), 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> {
return filterNotTo(ArrayList<Char>(), predicate)
Expand Down
2 changes: 2 additions & 0 deletions libraries/stdlib/common/src/generated/_Collections.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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].
*
* @sample samples.collections.Collections.Filtering.filter
*/
public inline fun <T> Iterable<T>.filterNot(predicate: (T) -> Boolean): List<T> {
return filterNotTo(ArrayList<T>(), predicate)
Expand Down
6 changes: 4 additions & 2 deletions libraries/stdlib/common/src/generated/_Sequences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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].
*
* 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> {
return FilteringSequence(this, true, predicate)
Expand Down Expand Up @@ -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].
*
* The operation is _intermediate_ and _stateless_.
*
* @sample samples.collections.Collections.Filtering.filter
*/
public fun <T> Sequence<T>.filterNot(predicate: (T) -> Boolean): Sequence<T> {
return FilteringSequence(this, false, predicate)
Expand Down
8 changes: 8 additions & 0 deletions libraries/stdlib/common/src/generated/_Strings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,17 @@ 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].
*
* @sample samples.collections.Collections.Filtering.filter
*/
public inline fun CharSequence.filter(predicate: (Char) -> Boolean): CharSequence {
return filterTo(StringBuilder(), 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 {
return filterTo(StringBuilder(), predicate).toString()
Expand Down Expand Up @@ -410,13 +414,17 @@ 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].
*
* @sample samples.collections.Collections.Filtering.filter
*/
public inline fun CharSequence.filterNot(predicate: (Char) -> Boolean): CharSequence {
return filterNotTo(StringBuilder(), 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 {
return filterNotTo(StringBuilder(), predicate).toString()
Expand Down
16 changes: 16 additions & 0 deletions libraries/stdlib/common/src/generated/_UArrays.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,8 @@ public inline fun UShortArray.dropWhile(predicate: (UShort) -> Boolean): List<US

/**
* Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
Expand All @@ -1802,6 +1804,8 @@ public inline fun UIntArray.filter(predicate: (UInt) -> Boolean): List<UInt> {

/**
* Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
Expand All @@ -1812,6 +1816,8 @@ public inline fun ULongArray.filter(predicate: (ULong) -> Boolean): List<ULong>

/**
* Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
Expand All @@ -1822,6 +1828,8 @@ public inline fun UByteArray.filter(predicate: (UByte) -> Boolean): List<UByte>

/**
* Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
Expand Down Expand Up @@ -1940,6 +1948,8 @@ public inline fun <C : MutableCollection<in UShort>> UShortArray.filterIndexedTo

/**
* Returns a list containing all elements not matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
Expand All @@ -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].
*
* @sample samples.collections.Collections.Filtering.filter
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
Expand All @@ -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].
*
* @sample samples.collections.Collections.Filtering.filter
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
Expand All @@ -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].
*
* @sample samples.collections.Collections.Filtering.filter
*/
@SinceKotlin("1.3")
@ExperimentalUnsignedTypes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,10 +792,12 @@ class Collections {

@Sample
fun filter() {
val numbers: List<Int> = listOf(1, 2, 3, 4)
val evenNumbers = numbers.filter { number -> number.rem(2) == 0 }
val numbers: List<Int> = listOf(1, 2, 3, 4, 5, 6, 7)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ object Filtering : TemplateGroupBase() {
specialFor(ArraysOfUnsigned) { inlineOnly() }

doc { "Returns a list containing all elements not matching the given [predicate]." }
sample("samples.collections.Collections.Filtering.filter")
returns("List<T>")
body {
"""
Expand Down

0 comments on commit 48a6025

Please sign in to comment.