Skip to content

Commit c216500

Browse files
Add second(), third(), forth(), fifth() and penultimate()
1 parent 4589082 commit c216500

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44

55
### Added
6+
- `second()`, `third()`, `forth()`, `fifth()` and `penultimate()`
67

78
### Changed
89

kotlin-stdlib/api/kotlin-stdlib.api

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ public final class com/javiersc/kotlin/stdlib/AnsiColorsKt {
141141
public static final fun ansiColor (Ljava/lang/String;Lcom/javiersc/kotlin/stdlib/AnsiColor;)Ljava/lang/String;
142142
}
143143

144+
public final class com/javiersc/kotlin/stdlib/CollectionsKt {
145+
public static final fun fifth (Ljava/lang/Iterable;)Ljava/lang/Object;
146+
public static final fun forth (Ljava/lang/Iterable;)Ljava/lang/Object;
147+
public static final fun getIndex (Ljava/lang/Iterable;I)Ljava/lang/Object;
148+
public static final fun penultimate (Ljava/lang/Iterable;)Ljava/lang/Object;
149+
public static final fun second (Ljava/lang/Iterable;)Ljava/lang/Object;
150+
public static final fun third (Ljava/lang/Iterable;)Ljava/lang/Object;
151+
public static final fun throwNoSuchElementException (I)V
152+
}
153+
144154
public final class com/javiersc/kotlin/stdlib/StringsKt {
145155
public static final fun remove (Ljava/lang/String;Ljava/lang/String;Z)Ljava/lang/String;
146156
public static synthetic fun remove$default (Ljava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Ljava/lang/String;

kotlin-stdlib/build.gradle.kts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ kotlin {
3434
watchosArm64()
3535
watchosSimulatorArm64()
3636
watchosX64()
37+
38+
sourceSets {
39+
commonTest {
40+
dependencies {
41+
implementation(libs.jetbrains.kotlin.kotlinTest)
42+
// implementation(libs.kotest.kotestAssertionsCore)
43+
}
44+
}
45+
}
3746
}
3847

3948
tasks.withType<DokkaTask> {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
@file:Suppress("MagicNumber")
2+
3+
package com.javiersc.kotlin.stdlib
4+
5+
public inline fun <T> Iterable<T>.second(): T = getIndex(2)
6+
7+
public inline fun <T> Iterable<T>.third(): T = getIndex(3)
8+
9+
public inline fun <T> Iterable<T>.forth(): T = getIndex(4)
10+
11+
public inline fun <T> Iterable<T>.fifth(): T = getIndex(5)
12+
13+
public inline fun <T> Iterable<T>.penultimate(): T {
14+
return when (this) {
15+
is List -> this[size - 2]
16+
else -> {
17+
val iterator = iterator()
18+
if (!iterator.hasNext()) throw NoSuchElementException("Collection is empty.")
19+
iterator.next()
20+
if (!iterator.hasNext()) {
21+
throw NoSuchElementException("Collection size is lower than 2.")
22+
}
23+
var penultimate: T = iterator.next()
24+
while (iterator.hasNext()) {
25+
val next: T = iterator.next()
26+
if (iterator.hasNext()) penultimate = next
27+
}
28+
return penultimate
29+
}
30+
}
31+
}
32+
33+
public inline fun <T> Iterable<T>.getIndex(index: Int): T {
34+
return when (this) {
35+
is List -> this[index - 1]
36+
else -> {
37+
val iterator = iterator()
38+
if (!iterator.hasNext()) throwNoSuchElementException(index)
39+
var value: T = iterator.next()
40+
41+
for (i in 0 until index - 1) {
42+
if (!iterator.hasNext()) throwNoSuchElementException(index)
43+
else value = iterator.next()
44+
}
45+
value
46+
}
47+
}
48+
}
49+
50+
@PublishedApi
51+
internal fun throwNoSuchElementException(size: Int): Unit =
52+
throw NoSuchElementException("Collection size is lower than $size.")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@file:Suppress("MagicNumber")
2+
3+
import com.javiersc.kotlin.stdlib.second
4+
import kotlin.test.Test
5+
import kotlin.test.assertTrue
6+
7+
class CollectionsTest {
8+
9+
private val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
10+
11+
@Test
12+
fun collection_second() {
13+
assertTrue { numbers.second() == 2 }
14+
}
15+
}

0 commit comments

Comments
 (0)