Refactored (and more correct) getOutermostLastBlockElement and its usages

This commit is contained in:
Valentin Kipyatkov
2015-05-12 18:28:37 +03:00
parent 38ac420057
commit d95b198da7
10 changed files with 50 additions and 50 deletions
@@ -53,6 +53,24 @@ public inline fun <reified T> Array<*>.firstIsInstance(): T {
throw NoSuchElementException("No element of given type found")
}
public inline fun <reified T : Any> Iterable<*>.lastIsInstanceOrNull(): T? {
when (this) {
is List<*> -> {
for (i in this.indices.reversed()) {
val element = this[i]
if (element is T) return element
}
return null
}
else -> {
return reverse().firstIsInstanceOrNull<T>()
}
}
}
public fun <T> streamOfLazyValues(vararg elements: () -> T): Stream<T> = elements.stream().map { it() }
public fun <T1, T2> Pair<T1, T2>.swap(): Pair<T2, T1> = Pair(second, first)
public fun <T1, T2> Pair<T1, T2>.swap(): Pair<T2, T1> = Pair(second, first)
public fun <T: Any> T.check(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null