Add samples for last() and lastOrNull() functions

This commit is contained in:
Kris
2020-10-02 14:46:12 -05:00
committed by Abduqodiri Qurbonzoda
parent e83a3c3f27
commit ebdd023633
8 changed files with 245 additions and 38 deletions
@@ -139,7 +139,10 @@ public inline fun CharSequence.indexOfLast(predicate: (Char) -> Boolean): Int {
/**
* Returns the last character.
* @throws [NoSuchElementException] if the char sequence is empty.
*
* @throws NoSuchElementException if the char sequence is empty.
*
* @sample samples.text.Strings.last
*/
public fun CharSequence.last(): Char {
if (isEmpty())
@@ -149,7 +152,10 @@ public fun CharSequence.last(): Char {
/**
* Returns the last character matching the given [predicate].
* @throws [NoSuchElementException] if no such character is found.
*
* @throws NoSuchElementException if no such character is found.
*
* @sample samples.text.Strings.last
*/
public inline fun CharSequence.last(predicate: (Char) -> Boolean): Char {
for (index in this.indices.reversed()) {
@@ -161,6 +167,8 @@ public inline fun CharSequence.last(predicate: (Char) -> Boolean): Char {
/**
* Returns the last character, or `null` if the char sequence is empty.
*
* @sample samples.text.Strings.last
*/
public fun CharSequence.lastOrNull(): Char? {
return if (isEmpty()) null else this[length - 1]
@@ -168,6 +176,8 @@ public fun CharSequence.lastOrNull(): Char? {
/**
* Returns the last character matching the given [predicate], or `null` if no such character was found.
*
* @sample samples.text.Strings.last
*/
public inline fun CharSequence.lastOrNull(predicate: (Char) -> Boolean): Char? {
for (index in this.indices.reversed()) {