darkmoon_uk
05/21/2019, 2:11 PMfun <T,R> Iterable<T>.firstMappedOrNull( mapping: (T)->R? ) : R? {
for(element in this) {
mapping(element)?.let { return it }
}
return null
}
rook
05/21/2019, 2:13 PMfoo.map { /*some transform*/ }.find { it != null }
marstran
05/21/2019, 2:22 PMreturn asSequence()
.map(mapping)
.find { it != null }
mapping
@rookrook
05/21/2019, 2:25 PMYossi Saiada
07/16/2019, 1:04 PMfoo.mapNotNull(mapping).firstOrNull()
darkmoon_uk
07/17/2019, 1:11 PMSequence
- where sequence operators generally act like builders, deferring computation of each element until there's an operator that forces the value to resolve e.g. .toList()
.