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

no.motif.iter.SuccessorIterable Maven / Gradle / Ivy

The newest version!
package no.motif.iter;

import static no.motif.Singular.optional;

import java.io.Serializable;
import java.util.Iterator;

import no.motif.f.Fn;
import no.motif.single.Optional;

/**
 * Iterable where its elements are computed from an initial element.
 *
 * @param  The type of object this iterable yields.
 */
final class SuccessorIterable implements Iterable, Serializable {

    private final Optional firstElement;
    private final Fn successor;

    SuccessorIterable(F firstElement, Fn successor) {
        this.firstElement = optional(firstElement);
        this.successor = successor;
    }

    @Override
    public Iterator iterator() {
        return new SimpleIterator() {
            Optional next = firstElement;
            @Override
            protected Optional nextIfAvailable() {
                Optional toReturn = next;
                next = next.map(successor);
                return toReturn;
            }};
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy