Package io.atlassian.fugue
Class Iterables
- java.lang.Object
-
- io.atlassian.fugue.Iterables
-
public class Iterables extends java.lang.ObjectContains static utility methods that operate on or return objects of type {code}Iterable{code}. The iterables produced from the functions in this class are safe to reuse multiple times. Iterables#iterator returns a new iterator each time.- Since:
- 1.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classIterables.CollectingIterable<A,B>CollectingIterable, filters and transforms in one.(package private) static classIterables.Cycle<A>(package private) static classIterables.Drop<A>Iterable that only shows a small range of the original Iterable.(package private) static classIterables.Filter<A>(package private) static classIterables.Intersperse<A>(package private) static classIterables.IterableToString<A>(package private) static classIterables.IteratingIterable<A>Infinite iterable that repeatedly appliesftostart(package private) static classIterables.Join<A>(package private) static classIterables.LazyReference<T>Class supports the implementation ofmemoize(Iterable)and is not intended for general use.(package private) static classIterables.Mapped<A,B>(package private) static classIterables.Memoizer<A>Memoizing iterable, maintains a lazily computed linked list of nodes.(package private) static classIterables.MergeSortedIterable<A>Merges multiple sorted Iterables into one, sorted iterable.(package private) static classIterables.Take<A>Iterable that only shows a small range of the original Iterable.(package private) static classIterables.UnfoldingIterable<A,B>Iterable that repeatedly appliesfuntil it returnsnone()(package private) static classIterables.Zipper<A,B,C>Iterable that combines two iterables using a combiner function.
-
Field Summary
Fields Modifier and Type Field Description (package private) static java.lang.Iterable<?>EMPTY
-
Constructor Summary
Constructors Modifier Constructor Description privateIterables()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <A> booleanaddAll(java.util.Collection<A> collectionToModify, java.lang.Iterable<? extends A> elementsToAdd)Add all the elements of the iterable to the collectionstatic <A> booleanall(java.lang.Iterable<? extends A> as, java.util.function.Predicate<? super A> p)Check if all elements in the input iterable match the input predicatestatic <A> booleanany(java.lang.Iterable<? extends A> as, java.util.function.Predicate<? super A> p)Check if the iterable contains any elements that match the predicate.static <A,B>
java.lang.Iterable<B>ap(java.lang.Iterable<A> as, java.lang.Iterable<java.util.function.Function<A,B>> fs)Performs function application within an iterable (applicative functor pattern).static <A,B>
java.lang.Iterable<B>collect(java.lang.Iterable<? extends A> from, java.util.function.Function<? super A,Option<B>> partial)Filters and maps (aka transforms) the unfiltered iterable.static <T,A,R>
Rcollect(java.lang.Iterable<T> elements, java.util.stream.Collector<T,A,R> collector)Uses aCollectorto collect/reduce anIterable.static <A> java.lang.Iterable<A>concat(java.lang.Iterable<? extends A>... as)Concatenate a series of iterables into a single iterable.static <A> intcount(java.lang.Iterable<A> as, java.util.function.Predicate<? super A> p)Return the size of the elements inside an iterable that meets a specified predicate.static <A> java.lang.Iterable<A>cycle(A... as)Return an infinite iterable that cycles through the input values in order in a loop.static <A> java.lang.Iterable<A>cycle(java.lang.Iterable<? extends A> as)Return an infinite iterable that cycles through the input values in order in a loop.static <A> java.lang.Iterable<A>drop(int n, java.lang.Iterable<A> as)Drop the firstnasand return the rest.static <A> java.lang.Iterable<A>dropWhile(java.lang.Iterable<A> as, java.util.function.Predicate<A> p)Drop elements ofasuntil an element returns false for p#teststatic <T> java.lang.Iterable<T>emptyIterable()Returns an empty iterable, that is, anIterablewith anIteratorfor whichhasNext()always returnsfalse, and the other methods throw appropriate exceptions if called.static <A> java.lang.Iterable<A>filter(java.lang.Iterable<A> as, java.util.function.Predicate<? super A> p)Remove elements from the input iterable for which the predicate returns falsestatic <T> Option<T>findFirst(java.lang.Iterable<? extends T> elements, java.util.function.Predicate<? super T> p)Finds the first item that matches the predicate.static <A> java.util.function.Function<java.lang.Iterable<A>,Option<A>>findFirst(java.util.function.Predicate<? super A> predicate)Partial application of the predicate argument tofindFirst(Iterable, Predicate)returning a function that takes anIterableas its argumentstatic <A> Option<A>first(java.lang.Iterable<A> as)Ifasis empty, returnsnone().static <A,B>
java.lang.Iterable<B>flatMap(java.lang.Iterable<A> collection, java.util.function.Function<? super A,? extends java.lang.Iterable<? extends B>> f)Appliesfto each element ofcollection, then concatenates the result.static <T,R>
RfoldLeft(java.lang.Iterable<T> elements, java.util.function.BiFunction<? super R,? super T,? extends R> foldFunction, R initial)Folds theIterableelements into the value of potentially another type.static <A> java.lang.Iterable<A>intersperse(java.lang.Iterable<? extends A> as, A a)Intersperse an element between all the elements in an iterable.static <A> java.lang.Iterable<A>intersperse(java.lang.Iterable<? extends A> as, java.util.function.Supplier<A> a)Intersperse an element between all the elements in an iterable.static java.util.function.Predicate<java.lang.Iterable<?>>isEmpty()Predicate that checks if an iterable is empty.static <A> java.lang.Iterable<A>iterable(A... as)Creates an Iterable from the underlying array of elements.static <A> java.lang.Iterable<A>iterate(java.util.function.Function<? super A,? extends A> f, A start)Returns an infinite Iterable constructed by applying the given iteration function starting at the given value.static <A> java.lang.Iterable<A>join(java.lang.Iterable<? extends java.lang.Iterable<? extends A>> ias)Join Iterable<Iterable<A>> down to Iterable<A>.static <A> java.lang.StringmakeString(java.lang.Iterable<? extends A> as, java.lang.String start, java.lang.String sep, java.lang.String end)Pretty print an Iterable.static <A> java.lang.StringmakeString(java.lang.Iterable<? extends A> as, java.lang.String start, java.lang.String sep, java.lang.String end, int maxLength)Pretty print an Iterable.static <A,B>
java.lang.Iterable<B>map(java.lang.Iterable<A> as, java.util.function.Function<? super A,? extends B> f)Apply the input function to each of the elements of the input iterable returning a new iterablestatic <A> java.lang.Iterable<A>memoize(java.lang.Iterable<A> xs)Makes a lazy copy ofxs.static <A extends java.lang.Comparable<A>>
java.lang.Iterable<A>mergeSorted(java.lang.Iterable<? extends java.lang.Iterable<A>> xss)Merge a number of already sorted collections of elements into a single collection of elements, using the elements natural ordering.static <A> java.lang.Iterable<A>mergeSorted(java.lang.Iterable<? extends java.lang.Iterable<A>> xss, java.util.Comparator<A> ordering)Merge a number of already sorted collections of elements into a single collection of elements.static <A> Pair<java.lang.Iterable<A>,java.lang.Iterable<A>>partition(java.lang.Iterable<A> iterable, java.util.function.Predicate<? super A> p)Filter anIterableinto aPairofIterable's.static java.lang.Iterable<java.lang.Integer>rangeTo(int start, int end)Creates a sequence ofintegersfrom start up to and including end.static java.lang.Iterable<java.lang.Integer>rangeTo(int start, int end, int step)Creates a sequence ofintegersfrom start up to and including end with the the supplied step between them.static java.lang.Iterable<java.lang.Integer>rangeUntil(int start, int end)Creates a sequence ofintegersfrom start up to but not including end.static java.lang.Iterable<java.lang.Integer>rangeUntil(int start, int end, int step)Creates a sequence ofintegersfrom start up to but not including end with the the supplied step between them.static <A,B>
java.lang.Iterable<B>revMap(java.lang.Iterable<? extends java.util.function.Function<A,B>> fs, A arg)Applies each function infstoarg.static <A> intsize(java.lang.Iterable<A> as)Return the size of an iterable.static <A> java.lang.Iterable<A>take(int n, java.lang.Iterable<A> as)Aakes the firstnasand returns them.static <A> java.lang.Iterable<A>takeWhile(java.lang.Iterable<A> as, java.util.function.Predicate<A> p)Return a new iterable containing only the first elements ofasfor whichp#testreturns true.static <A,B>
java.lang.Iterable<B>transform(java.lang.Iterable<A> as, java.util.function.Function<? super A,? extends B> f)Deprecated.function provided to make migration easier prefer to use #map where possiblestatic <A,B>
java.lang.Iterable<A>unfold(java.util.function.Function<? super B,Option<Pair<A,B>>> f, B seed)Builds an Iterable from a seed value untilfreturnsnone().static <A,B>
Pair<java.lang.Iterable<A>,java.lang.Iterable<B>>unzip(java.lang.Iterable<Pair<A,B>> pairs)static <A,B>
java.lang.Iterable<Pair<A,B>>zip(java.lang.Iterable<A> as, java.lang.Iterable<B> bs)Zips two iterables into a single iterable that producespairs.static <A,B,C>
java.util.function.BiFunction<java.lang.Iterable<A>,java.lang.Iterable<B>,java.lang.Iterable<C>>zipWith(java.util.function.BiFunction<A,B,C> f)Takes a two-arg function that returns a third type and reurn a new function that takes iterables of the two input types and combines them into a new iterable.static <A> java.lang.Iterable<Pair<A,java.lang.Integer>>zipWithIndex(java.lang.Iterable<A> as)Takes an Iterable, and returns an Iterable of a Pair of the original element and its index starting at zero.
-
-
-
Method Detail
-
emptyIterable
public static <T> java.lang.Iterable<T> emptyIterable()
Returns an empty iterable, that is, anIterablewith anIteratorfor whichhasNext()always returnsfalse, and the other methods throw appropriate exceptions if called. Intended to be used as a more idiomatic replacement forCollections.emptyList()in code that otherwise deals only with iterables.- Type Parameters:
T- the type- Returns:
- an empty iterable
- Since:
- 1.2
-
iterable
@SafeVarargs public static <A> java.lang.Iterable<A> iterable(A... as)
Creates an Iterable from the underlying array of elements.- Type Parameters:
A- The type of the elements- Parameters:
as- the elements to iterate over- Returns:
- an Iterable across the underlying elements
- Since:
- 2.3
-
findFirst
public static <T> Option<T> findFirst(java.lang.Iterable<? extends T> elements, java.util.function.Predicate<? super T> p)
Finds the first item that matches the predicate. Traditionally, this should be named find; in this case it is named findFirst to avoid clashing with static imports from Guava's com.google.common.collect.Iterables.- Type Parameters:
T- the type- Parameters:
elements- the iterable to search for a matching elementp- the predicate to use to determine if an element is eligible to be returned- Returns:
- the first item in elements that matches predicate
- Since:
- 1.0
-
findFirst
public static <A> java.util.function.Function<java.lang.Iterable<A>,Option<A>> findFirst(java.util.function.Predicate<? super A> predicate)
Partial application of the predicate argument tofindFirst(Iterable, Predicate)returning a function that takes anIterableas its argument- Type Parameters:
A- the type- Parameters:
predicate- the predicate to use to determine if an element is eligible to be returned- Returns:
- a Function that takes an
Iterableas its argument, and returns the first element that satisfies the predicate - Since:
- 2.2
-
first
public static <A> Option<A> first(java.lang.Iterable<A> as)
Ifasis empty, returnsnone(). Otherwise, returnssome(get(as, 0)).- Type Parameters:
A- type of elements inas- Parameters:
as- elements to get the first value of, must not be null- Returns:
none()ifasis empty.some(get(as, 0))otherwise- Since:
- 1.1
-
ap
public static <A,B> java.lang.Iterable<B> ap(java.lang.Iterable<A> as, java.lang.Iterable<java.util.function.Function<A,B>> fs)Performs function application within an iterable (applicative functor pattern).- Parameters:
as- an iterablefs- The iterable of functions to apply.- Returns:
- A new iterable after applying the given stream of functions through as.
-
flatMap
public static <A,B> java.lang.Iterable<B> flatMap(java.lang.Iterable<A> collection, java.util.function.Function<? super A,? extends java.lang.Iterable<? extends B>> f)Appliesfto each element ofcollection, then concatenates the result.- Type Parameters:
A- type of elements incollectionB- type elements in the newIterablefwill transform elements to- Parameters:
collection- elements to applyftof-Functionto apply to elements ofcollection- Returns:
- concatenated result of applying
fto each element ofcollection - Since:
- 1.1
-
revMap
public static <A,B> java.lang.Iterable<B> revMap(java.lang.Iterable<? extends java.util.function.Function<A,B>> fs, A arg)Applies each function infstoarg.- Type Parameters:
A- the argument typeB- the function output and type of the elements of the final iterable.- Parameters:
fs- an iterable of functions that the arg will be applied toarg- the argument to apply to the functions- Returns:
- the results of the functions when applied to the arg
- Since:
- 1.1
-
isEmpty
public static java.util.function.Predicate<java.lang.Iterable<?>> isEmpty()
Predicate that checks if an iterable is empty.- Returns:
Predicatewhich checks if anIterableis empty- Since:
- 1.1
-
collect
public static <A,B> java.lang.Iterable<B> collect(java.lang.Iterable<? extends A> from, java.util.function.Function<? super A,Option<B>> partial)Filters and maps (aka transforms) the unfiltered iterable. Applies the given partial function to each element of the unfiltered iterable. If the application returns none, the element will be left out; otherwise, the transformed object contained in the Option will be added to the result.- Type Parameters:
A- the input typeB- the output type- Parameters:
from- the input iterable, must not be null and must not contain nullpartial- the collecting function- Returns:
- the collected iterable
- Since:
- 2.2
-
collect
public static <T,A,R> R collect(java.lang.Iterable<T> elements, java.util.stream.Collector<T,A,R> collector)Uses aCollectorto collect/reduce anIterable.- Type Parameters:
T- the type of elements in the originalIterableA- accumulator typeR- return type- Parameters:
elements- the iterable to run the Collector on. Can not be null.collector- theCollector. Can not be null.- Returns:
- the result of the reduction, using the given
Collector - Since:
- 3.1
- See Also:
javadocs for more details on how to use Collectors.
-
foldLeft
public static <T,R> R foldLeft(java.lang.Iterable<T> elements, java.util.function.BiFunction<? super R,? super T,? extends R> foldFunction, R initial)Folds theIterableelements into the value of potentially another type. Starting the computation with the specified initial value.- Type Parameters:
T- the type of elements in the originalIterableR- return type- Parameters:
elements- the iterable to run the foldLeft operation on. Can not be null.foldFunction- theBiFunctionused for reducing. Can not be null.initial- the initial value the folding is started with- Returns:
- the result of the reduction, using the given
BiFunction - Since:
- 6.1
-
partition
public static <A> Pair<java.lang.Iterable<A>,java.lang.Iterable<A>> partition(java.lang.Iterable<A> iterable, java.util.function.Predicate<? super A> p)
Filter anIterableinto aPairofIterable's.- Type Parameters:
A- the type- Parameters:
iterable- to be filteredp- to filter each element- Returns:
- a pair where the left matches the predicate, and the right does not.
- Since:
- 1.2
-
take
public static <A> java.lang.Iterable<A> take(int n, java.lang.Iterable<A> as)Aakes the firstnasand returns them.- Type Parameters:
A- type ofas- Parameters:
n- number ofasto take, must greater than or equal to zeroas- list of values, must not be null and must not contain null- Returns:
- first
nas - Since:
- 1.1
-
drop
public static <A> java.lang.Iterable<A> drop(int n, java.lang.Iterable<A> as)Drop the firstnasand return the rest.- Type Parameters:
A- type ofas- Parameters:
n- number ofasto drop, must greater than or equal to zeroas- list of values, must not be null and must not contain null- Returns:
- remaining
asafter dropping the firstn - Since:
- 1.1
-
dropWhile
public static <A> java.lang.Iterable<A> dropWhile(java.lang.Iterable<A> as, java.util.function.Predicate<A> p)Drop elements ofasuntil an element returns false for p#test- Type Parameters:
A- type ofas- Parameters:
as- iterable to remove the first elements ofas, must not be nullp- predicate used to test which elements to drop from the iterable, must not be null- Returns:
- remaining elements of
asafter removing the starting elements that for whichp#testreturns true - Since:
- 3.0
- See Also:
to remove the first n elements
-
takeWhile
public static <A> java.lang.Iterable<A> takeWhile(java.lang.Iterable<A> as, java.util.function.Predicate<A> p)Return a new iterable containing only the first elements ofasfor whichp#testreturns true.- Type Parameters:
A- type ofas- Parameters:
as- iterable to source the first elements ofas, must not be nullp- predicate used to test which elements to include in the new iterable, must not be null- Returns:
- a new iterable containing elements of
asstarting from the first element ofasuntilp#testreturns false - Since:
- 3.0
- See Also:
to take only the first n elements
-
zip
public static <A,B> java.lang.Iterable<Pair<A,B>> zip(java.lang.Iterable<A> as, java.lang.Iterable<B> bs)
Zips two iterables into a single iterable that producespairs. See unzip(Iterable) for the opposite operation- Type Parameters:
A- LHS typeB- RHS type- Parameters:
as- left valuesbs- right values- Returns:
- an
iterableof pairs, only as long as the shortest input iterable. - Since:
- 1.2
-
zipWith
public static <A,B,C> java.util.function.BiFunction<java.lang.Iterable<A>,java.lang.Iterable<B>,java.lang.Iterable<C>> zipWith(java.util.function.BiFunction<A,B,C> f)
Takes a two-arg function that returns a third type and reurn a new function that takes iterables of the two input types and combines them into a new iterable.- Type Parameters:
A- LHS typeB- RHS typeC- result type- Parameters:
f- combiner function, must not be null- Returns:
- an Function that takes two iterables and zips them using the supplied function. The input iterables must not be null and must not contain null
- Since:
- 1.2
-
zipWithIndex
public static <A> java.lang.Iterable<Pair<A,java.lang.Integer>> zipWithIndex(java.lang.Iterable<A> as)
Takes an Iterable, and returns an Iterable of a Pair of the original element and its index starting at zero.- Type Parameters:
A- the type- Parameters:
as- the original iterable- Returns:
- the decorated iterable that generates pairs.
- Since:
- 1.2
-
unzip
public static <A,B> Pair<java.lang.Iterable<A>,java.lang.Iterable<B>> unzip(java.lang.Iterable<Pair<A,B>> pairs)
- Type Parameters:
A- LHS typeB- RHS type- Parameters:
pairs- the values- Returns:
- a
pairofiterableof the same length as the input iterable. - Since:
- 1.2
-
rangeUntil
public static java.lang.Iterable<java.lang.Integer> rangeUntil(int start, int end)Creates a sequence ofintegersfrom start up to but not including end.- Parameters:
start- from (inclusive)end- to (exclusive)- Returns:
- a sequence of
integers - Since:
- 1.2
-
rangeUntil
public static java.lang.Iterable<java.lang.Integer> rangeUntil(int start, int end, int step)Creates a sequence ofintegersfrom start up to but not including end with the the supplied step between them.- Parameters:
start- from (inclusive)end- to (exclusive)step- size to step – must not be zero, must be positive if end is greater than start, neagtive otherwise- Returns:
- a sequence of
integers - Since:
- 1.2
-
rangeTo
public static java.lang.Iterable<java.lang.Integer> rangeTo(int start, int end)Creates a sequence ofintegersfrom start up to and including end.- Parameters:
start- from (inclusive)end- to (inclusive)- Returns:
- a sequence of
integers - Since:
- 1.2
-
rangeTo
public static java.lang.Iterable<java.lang.Integer> rangeTo(int start, int end, int step)Creates a sequence ofintegersfrom start up to and including end with the the supplied step between them.- Parameters:
start- from (inclusive), must be greater than zero and less than endend- to (inclusive)step- size to step – must not be zero, must be positive if end is greater than start, negative otherwise- Returns:
- a sequence of
integers - Since:
- 1.2
-
intersperse
public static <A> java.lang.Iterable<A> intersperse(java.lang.Iterable<? extends A> as, A a)Intersperse an element between all the elements in an iterable.- Type Parameters:
A- the type of the elements.- Parameters:
as- the source iterable.a- the element to intersperse between the source elements.- Returns:
- a new Iterable that intersperses the element between the source.
- Since:
- 2.3
-
intersperse
public static <A> java.lang.Iterable<A> intersperse(java.lang.Iterable<? extends A> as, java.util.function.Supplier<A> a)Intersperse an element between all the elements in an iterable.- Type Parameters:
A- the type of the elements.- Parameters:
as- the source iterable.a- the supplier of elements to intersperse between the source elements.- Returns:
- a new Iterable that intersperses the element between the source.
- Since:
- 2.3
-
size
public static <A> int size(java.lang.Iterable<A> as)
Return the size of an iterable. In most cases this function is required to walk the entire iterable to determine the result. Consider this an O(n) complexity function.- Type Parameters:
A- element type- Parameters:
as- iterable to compute the size of- Returns:
- number of elements in the iterable
- Since:
- 3.0
-
count
public static <A> int count(java.lang.Iterable<A> as, java.util.function.Predicate<? super A> p)Return the size of the elements inside an iterable that meets a specified predicate.- Type Parameters:
A- element type- Parameters:
as- the source iterablep- predicate to filter by- Returns:
- number of elements in the iterable that meets a prediction
- Since:
- 6.1
-
transform
@Deprecated public static <A,B> java.lang.Iterable<B> transform(java.lang.Iterable<A> as, java.util.function.Function<? super A,? extends B> f)Deprecated.function provided to make migration easier prefer to use #map where possibleTransform an interable by mapping a function across each of its elements- Type Parameters:
A- original iterable typeB- output iterable type- Parameters:
as- the source iterablef- function to apply to all the elements of as- Returns:
- new iterable containing the transformed values produced by f#apply
- Since:
- 3.0
-
map
public static <A,B> java.lang.Iterable<B> map(java.lang.Iterable<A> as, java.util.function.Function<? super A,? extends B> f)Apply the input function to each of the elements of the input iterable returning a new iterable- Type Parameters:
A- original iterable typeB- output iterable type- Parameters:
as- the source iterablef- function to apply to all the elements of as- Returns:
- new iterable containing values produced by f#apply called on each element
- Since:
- 3.0
-
filter
public static <A> java.lang.Iterable<A> filter(java.lang.Iterable<A> as, java.util.function.Predicate<? super A> p)Remove elements from the input iterable for which the predicate returns false- Type Parameters:
A- element type- Parameters:
as- original iterablep- predicate to filter by- Returns:
- new iterable containing only those elements for which p#test returns true
- Since:
- 3.0
-
join
public static <A> java.lang.Iterable<A> join(java.lang.Iterable<? extends java.lang.Iterable<? extends A>> ias)
Join Iterable<Iterable<A>> down to Iterable<A>. The resulting iterable will exhaust the first input iterable in order before returning values from the second. Input iterables must not be null and must not contain null.- Type Parameters:
A- element type- Parameters:
ias- one or more iterable to merge into the final iterable result, must not be null and must not return null- Returns:
- single level iterable with all the elements of the original iterables
- Since:
- 3.0
-
concat
@SafeVarargs public static <A> java.lang.Iterable<A> concat(java.lang.Iterable<? extends A>... as)
Concatenate a series of iterables into a single iterable. Returns an empty iterable if no iterables are supplied. Input iterables must not be null and must not contain null.- Type Parameters:
A- super type of contained by all input iterables- Parameters:
as- any number of iterables containing A- Returns:
- new iterable containing all the elements of the input iterables
- Since:
- 3.0
-
any
public static <A> boolean any(java.lang.Iterable<? extends A> as, java.util.function.Predicate<? super A> p)Check if the iterable contains any elements that match the predicate.- Type Parameters:
A- type of elements inside the input iterable- Parameters:
as- iterable to compare for matching elementsp- predicate to test for matching elements- Returns:
- true if any element in the iterable returns true for the input predicate otherwise false. False for an empty iterable.
- Since:
- 3.0
-
all
public static <A> boolean all(java.lang.Iterable<? extends A> as, java.util.function.Predicate<? super A> p)Check if all elements in the input iterable match the input predicate- Type Parameters:
A- type of elements inside the input iterable- Parameters:
as- iterable to compare for matching elementsp- predicate to test for matching elements- Returns:
- true if all elements in the iterable return true for the input predicate otherwise false. True for an empty iterable.
- Since:
- 3.0
-
iterate
public static <A> java.lang.Iterable<A> iterate(java.util.function.Function<? super A,? extends A> f, A start)Returns an infinite Iterable constructed by applying the given iteration function starting at the given value.- Type Parameters:
A- type of the elements- Parameters:
f- The iteration function, must not return nullstart- The value to begin iterating from.- Returns:
- An infinite Iterable of repeated applications of
ftostart. - Since:
- 2.4
-
unfold
public static <A,B> java.lang.Iterable<A> unfold(java.util.function.Function<? super B,Option<Pair<A,B>>> f, B seed)
Builds an Iterable from a seed value untilfreturnsnone().- Type Parameters:
A- type of the returned elements.B- type of the elements for whichfis applied.- Parameters:
f- The function that returns some(pair(a, b)), in which caseais the next element of the resulting Iterable andbis used as the input value for the next call off, ornone()if it is done producing the elements. f must not be null and must not return a pair containing null.seed- The start value to begin the unfold.- Returns:
- An Iterable that is a result of unfolding.
- Since:
- 2.4
-
mergeSorted
public static <A extends java.lang.Comparable<A>> java.lang.Iterable<A> mergeSorted(java.lang.Iterable<? extends java.lang.Iterable<A>> xss)
Merge a number of already sorted collections of elements into a single collection of elements, using the elements natural ordering.- Type Parameters:
A- collection type- Parameters:
xss- collection of already sorted collections, must not be null and must not return null- Returns:
xssmerged in a sorted order- Since:
- 1.1
-
mergeSorted
public static <A> java.lang.Iterable<A> mergeSorted(java.lang.Iterable<? extends java.lang.Iterable<A>> xss, java.util.Comparator<A> ordering)Merge a number of already sorted collections of elements into a single collection of elements.- Type Parameters:
A- type of the elements- Parameters:
xss- already sorted collection of collections, must not be null and must not return nullordering- ordering to use when comparing elements, must not be null- Returns:
xssmerged in a sorted order- Since:
- 1.1
-
addAll
public static <A> boolean addAll(java.util.Collection<A> collectionToModify, java.lang.Iterable<? extends A> elementsToAdd)Add all the elements of the iterable to the collection- Type Parameters:
A- element type- Parameters:
collectionToModify- collection to add elements toelementsToAdd- source of addtional elements- Returns:
- true if the collectionToModify was changed
- Since:
- 3.0
-
cycle
@SafeVarargs public static <A> java.lang.Iterable<A> cycle(A... as)
Return an infinite iterable that cycles through the input values in order in a loop. If no elements are provided returns an empty iterable. Does not support element removal via Iterator#remove.- Type Parameters:
A- returned elements- Parameters:
as- input values to cycle through- Returns:
- an infinite iterable containing the original elements
- Since:
- 3.0
-
cycle
public static <A> java.lang.Iterable<A> cycle(java.lang.Iterable<? extends A> as)
Return an infinite iterable that cycles through the input values in order in a loop. If no elements are provided returns an empty iterable. Does not support element removal via Iterator#remove.- Type Parameters:
A- returned elements- Parameters:
as- input values to cycle through must not be null- Returns:
- an infinite iterable containing the original elements
- Since:
- 3.0
-
makeString
public static <A> java.lang.String makeString(java.lang.Iterable<? extends A> as, java.lang.String start, java.lang.String sep, java.lang.String end, int maxLength)Pretty print an Iterable. Printing following this pattern: <start><element><sep><element><end> If the iterable would result in a String with length more than maxLength printing follows this pattern: <start><element><sep><element>...<end>- Type Parameters:
A- type of the elements in the iterable- Parameters:
as- the iterable to print must not be nullstart- prefix to start the printing withsep- separator to use between each elementend- suffic to end the printing withmaxLength- limit the length of the resulting string- Returns:
- a pretty printed copy of the input iterable
- Since:
- 3.0
-
makeString
public static <A> java.lang.String makeString(java.lang.Iterable<? extends A> as, java.lang.String start, java.lang.String sep, java.lang.String end)Pretty print an Iterable. Printing following this pattern: <start><element><sep><element><end> If the iterable would result in a String with length more than 100 characters printing follows this pattern: <start><element><sep><element>...<end>- Type Parameters:
A- type of the elements in the iterable- Parameters:
as- the iterable to print must not be nullstart- prefix to start the printing withsep- separator to use between each elementend- suffic to end the printing with- Returns:
- a pretty printed copy of the input iterable
- Since:
- 3.0
- See Also:
pretty print iterable with a custom length
-
memoize
public static <A> java.lang.Iterable<A> memoize(java.lang.Iterable<A> xs)
Makes a lazy copy ofxs.- Type Parameters:
A- type of elements inxs- Parameters:
xs-Iterableto be memoized- Returns:
- lazy copy of
as - Since:
- 1.1
-
-