reduce() without identity
This commit is contained in:
parent
817a2f8459
commit
373b7ce682
@ -4,7 +4,7 @@ kotlin.code.style=official
|
|||||||
specifyKotlinAsDependency=false
|
specifyKotlinAsDependency=false
|
||||||
|
|
||||||
projectGroup=ru.dbotthepony.kommons
|
projectGroup=ru.dbotthepony.kommons
|
||||||
projectVersion=2.18.0
|
projectVersion=2.18.1
|
||||||
|
|
||||||
guavaDepVersion=33.0.0
|
guavaDepVersion=33.0.0
|
||||||
gsonDepVersion=2.8.9
|
gsonDepVersion=2.8.9
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package ru.dbotthepony.kommons.collect
|
package ru.dbotthepony.kommons.collect
|
||||||
|
|
||||||
|
import ru.dbotthepony.kommons.util.KOptional
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.function.Predicate
|
import java.util.function.Predicate
|
||||||
import java.util.stream.Collector
|
import java.util.stream.Collector
|
||||||
@ -218,6 +219,15 @@ inline fun <T> Iterator<T>.reduce(identity: T, reducer: (T, T) -> T): T {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline fun <T> Iterator<T>.reduce(reducer: (T, T) -> T): KOptional<T> {
|
||||||
|
if (!hasNext())
|
||||||
|
return KOptional()
|
||||||
|
|
||||||
|
var result = next()
|
||||||
|
while (hasNext()) result = reducer.invoke(result, next())
|
||||||
|
return KOptional(result)
|
||||||
|
}
|
||||||
|
|
||||||
fun <T> Iterator<T?>.filterNotNull(): MutableIterator<T> = filter { it != null } as MutableIterator<T>
|
fun <T> Iterator<T?>.filterNotNull(): MutableIterator<T> = filter { it != null } as MutableIterator<T>
|
||||||
|
|
||||||
inline fun <reified T> Iterator<*>.filterIsInstance(): MutableIterator<T> = filter { it is T } as MutableIterator<T>
|
inline fun <reified T> Iterator<*>.filterIsInstance(): MutableIterator<T> = filter { it is T } as MutableIterator<T>
|
||||||
|
Loading…
Reference in New Issue
Block a user