Skip to content

Commit c033094

Browse files
committed
KT-20357: Add a sample for the collections package
1 parent 6119907 commit c033094

File tree

6 files changed

+42
-0
lines changed

6 files changed

+42
-0
lines changed

libraries/stdlib/common/src/generated/_Arrays.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,8 @@ public inline fun CharArray.firstOrNull(predicate: (Char) -> Boolean): Char? {
14671467

14681468
/**
14691469
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array.
1470+
*
1471+
* @sample samples.collections.Collections.Elements.getOrElse
14701472
*/
14711473
@kotlin.internal.InlineOnly
14721474
public inline fun <T> Array<out T>.getOrElse(index: Int, defaultValue: (Int) -> T): T {
@@ -1478,6 +1480,8 @@ public inline fun <T> Array<out T>.getOrElse(index: Int, defaultValue: (Int) ->
14781480

14791481
/**
14801482
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array.
1483+
*
1484+
* @sample samples.collections.Collections.Elements.getOrElse
14811485
*/
14821486
@kotlin.internal.InlineOnly
14831487
public inline fun ByteArray.getOrElse(index: Int, defaultValue: (Int) -> Byte): Byte {
@@ -1489,6 +1493,8 @@ public inline fun ByteArray.getOrElse(index: Int, defaultValue: (Int) -> Byte):
14891493

14901494
/**
14911495
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array.
1496+
*
1497+
* @sample samples.collections.Collections.Elements.getOrElse
14921498
*/
14931499
@kotlin.internal.InlineOnly
14941500
public inline fun ShortArray.getOrElse(index: Int, defaultValue: (Int) -> Short): Short {
@@ -1500,6 +1506,8 @@ public inline fun ShortArray.getOrElse(index: Int, defaultValue: (Int) -> Short)
15001506

15011507
/**
15021508
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array.
1509+
*
1510+
* @sample samples.collections.Collections.Elements.getOrElse
15031511
*/
15041512
@kotlin.internal.InlineOnly
15051513
public inline fun IntArray.getOrElse(index: Int, defaultValue: (Int) -> Int): Int {
@@ -1511,6 +1519,8 @@ public inline fun IntArray.getOrElse(index: Int, defaultValue: (Int) -> Int): In
15111519

15121520
/**
15131521
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array.
1522+
*
1523+
* @sample samples.collections.Collections.Elements.getOrElse
15141524
*/
15151525
@kotlin.internal.InlineOnly
15161526
public inline fun LongArray.getOrElse(index: Int, defaultValue: (Int) -> Long): Long {
@@ -1522,6 +1532,8 @@ public inline fun LongArray.getOrElse(index: Int, defaultValue: (Int) -> Long):
15221532

15231533
/**
15241534
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array.
1535+
*
1536+
* @sample samples.collections.Collections.Elements.getOrElse
15251537
*/
15261538
@kotlin.internal.InlineOnly
15271539
public inline fun FloatArray.getOrElse(index: Int, defaultValue: (Int) -> Float): Float {
@@ -1533,6 +1545,8 @@ public inline fun FloatArray.getOrElse(index: Int, defaultValue: (Int) -> Float)
15331545

15341546
/**
15351547
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array.
1548+
*
1549+
* @sample samples.collections.Collections.Elements.getOrElse
15361550
*/
15371551
@kotlin.internal.InlineOnly
15381552
public inline fun DoubleArray.getOrElse(index: Int, defaultValue: (Int) -> Double): Double {
@@ -1544,6 +1558,8 @@ public inline fun DoubleArray.getOrElse(index: Int, defaultValue: (Int) -> Doubl
15441558

15451559
/**
15461560
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array.
1561+
*
1562+
* @sample samples.collections.Collections.Elements.getOrElse
15471563
*/
15481564
@kotlin.internal.InlineOnly
15491565
public inline fun BooleanArray.getOrElse(index: Int, defaultValue: (Int) -> Boolean): Boolean {
@@ -1555,6 +1571,8 @@ public inline fun BooleanArray.getOrElse(index: Int, defaultValue: (Int) -> Bool
15551571

15561572
/**
15571573
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array.
1574+
*
1575+
* @sample samples.collections.Collections.Elements.getOrElse
15581576
*/
15591577
@kotlin.internal.InlineOnly
15601578
public inline fun CharArray.getOrElse(index: Int, defaultValue: (Int) -> Char): Char {

libraries/stdlib/common/src/generated/_Collections.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ public inline fun <T> Iterable<T>.firstOrNull(predicate: (T) -> Boolean): T? {
298298

299299
/**
300300
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this list.
301+
*
302+
* @sample samples.collections.Collections.Elements.getOrElse
301303
*/
302304
@kotlin.internal.InlineOnly
303305
public inline fun <T> List<T>.getOrElse(index: Int, defaultValue: (Int) -> T): T {

libraries/stdlib/common/src/generated/_Strings.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ public inline fun CharSequence.firstOrNull(predicate: (Char) -> Boolean): Char?
133133

134134
/**
135135
* Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this char sequence.
136+
*
137+
* @sample samples.collections.Collections.Elements.getOrElse
136138
*/
137139
@kotlin.internal.InlineOnly
138140
public inline fun CharSequence.getOrElse(index: Int, defaultValue: (Int) -> Char): Char {

libraries/stdlib/common/src/generated/_UArrays.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,8 @@ public inline fun UShortArray.firstOrNull(predicate: (UShort) -> Boolean): UShor
737737

738738
/**
739739
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array.
740+
*
741+
* @sample samples.collections.Collections.Elements.getOrElse
740742
*/
741743
@SinceKotlin("1.3")
742744
@ExperimentalUnsignedTypes
@@ -750,6 +752,8 @@ public inline fun UIntArray.getOrElse(index: Int, defaultValue: (Int) -> UInt):
750752

751753
/**
752754
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array.
755+
*
756+
* @sample samples.collections.Collections.Elements.getOrElse
753757
*/
754758
@SinceKotlin("1.3")
755759
@ExperimentalUnsignedTypes
@@ -763,6 +767,8 @@ public inline fun ULongArray.getOrElse(index: Int, defaultValue: (Int) -> ULong)
763767

764768
/**
765769
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array.
770+
*
771+
* @sample samples.collections.Collections.Elements.getOrElse
766772
*/
767773
@SinceKotlin("1.3")
768774
@ExperimentalUnsignedTypes
@@ -776,6 +782,8 @@ public inline fun UByteArray.getOrElse(index: Int, defaultValue: (Int) -> UByte)
776782

777783
/**
778784
* Returns an element at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this array.
785+
*
786+
* @sample samples.collections.Collections.Elements.getOrElse
779787
*/
780788
@SinceKotlin("1.3")
781789
@ExperimentalUnsignedTypes

libraries/stdlib/samples/test/samples/collections/collections.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,17 @@ class Collections {
12331233
assertPrints(lastEven, "6")
12341234
}
12351235

1236+
@Sample
1237+
fun getOrElse() {
1238+
val list = listOf(1, 2, 3)
1239+
assertPrints(list.getOrElse(0) { 42 }, "1")
1240+
assertPrints(list.getOrElse(2) { 42 }, "3")
1241+
assertPrints(list.getOrElse(3) { 42 }, "42")
1242+
1243+
val emptyList = emptyList<Int>()
1244+
assertPrints(emptyList.elementAtOrElse(0) { "no int" }, "no int")
1245+
}
1246+
12361247
@Sample
12371248
fun getOrNull() {
12381249
val list = listOf(1, 2, 3)

libraries/tools/kotlin-stdlib-gen/src/templates/Elements.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ object Elements : TemplateGroupBase() {
402402
include(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned)
403403
} builder {
404404
doc { "Returns ${f.element.prefixWithArticle()} at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this ${f.collection}." }
405+
sample("samples.collections.Collections.Elements.getOrElse")
405406
returns("T")
406407
inlineOnly()
407408
val indices = if (family == Lists) "0..<size" else "indices"

0 commit comments

Comments
 (0)