aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-04-07 20:23:41 -0400
committerAllan Wang <me@allanwang.ca>2018-04-07 20:23:41 -0400
commitb85fc4f939a443368cea296bfa8439745419d887 (patch)
treec9049a69127ea0bd6b43f07bcbe5822145360f2a /core/src/main/kotlin
parenta0377be622f21b4c6a7d8828505c1e95efab1254 (diff)
downloadkau-b85fc4f939a443368cea296bfa8439745419d887.tar.gz
kau-b85fc4f939a443368cea296bfa8439745419d887.tar.bz2
kau-b85fc4f939a443368cea296bfa8439745419d887.zip
Add iterator firstOrNull
Diffstat (limited to 'core/src/main/kotlin')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kotlin/Streams.kt12
1 files changed, 12 insertions, 0 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/kotlin/Streams.kt b/core/src/main/kotlin/ca/allanwang/kau/kotlin/Streams.kt
index 0da6d7a..303153f 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/kotlin/Streams.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/kotlin/Streams.kt
@@ -15,4 +15,16 @@ inline fun <T, C : MutableIterable<T>> C.kauRemoveIf(filter: (item: T) -> Boolea
while (iter.hasNext())
if (filter(iter.next())) iter.remove()
return this
+}
+
+/**
+ * Returns the first element tha matches the predicate,
+ * or null if no match is found
+ */
+inline fun <T : Any> Iterator<T>.firstOrNull(predicate: (T) -> Boolean): T? {
+ while (hasNext()) {
+ val data = next()
+ if (predicate(data)) return data
+ }
+ return null
} \ No newline at end of file