info.javelot.functionalj.tuple
Class Tuples

java.lang.Object
  extended byinfo.javelot.functionalj.tuple.Tuples

public final class Tuples
extends Object

Contains static methods to zip and unzip tuples. These are:

Zipping consists of creating a list of tuples from a number of collections, creating each tuple by taking corresponding elements from the collections. For example:

zip([1,2,3,4],[5,6,7,8],[9,10,11,12])

returns

[(1,5,9),(2,6,10),(3,7,11),(4,8,12)]

where each element in the resulting list is a Triple.

Unzipping consists of returning a tuple of collections from a collection of tuples, thus reversing the zipping operation. Unzipping the result of the previous example would return a Triple containing the original collections.

The zipWith methods provide a more general mechanism, where the function used to zip corresponding elements from the collections can be specified as a parameter. For example:

 Function2 add = Operators.add;

 List list1 = Lists.asList(new int[] {1, 2, 3, 4});
 List list2 = Lists.asList(new int[] {5, 6, 7, 8});

 List results = Tuples.zipWith(add, list1, list2);
 //results contains [6, 8, 10, 12].

 

Author:
Copyright © 2006 Frederic Daoud

Method Summary
static Pair unzipPairs(Collection p_coll)
          Unzips a collection of Pairs into a Pair of lists, with the first list containing all of the first elements of the pairs, and the second list containing all of the second elements of the pairs.
static Pair unzipPairs(Pair[] p_pairs)
          Unzips an array of Pairs into a Pair of lists, similarily to the unzipPairs(Collection) method.
static Quadruple unzipQuadruples(Collection p_coll)
          Unzips a collection of Quadruples into a Quadruple of lists, similarily to the unzipPairs(Collection) method.
static Quadruple unzipQuadruples(Quadruple[] p_quadruples)
          Unzips an array of Quadruples into a Quadruple of lists, similarily to the unzipPairs(Collection) method.
static Triple unzipTriples(Collection p_coll)
          Unzips a collection of Triples into a Triple of lists, similarily to the unzipPairs(Collection) method.
static Triple unzipTriples(Triple[] p_triples)
          Unzips an array of Triples into a Triple of lists, similarily to the unzipPairs(Collection) method.
static List zip(Collection p_coll1, Collection p_coll2)
          Zips objects from the two collections to return a list of Pairs.
static List zip(Collection p_coll1, Collection p_coll2, Collection p_coll3)
          Zips objects from the three collections to return a list of Triples, similarily to the zip(Collection, Collection) method.
static List zip(Collection p_coll1, Collection p_coll2, Collection p_coll3, Collection p_coll4)
          Zips objects from the four collections to return a list of Quadruples, similarily to the zip(Collection, Collection) method.
static List zip(Object[] p_array1, Object[] p_array2)
          Zips objects from the two arrays to return a list of Pairs, similarily to the zip(Collection,Collection) method.
static List zip(Object[] p_array1, Object[] p_array2, Object[] p_array3)
          Zips objects from the three arrays to return a list of Triples, similarily to the zip(Collection,Collection) method.
static List zip(Object[] p_array1, Object[] p_array2, Object[] p_array3, Object[] p_array4)
          Zips objects from the four arrays to return a list of Quadruples, similarily to the zip(Collection, Collection) method.
static List zipWith(Function1 p_function1, Collection[] p_colls)
          Zips objects from the collections to return a list of objects returned by the specified function.
static List zipWith(Function2 p_function2, Collection p_coll1, Collection p_coll2)
          Zips objects from the two collections to return a list of objects returned by the specified function.
static List zipWith(Function2 p_function2, Object[] p_array1, Object[] p_array2)
          Zips objects from the two arrays to return a list of objects returned by the specified function.
static List zipWith(Function3 p_function3, Collection p_coll1, Collection p_coll2, Collection p_coll3)
          Zips objects from the three collections to return a list of objects returned by the specified function.
static List zipWith(Function3 p_function3, Object[] p_array1, Object[] p_array2, Object[] p_array3)
          Zips objects from the three arrays to return a list of objects returned by the specified function.
static List zipWith(Function4 p_function4, Collection p_coll1, Collection p_coll2, Collection p_coll3, Collection p_coll4)
          Zips objects from the four collections to return a list of objects returned by the specified function.
static List zipWith(Function4 p_function4, Object[] p_array1, Object[] p_array2, Object[] p_array3, Object[] p_array4)
          Zips objects from the four arrays to return a list of objects returned by the specified function.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

zip

public static List zip(Collection p_coll1,
                       Collection p_coll2)
Zips objects from the two collections to return a list of Pairs. This method calls zipWith(Function2, Collection, Collection), passing the Pair.pair function and the two collections. If the collections contain an unequal number of objects, the number of Pairs returned is equal to the number of objects contained in the smaller of the two collections.

Parameters:
p_coll1 - the first of the two collections of objects to zip.
p_coll2 - the second of the two collections of objects to zip.
Returns:
a list of Pairs.

zip

public static List zip(Collection p_coll1,
                       Collection p_coll2,
                       Collection p_coll3)
Zips objects from the three collections to return a list of Triples, similarily to the zip(Collection, Collection) method.


zip

public static List zip(Collection p_coll1,
                       Collection p_coll2,
                       Collection p_coll3,
                       Collection p_coll4)
Zips objects from the four collections to return a list of Quadruples, similarily to the zip(Collection, Collection) method.


zip

public static List zip(Object[] p_array1,
                       Object[] p_array2)
Zips objects from the two arrays to return a list of Pairs, similarily to the zip(Collection,Collection) method.


zip

public static List zip(Object[] p_array1,
                       Object[] p_array2,
                       Object[] p_array3)
Zips objects from the three arrays to return a list of Triples, similarily to the zip(Collection,Collection) method.


zip

public static List zip(Object[] p_array1,
                       Object[] p_array2,
                       Object[] p_array3,
                       Object[] p_array4)
Zips objects from the four arrays to return a list of Quadruples, similarily to the zip(Collection, Collection) method.


zipWith

public static List zipWith(Function2 p_function2,
                           Collection p_coll1,
                           Collection p_coll2)
Zips objects from the two collections to return a list of objects returned by the specified function. See zipWith(Function1, Collection[]) method for more details.

See Also:
zipWith(Function1, Collection[])

zipWith

public static List zipWith(Function3 p_function3,
                           Collection p_coll1,
                           Collection p_coll2,
                           Collection p_coll3)
Zips objects from the three collections to return a list of objects returned by the specified function. See zipWith(Function1, Collection[]) method for more details.

See Also:
zipWith(Function1, Collection[])

zipWith

public static List zipWith(Function4 p_function4,
                           Collection p_coll1,
                           Collection p_coll2,
                           Collection p_coll3,
                           Collection p_coll4)
Zips objects from the four collections to return a list of objects returned by the specified function. See zipWith(Function1, Collection[]) method for more details.

See Also:
zipWith(Function1, Collection[])

zipWith

public static List zipWith(Function1 p_function1,
                           Collection[] p_colls)
Zips objects from the collections to return a list of objects returned by the specified function. The function must accept as many parameters as there are collections. The function is called using corresponding objects from the collections, and a lis of results is returned. If the collections are not of the same size, the size of the smallest collection is used. Thus objects of the collections that exceed the size of the smallest collection are not used.

Parameters:
p_function1 - the function used to zip the objects.
p_colls - the collections of objects to zip.
Returns:
a list of results of calling the function with the corresponding objects from the collections.

zipWith

public static List zipWith(Function2 p_function2,
                           Object[] p_array1,
                           Object[] p_array2)
Zips objects from the two arrays to return a list of objects returned by the specified function. See zipWith(Function1, Collection[]) method for more details.

See Also:
zipWith(Function1, Collection[])

zipWith

public static List zipWith(Function3 p_function3,
                           Object[] p_array1,
                           Object[] p_array2,
                           Object[] p_array3)
Zips objects from the three arrays to return a list of objects returned by the specified function. See zipWith(Function1, Collection[]) method for more details.

See Also:
zipWith(Function1, Collection[])

zipWith

public static List zipWith(Function4 p_function4,
                           Object[] p_array1,
                           Object[] p_array2,
                           Object[] p_array3,
                           Object[] p_array4)
Zips objects from the four arrays to return a list of objects returned by the specified function. See zipWith(Function1, Collection[]) method for more details.

See Also:
zipWith(Function1, Collection[])

unzipPairs

public static Pair unzipPairs(Collection p_coll)
Unzips a collection of Pairs into a Pair of lists, with the first list containing all of the first elements of the pairs, and the second list containing all of the second elements of the pairs.

Parameters:
p_coll - the collection of Pairs to be unzipped.
Returns:
a Pair containing two lists, one of the first elements of the pairs, and one of the second elements of the pairs.

unzipTriples

public static Triple unzipTriples(Collection p_coll)
Unzips a collection of Triples into a Triple of lists, similarily to the unzipPairs(Collection) method.

See Also:
unzipPairs(Collection)

unzipQuadruples

public static Quadruple unzipQuadruples(Collection p_coll)
Unzips a collection of Quadruples into a Quadruple of lists, similarily to the unzipPairs(Collection) method.

See Also:
unzipPairs(Collection)

unzipPairs

public static Pair unzipPairs(Pair[] p_pairs)
Unzips an array of Pairs into a Pair of lists, similarily to the unzipPairs(Collection) method.

See Also:
unzipPairs(Collection)

unzipTriples

public static Triple unzipTriples(Triple[] p_triples)
Unzips an array of Triples into a Triple of lists, similarily to the unzipPairs(Collection) method.

See Also:
unzipPairs(Collection)

unzipQuadruples

public static Quadruple unzipQuadruples(Quadruple[] p_quadruples)
Unzips an array of Quadruples into a Quadruple of lists, similarily to the unzipPairs(Collection) method.

See Also:
unzipPairs(Collection)