Add samples for String/CharSequence filter & filterNot (#3390)

* Add samples for String/CharSequence filter & filterNot
* Link samples to stdlib generator
This commit is contained in:
Segun Famisa
2020-05-18 22:15:41 +02:00
committed by GitHub
parent 25f83e941c
commit d6a8003743
3 changed files with 24 additions and 4 deletions
@@ -367,7 +367,7 @@ 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
* @sample samples.text.Strings.filter
*/
public inline fun CharSequence.filter(predicate: (Char) -> Boolean): CharSequence {
return filterTo(StringBuilder(), predicate)
@@ -376,7 +376,7 @@ 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].
*
* @sample samples.collections.Collections.Filtering.filter
* @sample samples.text.Strings.filter
*/
public inline fun String.filter(predicate: (Char) -> Boolean): String {
return filterTo(StringBuilder(), predicate).toString()
@@ -415,7 +415,7 @@ 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
* @sample samples.text.Strings.filterNot
*/
public inline fun CharSequence.filterNot(predicate: (Char) -> Boolean): CharSequence {
return filterNotTo(StringBuilder(), predicate)
@@ -424,7 +424,7 @@ 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].
*
* @sample samples.collections.Collections.Filtering.filter
* @sample samples.text.Strings.filterNot
*/
public inline fun String.filterNot(predicate: (Char) -> Boolean): String {
return filterNotTo(StringBuilder(), predicate).toString()
@@ -79,6 +79,24 @@ class Strings {
assertPrints(proteins.take(5).toList(), "[Isoleucine, Arginine, Glycine, Arginine, Glutamine]")
}
@Sample
fun filter() {
val text = "a1b2c3d4e5"
val textWithOnlyDigits = text.filter { it.isDigit() }
assertPrints(textWithOnlyDigits, "12345")
}
@Sample
fun filterNot() {
val text = "a1b2c3d4e5"
val textWithoutDigits = text.filterNot { it.isDigit() }
assertPrints(textWithoutDigits, "abcde")
}
@Sample
fun zip() {
val stringA = "abcd"
@@ -557,6 +557,7 @@ object Filtering : TemplateGroupBase() {
}
specialFor(Strings, CharSequences) {
sample("samples.text.Strings.filter")
returns("SELF")
doc { "Returns a ${f.collection} containing only those characters from the original ${f.collection} that match the given [predicate]." }
body { """return filterTo(StringBuilder(), predicate)${toResult(f)}""" }
@@ -686,6 +687,7 @@ object Filtering : TemplateGroupBase() {
}
specialFor(Strings, CharSequences) {
sample("samples.text.Strings.filterNot")
returns("SELF")
doc { "Returns a ${f.collection} containing only those characters from the original ${f.collection} that do not match the given [predicate]." }
body { """return filterNotTo(StringBuilder(), predicate)${toResult(f)}""" }