Interface InfList<T>
- Type Parameters:
T- the type of the infinite list elements
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 TypeMethodDescriptionbooleanReturns whether all elements of this stream match the provided predicate.booleanReturns whether any elements of this stream match the provided predicate.Creates a lazily concatenated infinite list whose elements are all the elements of this list followed by all the elements of the other list.Returns an infinite list consisting of the elements of this list that match the given predicate.Returns anMaybedescribing the first element of this infinite list, or an emptyMaybeif the list is empty.<R> InfList<R> 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.voidPerforms an action for each element of this infinite list.static <T> InfList<T> iterate(T seed, UnaryOperator<T> next) Returns an infinite sequential orderedInfListproduced by iterative application of a functionnextto an initial elementseed, producing aInfListconsisting ofseed,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 thanmaxSizein length.<R> InfList<R> Returns an infinite list consisting of the results of applying the given function to the elements of this list.booleanReturns 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 aMaybedescribing the reduced value, if any.<U> Ureduce(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.Returns an infinite list consisting of the longest prefix of elements taken from this list that match the given predicate.
-
Method Details
-
iterate
Returns an infinite sequential orderedInfListproduced by iterative application of a functionnextto an initial elementseed, producing aInfListconsisting ofseed,next(seed),next(next(seed)), etc.The first element (position
0) in theInfListwill be the providedseed. Forn > 0, the element at positionn, will be the result of applying the functionnextto the element at positionn - 1.- Type Parameters:
T- the type of stream elements- Parameters:
seed- the initial elementnext- a function to be applied to the previous element to produce a new element- Returns:
- a new sequential
InfList
-
of
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
Returns anMaybedescribing the first element of this infinite list, or an emptyMaybeif the list is empty.- Returns:
- an
Maybedescribing the first element of this infinite list, or an emptyMaybeif the list is empty - Throws:
NullPointerException- if the element selected is null
-
concat
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
otherto the end of this infinite list
-
limit
Returns an infinite list consisting of the elements of this list, truncated to be no longer thanmaxSizein length.- Parameters:
maxSize- the number of elements the list should be limited to- Returns:
- the new list
- Throws:
IllegalArgumentException- ifmaxSizeis negative
-
forEach
Performs an action for each element of this infinite list.- Parameters:
action- an action to perform on the elements
-
map
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
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 isnullan 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
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
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
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 functionaccumulator- a binary function for combining two values- Returns:
- the result of the reduction
-
reduce
Performs a reduction on the elements of this infinite list, using a function, and returns aMaybedescribing 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
Maybedescribing the result of the reduction
-
anyMatch
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 thenfalseis 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:
trueif any elements of the stream match the provided predicate, otherwisefalse
-
allMatch
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 thentrueis 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 alwaystrue(regardless of P(x)).- Parameters:
predicate- a predicate to apply to elements of this stream- Returns:
trueif either all elements of the stream match the provided predicate or the stream is empty, otherwisefalse
-
noneMatch
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 thentrueis 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 alwaystrue, regardless of P(x).- Parameters:
predicate- a predicate to apply to elements of this stream- Returns:
trueif either no elements of the stream match the provided predicate or the stream is empty, otherwisefalse
-