Skip to content

Commit 8c54e6e

Browse files
committed
update: fix singleornull content in intermediate tour
1 parent 9a7633d commit 8c54e6e

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

docs/topics/tour/kotlin-tour-intermediate-null-safety.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ In both of these examples, if all items are `null` values, an empty list is retu
191191
Kotlin also provides functions that you can use to find values in collections. If a value isn't found, they return `null`
192192
values instead of triggering an error:
193193

194-
* [`singleOrNull()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/single-or-null.html) looks for only one item by its exact value. If one doesn't exist or there are multiple items with the same value, returns a `null` value.
195194
* [`maxOrNull()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/max-or-null.html) finds the highest value. If one doesn't exist, returns a `null` value.
196195
* [`minOrNull()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/min-or-null.html) finds the lowest value. If one doesn't exist, returns a `null` value.
197196

@@ -202,12 +201,7 @@ fun main() {
202201
//sampleStart
203202
// Temperatures recorded over a week
204203
val temperatures = listOf(15, 18, 21, 21, 19, 17, 16)
205-
206-
// Check if there was exactly one day with 30 degrees
207-
val singleHotDay = temperatures.singleOrNull()
208-
println("Single hot day with 30 degrees: ${singleHotDay ?: "None"}")
209-
// Single hot day with 30 degrees: None
210-
204+
211205
// Find the highest temperature of the week
212206
val maxTemperature = temperatures.maxOrNull()
213207
println("Highest temperature recorded: ${maxTemperature ?: "No data"}")
@@ -224,16 +218,38 @@ fun main() {
224218

225219
This example uses the Elvis operator `?:` to return a printed statement if the functions return a `null` value.
226220

227-
> The `singleOrNull()`, `maxOrNull()`, and `minOrNull()` functions are designed to be used with collections that **don't**
221+
> The `maxOrNull()`, and `minOrNull()` functions are designed to be used with collections that **don't**
228222
> contain `null` values. Otherwise, you can't tell whether the function couldn't find the desired value or whether it
229223
> found a `null` value.
230224
>
231225
{style="note"}
232226

233-
Some functions use a lambda expression to transform a collection and return `null` values if they can't
227+
You can use the [`singleOrNull()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/single-or-null.html) function with a lambda expression to find a single item that matches a condition.
228+
If one doesn't exist or there are multiple items that match, the function returns a `null` value:
229+
230+
```kotlin
231+
fun main() {
232+
//sampleStart
233+
// Temperatures recorded over a week
234+
val temperatures = listOf(15, 18, 21, 21, 19, 17, 16)
235+
236+
// Check if there was exactly one day with 30 degrees
237+
val singleHotDay = temperatures.singleOrNull{ it == 30 }
238+
println("Single hot day with 30 degrees: ${singleHotDay ?: "None"}")
239+
// Single hot day with 30 degrees: None
240+
//sampleEnd
241+
}
242+
```
243+
{kotlin-runnable="true" id="kotlin-tour-null-safety-singleornull"}
244+
245+
> The `singleOrNull()` function is designed to be used with collections that **don't** contain `null` values.
246+
>
247+
{style="note"}
248+
249+
Some functions use a lambda expression to transform a collection and return `null` values if they can't
234250
fulfill their purpose.
235251

236-
For example, to transform a collection with a lambda expression and return the first value that isn't `null`, use the
252+
To transform a collection with a lambda expression and return the first value that isn't `null`, use the
237253
[`firstNotNullOfOrNull()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/first-not-null-of-or-null.html) function. If no such value exists, the function returns a `null` value:
238254

239255
```kotlin
@@ -255,7 +271,7 @@ fun main() {
255271
```
256272
{kotlin-runnable="true" id="kotlin-tour-null-safety-firstnotnullofornull"}
257273

258-
To use a lambda function to process each collection item sequentially and create an accumulated value (or return a
274+
To use a lambda expression to process each collection item sequentially and create an accumulated value (or return a
259275
`null` value if the collection is empty) use the [`reduceOrNull()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/reduce-or-null.html) function:
260276

261277
```kotlin

0 commit comments

Comments
 (0)