KT-20357: Add samples for filter functions

This commit is contained in:
Dat Trieu
2020-06-04 19:16:41 +02:00
committed by Abduqodiri Qurbonzoda
parent ca6e430e89
commit 1009a240f2
10 changed files with 289 additions and 0 deletions
@@ -386,6 +386,8 @@ public inline fun String.filter(predicate: (Char) -> Boolean): String {
* Returns a char sequence containing only those characters from the original char sequence that match the given [predicate].
* @param [predicate] function that takes the index of a character and the character itself
* and returns the result of predicate evaluation on the character.
*
* @sample samples.collections.Collections.Filtering.filterIndexed
*/
public inline fun CharSequence.filterIndexed(predicate: (index: Int, Char) -> Boolean): CharSequence {
return filterIndexedTo(StringBuilder(), predicate)
@@ -395,6 +397,8 @@ public inline fun CharSequence.filterIndexed(predicate: (index: Int, Char) -> Bo
* Returns a string containing only those characters from the original string that match the given [predicate].
* @param [predicate] function that takes the index of a character and the character itself
* and returns the result of predicate evaluation on the character.
*
* @sample samples.collections.Collections.Filtering.filterIndexed
*/
public inline fun String.filterIndexed(predicate: (index: Int, Char) -> Boolean): String {
return filterIndexedTo(StringBuilder(), predicate).toString()
@@ -404,6 +408,8 @@ public inline fun String.filterIndexed(predicate: (index: Int, Char) -> Boolean)
* Appends all characters matching the given [predicate] to the given [destination].
* @param [predicate] function that takes the index of a character and the character itself
* and returns the result of predicate evaluation on the character.
*
* @sample samples.collections.Collections.Filtering.filterIndexedTo
*/
public inline fun <C : Appendable> CharSequence.filterIndexedTo(destination: C, predicate: (index: Int, Char) -> Boolean): C {
forEachIndexed { index, element ->
@@ -432,6 +438,8 @@ public inline fun String.filterNot(predicate: (Char) -> Boolean): String {
/**
* Appends all characters not matching the given [predicate] to the given [destination].
*
* @sample samples.collections.Collections.Filtering.filterTo
*/
public inline fun <C : Appendable> CharSequence.filterNotTo(destination: C, predicate: (Char) -> Boolean): C {
for (element in this) if (!predicate(element)) destination.append(element)
@@ -440,6 +448,8 @@ public inline fun <C : Appendable> CharSequence.filterNotTo(destination: C, pred
/**
* Appends all characters matching the given [predicate] to the given [destination].
*
* @sample samples.collections.Collections.Filtering.filterTo
*/
public inline fun <C : Appendable> CharSequence.filterTo(destination: C, predicate: (Char) -> Boolean): C {
for (index in 0 until length) {