Class Lazy


  • public class Lazy
    extends Object
    Class with lazy methods and lazy iterator and iterable implementations.
    Author:
    Herbert Prähofer
    • Constructor Detail

      • Lazy

        public Lazy()
    • Method Detail

      • iterator

        public static <T> Iterator<T> iterator​(Supplier<Optional<T>> supplier)
        Creates a lazy iterator from the given supplier function.
        Type Parameters:
        T - the type of the elements
        Parameters:
        supplier - the supplier function
        Returns:
        the lazy iterator
      • iterable

        public static <T> Iterable<T> iterable​(Supplier<Optional<T>> supplier)
        Creates a lazy iterable from the given supplier function. Uses a lazy iterator internally.
        Type Parameters:
        T - the type of the elements
        Parameters:
        supplier - the supplier function
        Returns:
        the lazy iterable object
      • find

        public static <T> Optional<T> find​(Iterator<T> iterator,
                                           Predicate<T> pred)
        Finds an element in the elements from the given iterator which satisfies the given predicate.
        Type Parameters:
        T - the type of the elements
        Parameters:
        iterator - the iterator for the elements; possibly lazy
        pred - the predicate
        Returns:
        the found element in an optional; or an empty optional if none found
      • find

        public static <T> Optional<T> find​(Iterable<T> iterable,
                                           Predicate<T> pred)
        Finds an element in the elements in the given iterable which satisfies the given predicate.
        Type Parameters:
        T - the type of the elements
        Parameters:
        iterable - the iterable with the elements; possibly lazy
        pred - the predicate
        Returns:
        the found element in an optional; or an empty optional if none found
      • generatable

        public static <T> Iterable<T> generatable​(Supplier<T> supplier)
        Creates an iterable the element of which are generated by the supplier function.
        Type Parameters:
        T - the type of the elements
        Parameters:
        supplier - the supplier function
        Returns:
        the iterable with the generated elements
      • generate

        public static <T> Iterator<T> generate​(Supplier<T> supplier)
        Creates an iterator for the elements which are generated by the supplier function.
        Type Parameters:
        T - the type of the elements
        Parameters:
        supplier - the supplier function
        Returns:
        the iterator for the generated elements
      • iterator

        public static <T> Iterator<T> iterator​(T initial,
                                               UnaryOperator<T> nextFn)
        Creates an iterator for the elements which are generated by the unary operator starting from an initial value.
        Type Parameters:
        T - the type of the elements
        Parameters:
        initial - the initial value
        nextFn - the unary function for creating a next value from the current value
        Returns:
        the iterator
      • iterate

        public static <T> Iterable<T> iterate​(T initial,
                                              UnaryOperator<T> nextFn)
        Creates an iterable with elements which are generated by the unary operator starting from an initial value.
        Type Parameters:
        T - the type of the elements
        Parameters:
        initial - the initial value
        nextFn - the unary function for creating a next value from the current value
        Returns:
        the iterable
      • words

        public static Iterable<String> words​(Reader reader)
                                      throws IOException
        Creates an iterable with words which are read in from a reader.
        Parameters:
        reader - the reader to read in the words
        Returns:
        the iterable with the words
        Throws:
        IOException - thrown if read fails
      • breadthFirstIterable

        public static <N> Iterable<N> breadthFirstIterable​(N start,
                                                           Function<N,​List<N>> successorFn)
        Creates an iterable with provides the nodes starting from the start node and using the successor function in a breadth-first manner.
        Type Parameters:
        N - the type of the nodes
        Parameters:
        start - the start node
        successorFn - the successor function
        Returns:
        the iterable with the nodes
      • breadthFirstIterator

        public static <N> Iterator<N> breadthFirstIterator​(N start,
                                                           Function<N,​List<N>> successorFn)
        Creates an iterator for the nodes starting from the start node and using the successor function in a breadth-first manner.
        Type Parameters:
        N - the type of the nodes
        Parameters:
        start - the start node
        successorFn - the successor function
        Returns:
        the iterator with the nodes