Skip to content

Commit 2f64430

Browse files
Add OrNull for second, third, forth, fifth and penultimate
1 parent f6ae8a7 commit 2f64430

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66
- `isNotNullNorBlank()` and `isNotNullNorEmpty()`
77
- `second()`, `third()`, `forth()`, `fifth()` and `penultimate()`
8+
- `OrNull` variants for `second()`, `third()`, `forth()`, `fifth()` and `penultimate()`
89

910
### Changed
1011

kotlin-stdlib/api/kotlin-stdlib.api

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,16 @@ public final class com/javiersc/kotlin/stdlib/AnsiColorsKt {
143143

144144
public final class com/javiersc/kotlin/stdlib/CollectionsKt {
145145
public static final fun fifth (Ljava/lang/Iterable;)Ljava/lang/Object;
146+
public static final fun fifthOrNull (Ljava/lang/Iterable;)Ljava/lang/Object;
146147
public static final fun forth (Ljava/lang/Iterable;)Ljava/lang/Object;
148+
public static final fun forthOrNull (Ljava/lang/Iterable;)Ljava/lang/Object;
147149
public static final fun getIndex (Ljava/lang/Iterable;I)Ljava/lang/Object;
148150
public static final fun penultimate (Ljava/lang/Iterable;)Ljava/lang/Object;
151+
public static final fun penultimateOrNull (Ljava/lang/Iterable;)Ljava/lang/Object;
149152
public static final fun second (Ljava/lang/Iterable;)Ljava/lang/Object;
153+
public static final fun secondOrNull (Ljava/lang/Iterable;)Ljava/lang/Object;
150154
public static final fun third (Ljava/lang/Iterable;)Ljava/lang/Object;
155+
public static final fun thirdOrNull (Ljava/lang/Iterable;)Ljava/lang/Object;
151156
public static final fun throwNoSuchElementException (I)V
152157
}
153158

kotlin-stdlib/commonMain/kotlin/Collections.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,35 @@ package com.javiersc.kotlin.stdlib
99
*/
1010
public inline fun <T> Iterable<T>.second(): T = getIndex(2)
1111

12+
public inline fun <T> Iterable<T>.secondOrNull(): T? = runCatching { second() }.getOrNull()
13+
1214
/**
1315
* Returns third element.
1416
*
1517
* Throws: `NoSuchElementException` - if the list size is 2.
1618
*/
1719
public inline fun <T> Iterable<T>.third(): T = getIndex(3)
1820

21+
public inline fun <T> Iterable<T>.thirdOrNull(): T? = runCatching { third() }.getOrNull()
22+
1923
/**
2024
* Returns forth element.
2125
*
2226
* Throws: `NoSuchElementException` - if the list size is 3.
2327
*/
2428
public inline fun <T> Iterable<T>.forth(): T = getIndex(4)
2529

30+
public inline fun <T> Iterable<T>.forthOrNull(): T? = runCatching { forth() }.getOrNull()
31+
2632
/**
2733
* Returns fifth element.
2834
*
2935
* Throws: `NoSuchElementException` - if the list size is 4.
3036
*/
3137
public inline fun <T> Iterable<T>.fifth(): T = getIndex(5)
3238

39+
public inline fun <T> Iterable<T>.fifthOrNull(): T? = runCatching { fifth() }.getOrNull()
40+
3341
/**
3442
* Returns penultimate element.
3543
*
@@ -55,6 +63,9 @@ public inline fun <T> Iterable<T>.penultimate(): T {
5563
}
5664
}
5765

66+
public inline fun <T> Iterable<T>.penultimateOrNull(): T? =
67+
runCatching { penultimate() }.getOrNull()
68+
5869
@PublishedApi
5970
internal inline fun <T> Iterable<T>.getIndex(index: Int): T {
6071
return when (this) {

0 commit comments

Comments
 (0)