Update docstrings

This commit is contained in:
kageru 2019-10-07 23:25:23 +02:00
parent a8137c6ee4
commit 020143ffc6
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 15 additions and 3 deletions

View File

@ -4,6 +4,11 @@ package moe.kageru.sekwences
* Returns a sequences that optionally merges adjacent elements. * Returns a sequences that optionally merges adjacent elements.
* Heavily inspired by Itertools.coalesce() from the Rust crate. * Heavily inspired by Itertools.coalesce() from the Rust crate.
* *
* The merge function will be passed two adjacent elements in the sequence.
* If the two can be merged, the result of the merging process should be returned.
* If null is returned, the first of the two elements is returned,
* and the second will be used as the first element in the next merge operation.
*
* https://docs.rs/itertools/0.8.0/itertools/trait.Itertools.html#method.coalesce * https://docs.rs/itertools/0.8.0/itertools/trait.Itertools.html#method.coalesce
*/ */
fun <T> Sequence<T>.coalesce(merger: (T, T) -> T?): Sequence<T> { fun <T> Sequence<T>.coalesce(merger: (T, T) -> T?): Sequence<T> {
@ -14,14 +19,21 @@ fun <T> Sequence<T>.coalesce(merger: (T, T) -> T?): Sequence<T> {
} }
} }
class CoalescingSequence<T>(private val source: Sequence<T>, private inline val merger: (T, T) -> T?) : Sequence<T> { /**
* Sequence that holds a [CoalescingIterator].
*/
internal class CoalescingSequence<T>(private val source: Sequence<T>, private inline val merger: (T, T) -> T?) : Sequence<T> {
override fun iterator(): Iterator<T> { override fun iterator(): Iterator<T> {
return CoalescingIterator(source.iterator(), merger) return CoalescingIterator(source.iterator(), merger)
} }
} }
class CoalescingIterator<T>(private val source: Iterator<T>, private inline val merger: (T, T) -> T?) : Iterator<T> { /**
* Iterator that will attempt to merge adjacent elements as described in the KDoc for [coalesce].
*/
internal class CoalescingIterator<T>(private val source: Iterator<T>, private inline val merger: (T, T) -> T?) : Iterator<T> {
private var previous: T = if (source.hasNext()) source.next() else error("Please don’t pass empty iterators in here") private var previous: T = if (source.hasNext()) source.next() else error("Please don’t pass empty iterators in here")
// The reason we need this marker is that our iterator can still hold one value, even if the source has been drained.
private var hasNext = true private var hasNext = true
override fun hasNext() = hasNext override fun hasNext() = hasNext

View File

@ -45,4 +45,4 @@ class SekwencesTest : ShouldSpec({
}.toList() }.toList()
output shouldBe input output shouldBe input
} }
}) })