Interface InfList<T>

Type Parameters:
T - the type of the infinite list elements

public interface InfList<T>
A infinite sequence of elements supporting sequential operations.

To perform a computation, infinite list operations are composed into a pipeline. A pipeline consists of a source iterator, zero or more intermediate operations (which transform a InfList into another InfList, such as filter(java.util.function.Predicate<? super T>)), and a terminal operation (which produces a result or side-effect, such as forEach(java.util.function.Consumer<? super T>)). Infinite lists are lazy; computation on the source data is only performed when the terminal operation is initiated, and source elements are consumed only as needed.

Most operations accept parameters that describe user-specified behavior, such as the lambda expression which are always instances of a functional interface such as Function. Unless other specified, these parameters must be non-null.

  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    allMatch(Predicate<? super T> predicate)
    Returns whether all elements of this stream match the provided predicate.
    boolean
    anyMatch(Predicate<? super T> predicate)
    Returns whether any elements of this stream match the provided predicate.
    concat(InfList<? extends T> other)
    Creates a lazily concatenated infinite list whose elements are all the elements of this list followed by all the elements of the other list.
    filter(Predicate<? super T> predicate)
    Returns an infinite list consisting of the elements of this list that match the given predicate.
    Returns an Maybe describing the first element of this infinite list, or an empty Maybe if the list is empty.
    <R> InfList<R>
    flatMap(Function<? super T,? extends InfList<? extends R>> mapper)
    Returns an infinite list consisting of the results of replacing each element of this list with the contents of a mapped list produced by applying the provided mapping function to each element.
    void
    forEach(Consumer<? super T> action)
    Performs an action for each element of this infinite list.
    static <T> InfList<T>
    iterate(T seed, UnaryOperator<T> next)
    Returns an infinite sequential ordered InfList produced by iterative application of a function next to an initial element seed, producing a InfList consisting of seed, next(seed), next(next(seed)), etc.
    limit(int maxSize)
    Returns an infinite list consisting of the elements of this list, truncated to be no longer than maxSize in length.
    <R> InfList<R>
    map(Function<? super T,? extends R> mapper)
    Returns an infinite list consisting of the results of applying the given function to the elements of this list.
    boolean
    noneMatch(Predicate<? super T> predicate)
    Returns whether no elements of this stream match the provided predicate.
    static <T> InfList<T>
    of(T... elems)
    Returns a sequential ordered stream whose elements are the specified values.
    reduce(BinaryOperator<T> accumulator)
    Performs a reduction on the elements of this infinite list, using a function, and returns a Maybe describing the reduced value, if any.
    <U> U
    reduce(U identity, BiFunction<U,T,U> accumulator)
    Performs a reduction on the elements of this infinite list, using the provided identity value and an accumulation function, and returns the reduced value.
    takeWhile(Predicate<? super T> predicate)
    Returns an infinite list consisting of the longest prefix of elements taken from this list that match the given predicate.
  • Method Details

    • iterate

      static <T> InfList<T> iterate(T seed, UnaryOperator<T> next)
      Returns an infinite sequential ordered InfList produced by iterative application of a function next to an initial element seed, producing a InfList consisting of seed, next(seed), next(next(seed)), etc.

      The first element (position 0) in the InfList will be the provided seed. For n > 0, the element at position n, will be the result of applying the function next to the element at position n - 1.

      Type Parameters:
      T - the type of stream elements
      Parameters:
      seed - the initial element
      next - a function to be applied to the previous element to produce a new element
      Returns:
      a new sequential InfList
    • of

      @SafeVarargs static <T> InfList<T> of(T... elems)
      Returns a sequential ordered stream whose elements are the specified values.
      Type Parameters:
      T - the type of stream elements
      Parameters:
      elems - the elements of the new stream
      Returns:
      the new stream
    • findFirst

      Maybe<T> findFirst()
      Returns an Maybe describing the first element of this infinite list, or an empty Maybe if the list is empty.
      Returns:
      an Maybe describing the first element of this infinite list, or an empty Maybe if the list is empty
      Throws:
      NullPointerException - if the element selected is null
    • concat

      InfList<T> concat(InfList<? extends T> other)
      Creates a lazily concatenated infinite list whose elements are all the elements of this list followed by all the elements of the other list.
      Parameters:
      other - the other infinite list
      Returns:
      result of concatenating other to the end of this infinite list
    • limit

      InfList<T> limit(int maxSize)
      Returns an infinite list consisting of the elements of this list, truncated to be no longer than maxSize in length.
      Parameters:
      maxSize - the number of elements the list should be limited to
      Returns:
      the new list
      Throws:
      IllegalArgumentException - if maxSize is negative
    • forEach

      void forEach(Consumer<? super T> action)
      Performs an action for each element of this infinite list.
      Parameters:
      action - an action to perform on the elements
    • map

      <R> InfList<R> map(Function<? super T,? extends R> mapper)
      Returns an infinite list consisting of the results of applying the given function to the elements of this list.
      Type Parameters:
      R - The element type of the new infinite list
      Parameters:
      mapper - a function to apply to each element
      Returns:
      the new infinite list
    • flatMap

      <R> InfList<R> flatMap(Function<? super T,? extends InfList<? extends R>> mapper)
      Returns an infinite list consisting of the results of replacing each element of this list with the contents of a mapped list produced by applying the provided mapping function to each element. If a mapped list is null an empty list is used, instead.)
      Type Parameters:
      R - The element type of the new infinitelist
      Parameters:
      mapper - a function to apply to each element which produces a list of new values
      Returns:
      the new infinite list
    • filter

      InfList<T> filter(Predicate<? super T> predicate)
      Returns an infinite list consisting of the elements of this list that match the given predicate.
      Parameters:
      predicate - a predicate to apply to each element to determine if it should be included
      Returns:
      the new infinite list
    • takeWhile

      InfList<T> takeWhile(Predicate<? super T> predicate)
      Returns an infinite list consisting of the longest prefix of elements taken from this list that match the given predicate.
      Parameters:
      predicate - a predicate to apply to elements to determine the longest prefix of elements.
      Returns:
      the new infinite list
    • reduce

      <U> U reduce(U identity, BiFunction<U,T,U> accumulator)
      Performs a reduction on the elements of this infinite list, using the provided identity value and an accumulation function, and returns the reduced value. This is equivalent to:
      
           T result = identity;
           for (T element : this stream)
               result = accumulator.apply(result, element)
           return result;
       
      Type Parameters:
      U - The element type of the new infinite list
      Parameters:
      identity - the identity value for the accumulating function
      accumulator - a binary function for combining two values
      Returns:
      the result of the reduction
    • reduce

      Maybe<T> reduce(BinaryOperator<T> accumulator)
      Performs a reduction on the elements of this infinite list, using a function, and returns a Maybe describing the reduced value, if any. This is equivalent to:
      
           boolean foundAny = false;
           T result = null;
           for (T element : this stream) {
               if (!foundAny) {
                   foundAny = true;
                   result = element;
               }
               else
                   result = accumulator.apply(result, element);
           }
           return foundAny ? Maybe.of(result) : Maybe.empty();
       
      Parameters:
      accumulator - a function for combining two values
      Returns:
      a Maybe describing the result of the reduction
    • anyMatch

      boolean anyMatch(Predicate<? super T> predicate)
      Returns whether any elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then false is returned and the predicate is not evaluated. This method evaluates the existential quantification of the predicate over the elements of the stream (for some x P(x)).
      Parameters:
      predicate - predicate to apply to elements of this stream
      Returns:
      true if any elements of the stream match the provided predicate, otherwise false
    • allMatch

      boolean allMatch(Predicate<? super T> predicate)
      Returns whether all elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then true is returned and the predicate is not evaluated. This method evaluates the universal quantification of the predicate over the elements of the stream (for all x P(x)). If the stream is empty, the quantification is said to be vacuously satisfied and is always true (regardless of P(x)).
      Parameters:
      predicate - a predicate to apply to elements of this stream
      Returns:
      true if either all elements of the stream match the provided predicate or the stream is empty, otherwise false
    • noneMatch

      boolean noneMatch(Predicate<? super T> predicate)
      Returns whether no elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then true is returned and the predicate is not evaluated. This method evaluates the universal quantification of the negated predicate over the elements of the stream (for all x ~P(x)). If the stream is empty, the quantification is said to be vacuously satisfied and is always true, regardless of P(x).
      Parameters:
      predicate - a predicate to apply to elements of this stream
      Returns:
      true if either no elements of the stream match the provided predicate or the stream is empty, otherwise false