info.javelot.functionalj.tuple
Class Tuples

java.lang.Object
  extended by info.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.

Author:
Copyright © 2006 Frederic Daoud

Method Summary
static
<A,B> Pair<List<A>,List<B>>
unzipPairs(Collection<Pair<A,B>> 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
<A,B> Pair<List<A>,List<B>>
unzipPairs(Pair<A,B>... p_pairs)
          Unzips an array of Pairs into a Pair of lists, similarily to the unzipPairs(Collection) method.
static
<A,B,C,D> Quadruple<List<A>,List<B>,List<C>,List<D>>
unzipQuadruples(Collection<Quadruple<A,B,C,D>> p_coll)
          Unzips a collection of Quadruples into a Quadruple of lists, similarily to the unzipPairs(Collection) method.
static
<A,B,C,D> Quadruple<List<A>,List<B>,List<C>,List<D>>
unzipQuadruples(Quadruple<A,B,C,D>... p_quadruples)
          Unzips an array of Quadruples into a Quadruple of lists, similarily to the unzipPairs(Collection) method.
static
<A,B,C> Triple<List<A>,List<B>,List<C>>
unzipTriples(Collection<Triple<A,B,C>> p_coll)
          Unzips a collection of Triples into a Triple of lists, similarily to the unzipPairs(Collection) method.
static
<A,B,C> Triple<List<A>,List<B>,List<C>>
unzipTriples(Triple<A,B,C>... p_triples)
          Unzips an array of Triples into a Triple of lists, similarily to the unzipPairs(Collection) method.
static
<A,B> List<Pair<A,B>>
zip(A[] p_array1, B[] p_array2)
          Zips objects from the two arrays to return a list of Pairs, similarily to the zip(Collection, Collection) method.
static
<A,B,C> List<Triple<A,B,C>>
zip(A[] p_array1, B[] p_array2, C[] p_array3)
          Zips objects from the three arrays to return a list of Triples, similarily to the zip(Collection, Collection) method.
static
<A,B,C,D> List<Quadruple<A,B,C,D>>
zip(A[] p_array1, B[] p_array2, C[] p_array3, D[] p_array4)
          Zips objects from the four arrays to return a list of Quadruples, similarily to the zip(Collection, Collection) method.
static
<A,B> List<Pair<A,B>>
zip(Collection<A> p_coll1, Collection<B> p_coll2)
          Zips objects from the two collections to return a list of Pairs.
static
<A,B,C> List<Triple<A,B,C>>
zip(Collection<A> p_coll1, Collection<B> p_coll2, Collection<C> p_coll3)
          Zips objects from the three collections to return a list of Triples, similarily to the zip(Collection, Collection) method.
static
<A,B,C,D> List<Quadruple<A,B,C,D>>
zip(Collection<A> p_coll1, Collection<B> p_coll2, Collection<C> p_coll3, Collection<D> p_coll4)
          Zips objects from the four collections to return a list of Quadruples, similarily to the zip(Collection, Collection) method.
static
<R,P1,P2> List<R>
zipWith(Function2<R,P1,P2> p_function2, Collection<? extends P1> p_coll1, Collection<? extends P2> p_coll2)
          Zips objects from the collections to return a list of objects returned by the specified function.
static
<R,P1,P2> List<R>
zipWith(Function2<R,P1,P2> p_function2, P1[] p_array1, P2[] p_array2)
          Zips objects from the two arrays to return a list of objects returned by the specified function.
static
<R,P1,P2,P3>
List<R>
zipWith(Function3<R,P1,P2,P3> p_function3, Collection<? extends P1> p_coll1, Collection<? extends P2> p_coll2, Collection<? extends P3> p_coll3)
          Zips objects from the three collections to return a list of objects returned by the specified function.
static
<R,P1,P2,P3>
List<R>
zipWith(Function3<R,P1,P2,P3> p_function3, P1[] p_array1, P2[] p_array2, P3[] p_array3)
          Zips objects from the three arrays to return a list of objects returned by the specified function.
static
<R,P1,P2,P3,P4>
List<R>
zipWith(Function4<R,P1,P2,P3,P4> p_function4, Collection<? extends P1> p_coll1, Collection<? extends P2> p_coll2, Collection<? extends P3> p_coll3, Collection<? extends P4> p_coll4)
          Zips objects from the four collections to return a list of objects returned by the specified function.
static
<R,P1,P2,P3,P4>
List<R>
zipWith(Function4<R,P1,P2,P3,P4> p_function4, P1[] p_array1, P2[] p_array2, P3[] p_array3, P4[] 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 <A,B> List<Pair<A,B>> zip(Collection<A> p_coll1,
                                        Collection<B> 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 <A,B,C> List<Triple<A,B,C>> zip(Collection<A> p_coll1,
                                              Collection<B> p_coll2,
                                              Collection<C> p_coll3)
Zips objects from the three collections to return a list of Triples, similarily to the zip(Collection, Collection) method.


zip

public static <A,B,C,D> List<Quadruple<A,B,C,D>> zip(Collection<A> p_coll1,
                                                     Collection<B> p_coll2,
                                                     Collection<C> p_coll3,
                                                     Collection<D> p_coll4)
Zips objects from the four collections to return a list of Quadruples, similarily to the zip(Collection, Collection) method.


zip

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


zip

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


zip

public static <A,B,C,D> List<Quadruple<A,B,C,D>> zip(A[] p_array1,
                                                     B[] p_array2,
                                                     C[] p_array3,
                                                     D[] p_array4)
Zips objects from the four arrays to return a list of Quadruples, similarily to the zip(Collection, Collection) method.


zipWith

public static <R,P1,P2> List<R> zipWith(Function2<R,P1,P2> p_function2,
                                        Collection<? extends P1> p_coll1,
                                        Collection<? extends P2> p_coll2)
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 list 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_function2 - the function used to zip the objects.
p_coll1 - the list of the first parameters to the function.
p_coll2 - the list of the second parameters to the function.
Returns:
a list of results of calling the function with the corresponding objects from the collections.

zipWith

public static <R,P1,P2,P3> List<R> zipWith(Function3<R,P1,P2,P3> p_function3,
                                           Collection<? extends P1> p_coll1,
                                           Collection<? extends P2> p_coll2,
                                           Collection<? extends P3> p_coll3)
Zips objects from the three collections to return a list of objects returned by the specified function.

See Also:
zipWith(Function2, Collection, Collection)

zipWith

public static <R,P1,P2,P3,P4> List<R> zipWith(Function4<R,P1,P2,P3,P4> p_function4,
                                              Collection<? extends P1> p_coll1,
                                              Collection<? extends P2> p_coll2,
                                              Collection<? extends P3> p_coll3,
                                              Collection<? extends P4> p_coll4)
Zips objects from the four collections to return a list of objects returned by the specified function.

See Also:
zipWith(Function2, Collection, Collection)

zipWith

public static <R,P1,P2> List<R> zipWith(Function2<R,P1,P2> p_function2,
                                        P1[] p_array1,
                                        P2[] p_array2)
Zips objects from the two arrays to return a list of objects returned by the specified function.

See Also:
zipWith(Function2, Collection, Collection)

zipWith

public static <R,P1,P2,P3> List<R> zipWith(Function3<R,P1,P2,P3> p_function3,
                                           P1[] p_array1,
                                           P2[] p_array2,
                                           P3[] p_array3)
Zips objects from the three arrays to return a list of objects returned by the specified function.

See Also:
zipWith(Function2, Collection, Collection)

zipWith

public static <R,P1,P2,P3,P4> List<R> zipWith(Function4<R,P1,P2,P3,P4> p_function4,
                                              P1[] p_array1,
                                              P2[] p_array2,
                                              P3[] p_array3,
                                              P4[] p_array4)
Zips objects from the four arrays to return a list of objects returned by the specified function.

See Also:
zipWith(Function2, Collection, Collection)

unzipPairs

public static <A,B> Pair<List<A>,List<B>> unzipPairs(Collection<Pair<A,B>> 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 <A,B,C> Triple<List<A>,List<B>,List<C>> unzipTriples(Collection<Triple<A,B,C>> 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 <A,B,C,D> Quadruple<List<A>,List<B>,List<C>,List<D>> unzipQuadruples(Collection<Quadruple<A,B,C,D>> 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 <A,B> Pair<List<A>,List<B>> unzipPairs(Pair<A,B>... 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 <A,B,C> Triple<List<A>,List<B>,List<C>> unzipTriples(Triple<A,B,C>... 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 <A,B,C,D> Quadruple<List<A>,List<B>,List<C>,List<D>> unzipQuadruples(Quadruple<A,B,C,D>... p_quadruples)
Unzips an array of Quadruples into a Quadruple of lists, similarily to the unzipPairs(Collection) method.

See Also:
unzipPairs(Collection)