summaryrefslogtreecommitdiff
path: root/src/main/kotlin/util/iterutil.kt
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2025-01-29 00:26:56 +0100
committerLinnea Gräf <nea@nea.moe>2025-01-29 00:26:56 +0100
commit29857bff0875c43e055e89d34bc0d616d5d7dc4f (patch)
tree5875aed4144c0c1f004a86f6768eb5d6013d68f6 /src/main/kotlin/util/iterutil.kt
parentad03f074c603f38f9319411d785e598e8a92e42c (diff)
downloadultra-notifier-master.tar.gz
ultra-notifier-master.tar.bz2
ultra-notifier-master.zip
Load universe from jsonHEADmaster
Diffstat (limited to 'src/main/kotlin/util/iterutil.kt')
-rw-r--r--src/main/kotlin/util/iterutil.kt36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/main/kotlin/util/iterutil.kt b/src/main/kotlin/util/iterutil.kt
new file mode 100644
index 0000000..7845b05
--- /dev/null
+++ b/src/main/kotlin/util/iterutil.kt
@@ -0,0 +1,36 @@
+package moe.nea.ultranotifier.util
+
+
+fun <T, K : Any> Sequence<T>.duplicatesBy(keyFunc: (T) -> K): Sequence<T> {
+ return object : Sequence<T> {
+ override fun iterator(): Iterator<T> {
+ val observed = HashSet<K>()
+ val oldIterator = this@duplicatesBy.iterator()
+
+ return object : Iterator<T> {
+ var next: T? = null
+ var hasNext = false
+ override fun hasNext(): Boolean {
+ if (hasNext) return true
+ while (oldIterator.hasNext()) {
+ val elem = oldIterator.next()
+ val key = keyFunc(elem)
+ if (observed.add(key))
+ continue
+ hasNext = true
+ next = elem
+ }
+ return hasNext
+ }
+
+ override fun next(): T {
+ if (!hasNext()) throw NoSuchElementException()
+ hasNext = false
+ val elem = next as T
+ next = null
+ return elem
+ }
+ }
+ }
+ }
+}