All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hp.jipp.util.Generate.kt Maven / Gradle / Ivy

// Copyright 2017 HP Development Company, L.P.
// SPDX-License-Identifier: MIT

package com.hp.jipp.util

/** Return a sequence representing all values returned by a block until it returns `null` */
fun  (() -> T?).repeatUntilNull(): Sequence {
    val block = this
    return object : Sequence {
        override fun iterator() = block.iterateUntilNull()
    }
}

/** Return an iterator that returns values generated by the block until it returns `null` */
fun  (() -> T?).iterateUntilNull(): Iterator {
    val block = this
    return object : Iterator {
        var current = block()

        override fun next(): T {
            val last = current ?: throw IllegalArgumentException("next() called on empty iterator")
            current = block()
            return last
        }

        override fun hasNext() = current != null
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy