net.ericaro.neoitertools
Class Itertools

java.lang.Object
  extended by net.ericaro.neoitertools.Itertools

public class Itertools
extends java.lang.Object

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.

Author:
eric
See Also:
Itertools's wiki page, neoitertools site

Constructor Summary
Itertools()
           
 
Method Summary
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).
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.
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.
static
<T> Generator<T>
chain(Generator<T>... generators)
           Chain together two generators.
static
<T> Generator<java.util.List<T>>
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
<T> Generator<T>
cycle(Generator<T> generator)
           Make an generator returning elements from the generator and saving a copy of each.
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.
static
<T> Generator<Index<T>>
enumerate(Generator<T> generator)
           Return an Generator of Index object.
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.
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.
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.
static
<T> Lambda<T,T>
identity()
          Return the identity Lambda function.
static
<T> java.lang.Iterable<T>
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
<T> Generator<T>
iter(java.lang.Iterable<T> iterable)
          Turn any Iterable into a Generator
static
<T> Generator<T>
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
<T> Generator<T>
iter(T[] t)
          Turns any object array into an Generator
static
<T> Generator<T>
iter(Yield<java.lang.Void,T> yield)
          Turns a Yield generator into a standard Generator.
static
<T> java.util.List<T>
list(Generator<T> generator)
          Creates a List from a Generator
static
<T> java.util.List<T>
list(Generator<T> generator, int max)
          Creates a list from a generator, of size max
static
<T,K> Generator<K>
map(Lambda<? super T,K> mapper, Generator<T> sequence)
          Apply Lambda to every item of sequence and return a Generator of the results.
static
<T> Generator<java.util.List<T>>
permutations(Generator<T> generator)
           Return successive full length permutations of elements in the generator.
static
<T> Generator<java.util.List<T>>
permutations(Generator<T> generator, int r)
           Return successive r-length permutations of elements in the generator.
static
<T> Generator<java.util.List<T>>
product(Generator<Generator<T>> generators)
          Cartesian product of input sequences.
static
<T> Generator<java.util.List<T>>
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
<T> T
reduce(Operator<T> operator, Generator<T> generator)
          Equivalent to reduce(operator, generator, null);
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.
static
<T> Generator<T>
repeat(T object)
           Make an generator that returns object over and over again.
static
<T> Generator<T>
repeat(T object, int times)
           Make an generator that returns object times times.
static
<T> Generator<T>
reversed(Generator<T> generator)
           Return a reverse generator.
static
<T> Generator<T>
slice(Generator<T> generator, int stop)
          equivalent to slice(net.ericaro.neoitertools.Generator, int)(0, stop, 1);
static
<T> Generator<T>
slice(Generator<T> generator, int start, int stop)
          equivalent to slice(net.ericaro.neoitertools.Generator, int)(start, stop, 1);
static
<T> Generator<T>
slice(Generator<T> generator, int start, int stop, int step)
           Make an generator that returns selected elements from the generator.
static
<T extends java.lang.Comparable<? super T>>
Generator<T>
sorted(Generator<T> generator)
          Returns a sorted Generator in natural ascending order of T.
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.
static
<T> Generator<T>
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
<T,K extends java.lang.Comparable<? super K>>
Generator<T>
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
<T> Generator<T>
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
<T> java.util.List<Generator<T>>
tee(Generator<T> generator, int n)
          Return n independent generators from a single iterable.
static
<T> java.util.List<T>
tuple(Generator<T> generator)
          Turns any Generator into a "tuple", here an unmodifiable List
static
<R,T> R
yield(T t)
          Causes the generate method to stop, and make the tvalue returned by the associated next method.
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.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Itertools

public Itertools()
Method Detail

all

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).

Type Parameters:
T - type of generator elements
Parameters:
generator -
predicate -
Returns:
true|false
See Also:
Itertools's wiki page, neoitertools site

any

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.

Parameters:
generator -
predicate -
Returns:
true|false
See Also:
Itertools's wiki page, neoitertools site

chain

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

Parameters:
generators - a generator over generators
Returns:
a new generator that chain together all the above generators
See Also:
Itertools's wiki page, neoitertools site

chain

public static <T> Generator<T> chain(Generator<T>... generators)

Chain together two generators.

Parameters:
generators - any generators
See Also:
Itertools's wiki page, neoitertools site

combinations

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.

Parameters:
generator -
r -
Returns:
generator over combinations as list
See Also:
Itertools's wiki page, neoitertools site

count

public static Generator<java.lang.Integer> count()

Make a generator that returns all the consecutive integers starting with 0.

Returns:
a count generator
See Also:
count(int), Itertools's wiki page, neoitertools site

count

public static Generator<java.lang.Integer> count(int n)

Make an generator that returns consecutive integers starting with n.

Parameters:
n - the first integer returned by this generator
Returns:
a count generator
See Also:
Itertools's wiki page, neoitertools site

cycle

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.

Parameters:
generator - the source generator
Returns:
an generator returning elements from the generator over and over again.
See Also:
Itertools's wiki page, neoitertools site

dropwhile

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.

Type Parameters:
T -
Parameters:
generator -
predicate -
Returns:
generator
See Also:
Itertools's wiki page, neoitertools site

enumerate

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
 

Parameters:
generator - the source generator to enumerate
Returns:
a generator of index
See Also:
Itertools's wiki page, neoitertools site

filter

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.

Parameters:
predicate -
generator -
Returns:
a filtered generator
See Also:
Itertools's wiki page, neoitertools site

filterfalse

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.

Parameters:
predicate -
generator -
Returns:
a filtered generator
See Also:
Itertools's wiki page, neoitertools site

groupby

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.

Parameters:
generator - the source generator
key - the key mapper
Returns:
an generator that returns consecutive keys and groups from the source generator
See Also:
Itertools's wiki page, neoitertools site

identity

public static <T> Lambda<T,T> identity()
Return the identity Lambda function.

Type Parameters:
T - any type

in

public static <T> java.lang.Iterable<T> in(Generator<T> generator)

Turn a Generator into an Iterable.

A much required function to use the java foreach statement.
 for (int i : in(range(12)))
        System.out.println(i);
 

Parameters:
generator - the source generator
Returns:
a iterable fully equivalent to the generator
See Also:
Itertools's wiki page, neoitertools site

iter

public static Generator<java.lang.Boolean> iter(boolean[] array)
Turns any boolean[] array into a generator

Parameters:
array -
Returns:
a Generator over array
See Also:
Itertools's wiki page, neoitertools site

iter

public static Generator<java.lang.Byte> iter(byte[] array)
Turns any byte[] array into a generator

Parameters:
array -
Returns:
a Generator over array
See Also:
Itertools's wiki page, neoitertools site

iter

public static Generator<java.lang.Character> iter(char[] array)
Turns any char[] array into a generator

Parameters:
array -
Returns:
a Generator over array
See Also:
Itertools's wiki page, neoitertools site

iter

public static Generator<java.lang.Character> iter(java.lang.CharSequence seq)
Turns a CharSequence into an Generator

Parameters:
seq - any CharSequence
See Also:
Itertools's wiki page, neoitertools site

iter

public static Generator<java.lang.Double> iter(double[] array)
Turns any double[] array into a generator

Parameters:
array -
Returns:
a Generator over array
See Also:
Itertools's wiki page, neoitertools site

iter

public static Generator<java.lang.Float> iter(float[] array)
Turns any float[] array into a generator

Parameters:
array -
Returns:
a Generator over array
See Also:
Itertools's wiki page, neoitertools site

iter

public static Generator<java.lang.Integer> iter(int[] array)
Turns any int[] array into a generator

Parameters:
array -
Returns:
a Generator over array
See Also:
Itertools's wiki page, neoitertools site

iter

public static Generator<java.lang.Long> iter(long[] array)
Turns any long[] array into a generator

Parameters:
array -
Returns:
a Generator over array
See Also:
Itertools's wiki page, neoitertools site

iter

public static Generator<java.lang.Short> iter(short[] array)
Turns any short[] array into a generator

Parameters:
array -
Returns:
a Generator over array
See Also:
Itertools's wiki page, neoitertools site

iter

public static <T> Generator<T> iter(T[] t)
Turns any object array into an Generator

Parameters:
t -
See Also:
Itertools's wiki page, neoitertools site

iter

public static <T> Generator<T> iter(java.lang.Iterable<T> iterable)
Turn any Iterable into a Generator

Parameters:
iterable -
Returns:
a generator over the iterable
See Also:
Itertools's wiki page, neoitertools site

iter

public static <T> Generator<T> iter(java.util.Iterator<T> iterator)
Turn any Iterator into a Generator

Parameters:
iterator -
See Also:
Itertools's wiki page, neoitertools site

iter

public static <T> Generator<T> iter(Yield<java.lang.Void,T> yield)
Turns a Yield generator into a standard Generator.

Parameters:
yield - a Yield generator statement
See Also:
Itertools's wiki page, neoitertools site

list

public static <T> java.util.List<T> list(Generator<T> generator)
Creates a List from a Generator

Parameters:
generator -
Returns:
a java modifiable List
See Also:
Itertools's wiki page, neoitertools site

list

public static <T> java.util.List<T> list(Generator<T> generator,
                                         int max)
Creates a list from a generator, of size max

Parameters:
generator -
max - maximum size of the list
See Also:
Itertools's wiki page, neoitertools site

map

public static <T,K> Generator<K> map(Lambda<? super T,K> mapper,
                                     Generator<T> sequence)
Apply Lambda to every item of sequence and return a Generator of the results.

Parameters:
mapper -
sequence -
See Also:
Itertools's wiki page, neoitertools site

permutations

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.

Parameters:
generator -
Returns:
generator of permuted list
See Also:
Itertools's wiki page, neoitertools site

permutations

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.

Parameters:
generator -
r -
Returns:
a generator of r-sized permutations
See Also:
Itertools's wiki page, neoitertools site

product

public static <T> Generator<java.util.List<T>> product(Generator<Generator<T>> generators)
Cartesian product of input sequences.

Parameters:
generators - a generator of Generators
See Also:
Itertools's wiki page, neoitertools site

product

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.

Parameters:
generators - a generator of Generators
repeat -
Returns:
a generator of cartesian product items
See Also:
Itertools's wiki page, neoitertools site

range

public static Generator<java.lang.Integer> range(int end)
Equivalent to
 range(0, end, 1)
 

Parameters:
end -
See Also:
range(int, int, int), Itertools's wiki page, neoitertools site

range

public static Generator<java.lang.Integer> range(int start,
                                                 int end)
equivalent to
 range(start, end, 1)
 

Parameters:
start -
end -
See Also:
range(int, int, int), Itertools's wiki page, neoitertools site

range

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, ...].


reduce

public static <T> T reduce(Operator<T> operator,
                           Generator<T> generator)
Equivalent to reduce(operator, generator, null);

Type Parameters:
T -
Parameters:
operator -
generator -
See Also:
Itertools's wiki page, neoitertools site

reduce

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.

Parameters:
operator -
generator -
initializer -
Returns:
all items reduced to a single one
See Also:
Itertools's wiki page, neoitertools site

repeat

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.

Parameters:
object -
Returns:
an generator that returns object over and over again.
See Also:
Itertools's wiki page, neoitertools site

repeat

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.

Parameters:
object -
times - number of times the object is returned
Returns:
an generator that returns object over and over again.
See Also:
Itertools's wiki page, neoitertools site

reversed

public static <T> Generator<T> reversed(Generator<T> generator)

Return a reverse generator.

The whole generator is stored, so be careful when used.

Parameters:
generator - the source generator
Returns:
a generator that returns values in the reverse order
See Also:
Itertools's wiki page, neoitertools site

slice

public static <T> Generator<T> slice(Generator<T> generator,
                                     int stop)
equivalent to slice(net.ericaro.neoitertools.Generator, int)(0, stop, 1);

Parameters:
generator - source generator
stop - last included index
See Also:
Itertools's wiki page, neoitertools site

slice

public static <T> Generator<T> slice(Generator<T> generator,
                                     int start,
                                     int stop)
equivalent to slice(net.ericaro.neoitertools.Generator, int)(start, stop, 1);

Parameters:
generator - source generator
start - first included index
stop - last included index
See Also:
Itertools's wiki page, neoitertools site

slice

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.

Parameters:
generator - source generator
start - first included index
stop - last included index
step -
See Also:
Itertools's wiki page, neoitertools site

sorted

public static <T extends java.lang.Comparable<? super T>> Generator<T> sorted(Generator<T> generator)
Returns a sorted Generator in natural ascending order of T.

Type Parameters:
T -
Parameters:
generator -
See Also:
Itertools's wiki page, neoitertools site

sorted

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.

Type Parameters:
T - Type of items
K - Type of the key used to filter
Parameters:
generator - source generator
key - key extraction function
reverse - if true the the comparison is used in reverse order
See Also:
Itertools's wiki page, neoitertools site

sorted

public static <T> Generator<T> 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.

Type Parameters:
T -
Parameters:
generator -
See Also:
Itertools's wiki page, neoitertools site

sorted

public static <T,K extends java.lang.Comparable<? super K>> Generator<T> 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.

Type Parameters:
T - Type of items
K - Type of the key used to filter
Parameters:
generator - source generator
key - key extraction function
reverse - if true the the comparison is used in reverse order
See Also:
Itertools's wiki page, neoitertools site

string

public static java.lang.String string(Generator<java.lang.Character> chars)
Turn any Generator of Character into a String

Parameters:
chars -
See Also:
Itertools's wiki page, neoitertools site

stringBuilder

public static java.lang.StringBuilder stringBuilder(Generator<java.lang.Character> chars)
Turn any Generator of Character into a StringBuilder

Parameters:
chars -
See Also:
Itertools's wiki page, neoitertools site

takewhile

public static <T> Generator<T> 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.

Parameters:
generator - the source generator
predicate - test function
Returns:
an generator that returns elements from the generator as long as the predicate is true.
See Also:
Itertools's wiki page, neoitertools site

tee

public static <T> java.util.List<Generator<T>> tee(Generator<T> generator,
                                                   int n)
Return n independent generators from a single iterable.

Parameters:
generator - the source generator
n - number of independent generators
Returns:
an unmodifiable list of generators.
See Also:
Itertools's wiki page, neoitertools site

tuple

public static <T> java.util.List<T> tuple(Generator<T> generator)
Turns any Generator into a "tuple", here an unmodifiable List

Parameters:
generator -
Returns:
a tuple extracted from a generator
See Also:
Itertools's wiki page, neoitertools site

yield

public static <R,T> R yield(T t)
Causes the generate method to stop, and make the tvalue returned by the associated next method.

Parameters:
t -
See Also:
Yield protocol, Itertools's wiki page, neoitertools site

zip

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)

Parameters:
generators -
See Also:
Itertools's wiki page, neoitertools site

zip

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.

Parameters:
generator1 -
generator2 -
See Also:
Itertools's wiki page, neoitertools site


Copyright © 2011. All Rights Reserved.