|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnet.ericaro.neoitertools.Itertools
public class Itertools
This module implements a number of generator building blocks inspired by constructs from the Python programming languages. Each has been recast in a form suitable for Java.
The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Standardization helps avoid the readability and reliability problems which arise when many different individuals create their own slightly varying implementations, each with their own quirks and naming conventions.
The tools are designed to combine readily with one another. This makes it easy to construct more specialized tools succinctly and efficiently in pure Java.
| Constructor Summary | |
|---|---|
Itertools()
|
|
| Method Summary | ||
|---|---|---|
static
|
all(Generator<T> generator,
Lambda<? super T,java.lang.Boolean> predicate)
Return True if all elements of the generator are evaluated to true with the Predicate (or if the generator is empty). |
|
static
|
any(Generator<T> generator,
Lambda<? super T,java.lang.Boolean> predicate)
Return True if any element of the generator is mapped to true. |
|
static
|
chain(Generator<Generator<T>> generators)
Make an generator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. |
|
static
|
chain(Generator<T>... generators)
Chain together two generators. |
|
static
|
combinations(Generator<T> generator,
int r)
Return r length subsequences of elements from the input generator. |
|
static Generator<java.lang.Integer> |
count()
Make a generator that returns all the consecutive integers starting with 0. |
|
static Generator<java.lang.Integer> |
count(int n)
Make an generator that returns consecutive integers starting with n. |
|
static
|
cycle(Generator<T> generator)
Make an generator returning elements from the generator and saving a copy of each. |
|
static
|
dropwhile(Lambda<T,java.lang.Boolean> predicate,
Generator<T> generator)
Make an generator that drops elements from the generator as long as the predicate is true. |
|
static
|
enumerate(Generator<T> generator)
Return an Generator of Index object. |
|
static
|
filter(Lambda<T,java.lang.Boolean> predicate,
Generator<T> generator)
Make an generator that filters elements from generator returning only those for which the predicate is True. |
|
static
|
filterfalse(Lambda<T,java.lang.Boolean> predicate,
Generator<T> generator)
Make an generator that filters elements from generator returning only those for which the predicate is False. |
|
static
|
groupby(Generator<T> generator,
Lambda<T,K> key)
Make an generator that returns consecutive keys and groups from the source generator. |
|
static
|
identity()
Return the identity Lambda function. |
|
static
|
in(Generator<T> generator)
Turn a Generator into an Iterable. |
|
static Generator<java.lang.Boolean> |
iter(boolean[] array)
Turns any boolean[] array into a generator |
|
static Generator<java.lang.Byte> |
iter(byte[] array)
Turns any byte[] array into a generator |
|
static Generator<java.lang.Character> |
iter(char[] array)
Turns any char[] array into a generator |
|
static Generator<java.lang.Character> |
iter(java.lang.CharSequence seq)
Turns a CharSequence into an Generator |
|
static Generator<java.lang.Double> |
iter(double[] array)
Turns any double[] array into a generator |
|
static Generator<java.lang.Float> |
iter(float[] array)
Turns any float[] array into a generator |
|
static Generator<java.lang.Integer> |
iter(int[] array)
Turns any int[] array into a generator |
|
static
|
iter(java.lang.Iterable<T> iterable)
Turn any Iterable into a Generator |
|
static
|
iter(java.util.Iterator<T> iterator)
Turn any Iterator into a Generator |
|
static Generator<java.lang.Long> |
iter(long[] array)
Turns any long[] array into a generator |
|
static Generator<java.lang.Short> |
iter(short[] array)
Turns any short[] array into a generator |
|
static
|
iter(T[] t)
Turns any object array into an Generator |
|
static
|
iter(Yield<java.lang.Void,T> yield)
Turns a Yield generator into a standard Generator. |
|
static
|
list(Generator<T> generator)
Creates a List from a Generator |
|
static
|
list(Generator<T> generator,
int max)
Creates a list from a generator, of size max |
|
static
|
map(Lambda<? super T,K> mapper,
Generator<T> sequence)
Apply Lambda to every item of sequence and return a Generator of the results. |
|
static
|
permutations(Generator<T> generator)
Return successive full length permutations of elements in the generator. |
|
static
|
permutations(Generator<T> generator,
int r)
Return successive r-length permutations of elements in the generator. |
|
static
|
product(Generator<Generator<T>> generators)
Cartesian product of input sequences. |
|
static
|
product(Generator<Generator<T>> generators,
int repeat)
Cartesian product of input sequences. |
|
static Generator<java.lang.Integer> |
range(int end)
Equivalent to range(0, end, 1) |
|
static Generator<java.lang.Integer> |
range(int start,
int end)
equivalent to range(start, end, 1) |
|
static Generator<java.lang.Integer> |
range(int start,
int end,
int step)
This is a versatile function to create generator containing arithmetic progressions. |
|
static
|
reduce(Operator<T> operator,
Generator<T> generator)
Equivalent to reduce(operator, generator, null); |
|
static
|
reduce(Operator<T> operator,
Generator<T> generator,
T initializer)
Apply function of two arguments cumulatively to the items of generator, from left to right, so as to reduce the generator to a single value. |
|
static
|
repeat(T object)
Make an generator that returns object over and over again. |
|
static
|
repeat(T object,
int times)
Make an generator that returns object times times. |
|
static
|
reversed(Generator<T> generator)
Return a reverse generator. |
|
static
|
slice(Generator<T> generator,
int stop)
equivalent to slice(net.ericaro.neoitertools.Generator(0, stop, 1); |
|
static
|
slice(Generator<T> generator,
int start,
int stop)
equivalent to slice(net.ericaro.neoitertools.Generator(start, stop, 1); |
|
static
|
slice(Generator<T> generator,
int start,
int stop,
int step)
Make an generator that returns selected elements from the generator. |
|
static
|
sorted(Generator<T> generator)
Returns a sorted Generator in natural ascending order of T. |
|
static
|
sorted(Generator<T> generator,
java.util.Comparator<? super K> cmp,
Lambda<? super T,K> key,
boolean reverse)
Return a new sorted generator from the items in generator. |
|
static
|
sorted(Generator<T> generator,
java.util.Comparator<? super T> cmp)
Return a new sorted generator from the items in generator. the comparator is used to sort the generator. |
|
static
|
sorted(Generator<T> generator,
Lambda<T,K> key,
boolean reverse)
Return a new sorted generator from the items in generator. the Key Lambda is used to extract a key from T, and that key natural order is used to sort the whole generator. |
|
static java.lang.String |
string(Generator<java.lang.Character> chars)
Turn any Generator of Character into a String |
|
static java.lang.StringBuilder |
stringBuilder(Generator<java.lang.Character> chars)
Turn any Generator of Character into a StringBuilder |
|
static
|
takewhile(Generator<T> generator,
Lambda<? super T,java.lang.Boolean> predicate)
Make an generator that returns elements from the generator as long as the predicate is true. |
|
static
|
tee(Generator<T> generator,
int n)
Return n independent generators from a single iterable. |
|
static
|
tuple(Generator<T> generator)
Turns any Generator into a "tuple", here an unmodifiable List |
|
static
|
yield(T t)
Causes the generate method to stop, and make the tvalue
returned by the associated next method. |
|
static
|
zip(Generator<Generator<T>> generators)
This function returns an Generator of tuple (unmodifiable List) , where the i-th couple contains the i-th element from each of the argument
generators. |
|
static
|
zip(Generator<T1> generator1,
Generator<T2> generator2)
This function returns an Generator of Pairs, where the i-th pair contains the i-th element from each of the argument generators. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public Itertools()
| Method Detail |
|---|
public static <T> boolean all(Generator<T> generator,
Lambda<? super T,java.lang.Boolean> predicate)
Return True if all elements of the generator are evaluated to true with the Predicate (or if the generator is empty).
T - type of generator elementsgenerator - predicate -
public static <T> boolean any(Generator<T> generator,
Lambda<? super T,java.lang.Boolean> predicate)
Return True if any element of the generator is mapped to true. If the generator is empty, return False.
generator - predicate -
public static <T> Generator<T> chain(Generator<Generator<T>> generators)
Make an generator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. Used for treating consecutive sequences as a single sequence.
We do not use varargs due to an inner flaw in varargs that make them hard/impossible to combine with generics
generators - a generator over generators
public static <T> Generator<T> chain(Generator<T>... generators)
Chain together two generators.
generators - any generators
public static <T> Generator<java.util.List<T>> combinations(Generator<T> generator,
int r)
Return r length subsequences of elements from the input generator.
Combinations are emitted in lexicographic sort order. So, if the input generator is sorted, the combination tuples will be produced in sorted order.
Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination.
generator - r -
public static Generator<java.lang.Integer> count()
Make a generator that returns all the consecutive integers starting with 0.
count(int),
Itertools's wiki page,
neoitertools sitepublic static Generator<java.lang.Integer> count(int n)
Make an generator that returns consecutive integers starting with n.
n - the first integer returned by this generator
public static <T> Generator<T> cycle(Generator<T> generator)
Make an generator returning elements from the generator and saving a copy of each. When the generator is exhausted, return elements from the saved copy. Repeats indefinitely.
generator - the source generator
public static <T> Generator<T> dropwhile(Lambda<T,java.lang.Boolean> predicate,
Generator<T> generator)
Make an generator that drops elements from the generator as long as the predicate is true. Afterwards, returns every element.
Note, the generator does not produce any output until the predicate first becomes false, so it may have a lengthy start-up time.
T - generator - predicate -
public static <T> Generator<Index<T>> enumerate(Generator<T> generator)
Return an Generator of Index object.
The next() method of the generator returned by enumerate() returns an Index containing a count (from start which defaults to 0) and the corresponding value obtained from iterating over generator.
enumerate() is useful for obtaining an indexed series: (0, seq[0]), (1, seq[1]), (2, seq[2]), .... For example:
for (Index index : enumerate(seasons))
System.out.println(index.i + " " + index.value);
gives:
0 Spring
1 Summer
2 Fall
3 Winter
generator - the source generator to enumerate
public static <T> Generator<T> filter(Lambda<T,java.lang.Boolean> predicate,
Generator<T> generator)
Make an generator that filters elements from generator returning only those for which the predicate is True.
predicate - generator -
public static <T> Generator<T> filterfalse(Lambda<T,java.lang.Boolean> predicate,
Generator<T> generator)
Make an generator that filters elements from generator returning only those for which the predicate is False.
predicate - generator -
public static <T,K> Generator<Pair<K,Generator<T>>> groupby(Generator<T> generator,
Lambda<T,K> key)
Make an generator that returns consecutive keys and groups from the source generator.
The key is a function computing a key value for each element. Generally, the generator needs to already be sorted on the same key function.
The operation of groupby() is similar to the uniq filter in Unix. It generates a break or new group every time the value of the key function changes (which is why it is usually necessary to have sorted the data using the same key function). That behavior differs from SQL’s GROUP BY which aggregates common elements regardless of their input order. The returned group is itself an generator that shares the underlying generator with groupby(). Because the source is shared, when the groupby() object is advanced, the previous group is no longer visible. So, if that data is needed later, it should be stored as a list.
generator - the source generatorkey - the key mapper
public static <T> Lambda<T,T> identity()
T - any typepublic static <T> java.lang.Iterable<T> in(Generator<T> generator)
Turn a Generator into an Iterable.
for (int i : in(range(12)))
System.out.println(i);
generator - the source generator
public static Generator<java.lang.Boolean> iter(boolean[] array)
array -
arraypublic static Generator<java.lang.Byte> iter(byte[] array)
array -
arraypublic static Generator<java.lang.Character> iter(char[] array)
array -
arraypublic static Generator<java.lang.Character> iter(java.lang.CharSequence seq)
CharSequence into an Generator
seq - any CharSequencepublic static Generator<java.lang.Double> iter(double[] array)
array -
arraypublic static Generator<java.lang.Float> iter(float[] array)
array -
arraypublic static Generator<java.lang.Integer> iter(int[] array)
array -
arraypublic static Generator<java.lang.Long> iter(long[] array)
array -
arraypublic static Generator<java.lang.Short> iter(short[] array)
array -
arraypublic static <T> Generator<T> iter(T[] t)
t - public static <T> Generator<T> iter(java.lang.Iterable<T> iterable)
Iterable into a Generator
iterable -
public static <T> Generator<T> iter(java.util.Iterator<T> iterator)
Iterator into a Generator
iterator - public static <T> Generator<T> iter(Yield<java.lang.Void,T> yield)
yield - a Yield generator statementpublic static <T> java.util.List<T> list(Generator<T> generator)
List from a Generator
generator -
public static <T> java.util.List<T> list(Generator<T> generator,
int max)
max
generator - max - maximum size of the list
public static <T,K> Generator<K> map(Lambda<? super T,K> mapper,
Generator<T> sequence)
Lambda to every item of sequence and return a Generator of the results.
mapper - sequence - public static <T> Generator<java.util.List<T>> permutations(Generator<T> generator)
Return successive full length permutations of elements in the generator.
Permutations are emitted in lexicographic sort order. So, if the input iterable is sorted, the permutation list will be produced in sorted order.
Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each permutation.
generator -
public static <T> Generator<java.util.List<T>> permutations(Generator<T> generator,
int r)
Return successive r-length permutations of elements in the generator.
Permutations are emitted in lexicographic sort order. So, if the input iterable is sorted, the permutation list will be produced in sorted order.
Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each permutation.
generator - r -
public static <T> Generator<java.util.List<T>> product(Generator<Generator<T>> generators)
generators - a generator of Generators
public static <T> Generator<java.util.List<T>> product(Generator<Generator<T>> generators,
int repeat)
Cartesian product of input sequences.
repeat simulate the repetition of the input sequence.
generators - a generator of Generatorsrepeat -
public static Generator<java.lang.Integer> range(int end)
range(0, end, 1)
end - range(int, int, int),
Itertools's wiki page,
neoitertools site
public static Generator<java.lang.Integer> range(int start,
int end)
range(start, end, 1)
start - end - range(int, int, int),
Itertools's wiki page,
neoitertools site
public static Generator<java.lang.Integer> range(int start,
int end,
int step)
throws java.security.InvalidParameterException
This is a versatile function to create generator containing arithmetic progressions.
It is most often used in for loops. The full form returns an generator over Integers [start, start + step, start + 2 * step, ...].
Example:
range(0, 30, 5);gives
[0, 5, 10, 15, 20, 25]
range(0, 10, 3);gives
[0, 3, 6, 9];
range(0, -10, -1);gives
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9];
start - end - step -
java.security.InvalidParameterException
public static <T> T reduce(Operator<T> operator,
Generator<T> generator)
T - operator - generator -
public static <T> T reduce(Operator<T> operator,
Generator<T> generator,
T initializer)
Apply function of two arguments cumulatively to the items of generator, from left to right, so as to reduce the generator to a single value.
For example,
Operator<Integer> iadd = new Operator<Integer>() {
public Integer operate(Integer t1, Integer t2) {
return t1 + t2;
}
};
Integer res = reduce(iadd, range(1, 6), 0);
calculates
((((1 + 2) + 3) + 4) + 5)The left argument, x, is the accumulated value and the right argument, y, is the update value from the generator. If the initializer is not null, it is placed before the items of the generator in the calculation, and serves as a default when the iterable is empty. If initializer is null and generator contains only one item, the first item is returned.
operator - generator - initializer -
public static <T> Generator<T> repeat(T object)
Make an generator that returns object over and over again.
Runs indefinitely Used as argument to map() for invariant function parameters. Also used with zip() to create constant fields in a tuple record.
object -
public static <T> Generator<T> repeat(T object,
int times)
Make an generator that returns object times times.
Runs indefinitely unless the times argument is specified. Used as argument to imap() for invariant function parameters. Also used with izip() to create constant fields in a tuple record.
object - times - number of times the object is returned
public static <T> Generator<T> reversed(Generator<T> generator)
Return a reverse generator.
The whole generator is stored, so be careful when used.
generator - the source generator
public static <T> Generator<T> slice(Generator<T> generator,
int stop)
slice(net.ericaro.neoitertools.Generator, int) (0, stop, 1);
generator - source generatorstop - last included index
public static <T> Generator<T> slice(Generator<T> generator,
int start,
int stop)
slice(net.ericaro.neoitertools.Generator, int) (start, stop, 1);
generator - source generatorstart - first included indexstop - last included index
public static <T> Generator<T> slice(Generator<T> generator,
int start,
int stop,
int step)
Make an generator that returns selected elements from the generator.
If start is non-zero, then elements from the generator are skipped until start is reached. Afterward, elements are returned consecutively unless step is set higher than one which results in items being skipped. It stops at the specified position. slice() does not support negative values for start, stop, or step.
generator - source generatorstart - first included indexstop - last included indexstep - public static <T extends java.lang.Comparable<? super T>> Generator<T> sorted(Generator<T> generator)
T - generator -
public static <T,K> Generator<T> sorted(Generator<T> generator,
java.util.Comparator<? super K> cmp,
Lambda<? super T,K> key,
boolean reverse)
Return a new sorted generator from the items in generator.
cmp specifies a custom Comparator of K. key specifies a Lambda that is used to extract a comparison key (K) from each generator
element. reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.
T - Type of itemsK - Type of the key used to filtergenerator - source generatorkey - key extraction functionreverse - if true the the comparison is used in reverse order
public static <T> Generator<T> sorted(Generator<T> generator,
java.util.Comparator<? super T> cmp)
T - generator -
public static <T,K extends java.lang.Comparable<? super K>> Generator<T> sorted(Generator<T> generator,
Lambda<T,K> key,
boolean reverse)
T - Type of itemsK - Type of the key used to filtergenerator - source generatorkey - key extraction functionreverse - if true the the comparison is used in reverse orderpublic static java.lang.String string(Generator<java.lang.Character> chars)
chars - public static java.lang.StringBuilder stringBuilder(Generator<java.lang.Character> chars)
chars -
public static <T> Generator<T> takewhile(Generator<T> generator,
Lambda<? super T,java.lang.Boolean> predicate)
generator - the source generatorpredicate - test function
public static <T> java.util.List<Generator<T>> tee(Generator<T> generator,
int n)
generator - the source generatorn - number of independent generators
public static <T> java.util.List<T> tuple(Generator<T> generator)
List
generator -
public static <R,T> R yield(T t)
tvalue
returned by the associated next method.
t - public static <T> Generator<java.util.List<T>> zip(Generator<Generator<T>> generators)
This function returns an Generator of tuple (unmodifiable List) , where the i-th couple contains the i-th element from each of the argument
generators.
The returned generator is truncated in length to the length of the shortest argument sequence.
Due to static typing of java, it is not possible to provide a generic length of generator and at the same time provide mixed-type tuples, therefore every
generator must be of type T. To have two-mixed type use zip(Generator, Generator)
generators -
public static <T1,T2> Generator<Pair<T1,T2>> zip(Generator<T1> generator1,
Generator<T2> generator2)
This function returns an Generator of Pairs, where the i-th pair contains the i-th element from each of the argument generators.
The returned generator is truncated in length to the length of the shortest argument sequence. Due to static typing of java, it is not possible to provide a generic length of generator and at the same time provide mixed-type tuples.
generator1 - generator2 -
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||