|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectinfo.javelot.functionalj.Functions
public final class Functions
The Functions class contains static methods that use functions to
perform various computations, which makes it possible to use certain functional
programming patterns. These patterns include:
Note about Folding
Folding consists of using a function that requires two parameters, and applying it to a collection of elements, each time using the result of the previous function call as the first parameter to the next function call. The result of the last function call is the overall result of the fold.
Folding can be performed from the left or from the right.
Folding a collection from the left using a function f, a
starting parameter p, and a collection of elements
[e1, e2, ..., en] is equal to:
f(f(f(f(p, e1), e2), ...), en)
Folding a collection from the right using a function f, a
starting parameter p, and a collection of elements
[e1, e2, ..., en] is equal to:
f(e1, f(e2, f(..., f(en, p))))
Note about Scanning
Scanning is similar to folding except that scanning returns a list of successive reduced values, from the left or from the right.
Thus, folding from the left takes the last element of the list that results from scanning from the left; folding from the right takes the first element of the list that results from scanning from the right.
Thanks to Paul Field for his suggestions on improving performance.
| Method Summary | ||
|---|---|---|
static
|
all(Function1<Boolean,P> p_function1,
Collection<? extends P> p_coll)
Determines if all of the elements of the collection satisfy the condition represented by the given function. |
|
static
|
all(Function1<Boolean,P> p_function1,
P[] p_array)
Version of all(Function1,Collection) that accepts an array. |
|
static boolean |
and(boolean[] p_array)
Version of and(Collection) that accepts an array. |
|
static boolean |
and(Boolean[] p_array)
Version of and(Collection) that accepts an array. |
|
static boolean |
and(Collection<Boolean> p_coll)
Returns the result of &&ing the Boolean
values in the given collection. |
|
static
|
any(Function1<Boolean,P> p_function1,
Collection<? extends P> p_coll)
Determines whether any of the elements of the collection satisfy the condition represented by the given function. |
|
static
|
any(Function1<Boolean,P> p_function1,
P[] p_array)
Version of any(Function1,Collection) that accepts an array. |
|
static
|
filter(Function1<Boolean,P> p_filter,
Collection<? extends P> p_coll)
Filters a collection of items according to a filter function, which accepts one parameter of the same type as the items, and returns a Boolean value indicating whether or not the item satisfies the
filter condition. |
|
static
|
filter(Function1<Boolean,P> p_filter,
P[] p_array)
Version of filter(Function1, Collection) which accepts an array. |
|
static
|
filterNot(Function1<Boolean,P> p_filter,
Collection<? extends P> p_coll)
Same as filter(Function1,Collection), except that the elements that
do not satisfy the filter condition are returned. |
|
static
|
filterNot(Function1<Boolean,P> p_filter,
P[] p_array)
Version of filterNot(Function1, Collection) which accepts an array. |
|
static
|
foldl(Function2<R,R,P> p_function2,
R p_start,
Collection<? extends P> p_coll)
Folds a collection from the left using the given function and starting parameter. |
|
static
|
foldl(Function2<R,R,P> p_function2,
R p_start,
P[] p_array)
Version of foldl(Function2,Object,Collection) that accepts an array. |
|
static
|
foldl1(Function2<R,R,R> p_function2,
Collection<? extends R> p_coll)
Folds a collection from the left using the given function. |
|
static
|
foldl1(Function2<R,R,R> p_function2,
R[] p_array)
Version of foldl1(Function2,Collection) that accepts an array. |
|
static
|
foldr(Function2<R,P,R> p_function2,
R p_start,
Collection<? extends P> p_coll)
Folds a collection from the right using the given function and starting parameter. |
|
static
|
foldr(Function2<R,P,R> p_function2,
R p_start,
P[] p_array)
Version of foldr(Function2,Object,Collection) that accepts an array. |
|
static
|
foldr1(Function2<R,R,R> p_function2,
Collection<? extends R> p_coll)
Folds a collection from the right using the given function. |
|
static
|
foldr1(Function2<R,R,R> p_function2,
R[] p_array)
Version of foldr1(Function2,Collection) that accepts an array. |
|
static
|
map(Function1<R,P> p_function1,
Collection<? extends P> p_coll)
Maps a function to a collection of parameters, by calling the function with each element of the collection in turn as a parameter, and returning a list of the results. |
|
static
|
map(Function1<R,P> p_function1,
P... p_array)
Version of map(Function1, Collection) which accepts an array. |
|
static boolean |
or(boolean[] p_array)
Version of or(Collection) that accepts an array. |
|
static boolean |
or(Boolean[] p_array)
Version of or(Collection) that accepts an array. |
|
static boolean |
or(Collection<Boolean> p_coll)
Returns the result of ||ing the Boolean values
in the given collection. |
|
static
|
scanl(Function2<R,R,P> p_function2,
R p_start,
Collection<? extends P> p_coll)
Scans a collection to return successive reduced values from the left. |
|
static
|
scanl(Function2<R,R,P> p_function2,
R p_start,
P[] p_array)
Version of scanl(Function2,Object,Collection) which accepts an array. |
|
static
|
scanl1(Function2<R,R,R> p_function2,
Collection<? extends R> p_coll)
Scans a collection to return successive reduced values from the left, using the first element of the collection as a starting parameter. |
|
static
|
scanl1(Function2<R,R,R> p_function2,
R[] p_array)
Version of scanl1(Function2,Collection) that accepts an array. |
|
static
|
scanr(Function2<R,P,R> p_function2,
R p_start,
Collection<? extends P> p_coll)
Scans a collection to return successive reduced values from the right. |
|
static
|
scanr(Function2<R,P,R> p_function2,
R p_start,
P[] p_array)
Version of scanr(Function2,Object,Collection) that accepts an array. |
|
static
|
scanr1(Function2<R,R,R> p_function2,
Collection<? extends R> p_coll)
Scans a collection to return successive reduced values from the right, using the first element of the collection as a starting parameter. |
|
static
|
scanr1(Function2<R,R,R> p_function2,
R[] p_array)
Version of scanr1(Function2,Collection) that accepts an array. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static <R,P> List<R> map(Function1<R,P> p_function1,
Collection<? extends P> p_coll)
throws FunctionException
p_function1 - the function to map.p_coll - the collection of parameters on which to map the function.
null or empty, an empty
list is returned.
FunctionException - thrown if the function is null, or
if one is thrown while using the function.
public static <R,P> List<R> map(Function1<R,P> p_function1,
P... p_array)
map(Function1, Collection) which accepts an array.
map(Function1, Collection).
public static <P> List<P> filter(Function1<Boolean,P> p_filter,
Collection<? extends P> p_coll)
throws FunctionException
Boolean value indicating whether or not the item satisfies the
filter condition.
P - the type of the items to be filtered.p_filter - the filter function.p_coll - the collection of items to be filtered.
true.
FunctionException - thrown if an exception occurs while calling the
filter function.
public static <P> List<P> filter(Function1<Boolean,P> p_filter,
P[] p_array)
throws FunctionException
filter(Function1, Collection) which accepts an array.
FunctionExceptionfilter(Function1, Collection).
public static <P> List<P> filterNot(Function1<Boolean,P> p_filter,
Collection<? extends P> p_coll)
throws FunctionException
filter(Function1,Collection), except that the elements that
do not satisfy the filter condition are returned.
FunctionExceptionfilter(Function1,Collection)
public static <P> List<P> filterNot(Function1<Boolean,P> p_filter,
P[] p_array)
throws FunctionException
filterNot(Function1, Collection) which accepts an array.
FunctionExceptionfilterNot(Function1, Collection).
public static <R,P> R foldl(Function2<R,R,P> p_function2,
R p_start,
Collection<? extends P> p_coll)
throws FunctionException
p_function2 - the function to use while folding.p_start - the starting parameter to the function.p_coll - the collection of elements to be folded.
FunctionException - thrown if the function is null, or
if an exception occurs while using the function.
public static <R,P> R foldl(Function2<R,R,P> p_function2,
R p_start,
P[] p_array)
throws FunctionException
foldl(Function2,Object,Collection) that accepts an array.
FunctionExceptionfoldl(Function2,Object,Collection)
public static <R> R foldl1(Function2<R,R,R> p_function2,
Collection<? extends R> p_coll)
throws FunctionException
p_function2 - the function to use while folding.p_coll - the collection of elements to be folded.
FunctionException - thrown if the collection is null or
empty.foldl(Function2, Object, Collection)
public static <R> R foldl1(Function2<R,R,R> p_function2,
R[] p_array)
throws FunctionException
foldl1(Function2,Collection) that accepts an array.
FunctionExceptionfoldl1(Function2,Collection)
public static <R,P> List<R> scanl(Function2<R,R,P> p_function2,
R p_start,
Collection<? extends P> p_coll)
throws FunctionException
p_function2 - the function used for scanning.p_start - the starting parameter to the function.p_coll - the collection of elements to be scanned.
FunctionException - thrown if the function is null or if
an exception occurs while calling the function.
public static <R,P> List<R> scanl(Function2<R,R,P> p_function2,
R p_start,
P[] p_array)
throws FunctionException
scanl(Function2,Object,Collection) which accepts an array.
FunctionExceptionscanl(Function2,Object,Collection)
public static <R> List<R> scanl1(Function2<R,R,R> p_function2,
Collection<? extends R> p_coll)
throws FunctionException
FunctionExceptionscanl(Function2, Object, Collection).
public static <R> List<R> scanl1(Function2<R,R,R> p_function2,
R[] p_array)
throws FunctionException
scanl1(Function2,Collection) that accepts an array.
FunctionExceptionscanl1(Function2,Collection)
public static <R,P> R foldr(Function2<R,P,R> p_function2,
R p_start,
Collection<? extends P> p_coll)
throws FunctionException
p_function2 - the function to use while folding.p_start - the starting parameter to the function.p_coll - the collection of elements to be folded.
FunctionException - thrown if the function is null, or
if an exception occurs while using the function.
public static <R,P> R foldr(Function2<R,P,R> p_function2,
R p_start,
P[] p_array)
throws FunctionException
foldr(Function2,Object,Collection) that accepts an array.
FunctionExceptionfoldr(Function2,Object,Collection).
public static <R> R foldr1(Function2<R,R,R> p_function2,
Collection<? extends R> p_coll)
throws FunctionException
p_function2 - the function to use while folding.p_coll - the collection of elements to be folded.
FunctionException - thrown if the list is null or empty.foldr(Function2,Object,Collection)
public static <R> R foldr1(Function2<R,R,R> p_function2,
R[] p_array)
throws FunctionException
foldr1(Function2,Collection) that accepts an array.
FunctionExceptionfoldr1(Function2,Collection).
public static <R,P> List<R> scanr(Function2<R,P,R> p_function2,
R p_start,
Collection<? extends P> p_coll)
throws FunctionException
p_function2 - the function used for scanning.p_start - the starting parameter to the function.p_coll - the collection of elements to be scanned.
FunctionException - thrown if the function is null.
public static <R,P> List<R> scanr(Function2<R,P,R> p_function2,
R p_start,
P[] p_array)
throws FunctionException
scanr(Function2,Object,Collection) that accepts an array.
FunctionExceptionscanr(Function2,Object,Collection).
public static <R> List<R> scanr1(Function2<R,R,R> p_function2,
Collection<? extends R> p_coll)
throws FunctionException
FunctionExceptionscanr(Function2,Object,Collection).
public static <R> List<R> scanr1(Function2<R,R,R> p_function2,
R[] p_array)
throws FunctionException
scanr1(Function2,Collection) that accepts an array.
FunctionExceptionscanr1(Function2,Collection).
public static boolean and(Collection<Boolean> p_coll)
throws FunctionException
&&ing the Boolean
values in the given collection. Thus if at least one value is
false, the result will be false.
p_coll - the collection of Boolean values to be
&&ed.
true if all the values of the collection are
true; false otherwise.
FunctionException - thrown if not all elements of the collection are
of Boolean type.
public static boolean and(Boolean[] p_array)
throws FunctionException
and(Collection) that accepts an array.
FunctionExceptionand(Collection).
public static boolean and(boolean[] p_array)
throws FunctionException
and(Collection) that accepts an array.
FunctionExceptionand(Collection).
public static <P> boolean all(Function1<Boolean,P> p_function1,
Collection<? extends P> p_coll)
throws FunctionException
p_function1 - the function that represents the condition to be met.p_coll - the collection of elements to be tested against the condition.
true if all of the elements of the collection satisfy the
condition; false otherwise.
FunctionException
public static <P> boolean all(Function1<Boolean,P> p_function1,
P[] p_array)
throws FunctionException
all(Function1,Collection) that accepts an array.
FunctionExceptionall(Function1,Collection).
public static boolean or(Collection<Boolean> p_coll)
throws FunctionException
||ing the Boolean values
in the given collection. Thus if at least one value is true, the
result will be true.
p_coll - the collection of Boolean values to be
||ed.
false if all the values of the collection are
false; true otherwise.
FunctionException
public static boolean or(Boolean[] p_array)
throws FunctionException
or(Collection) that accepts an array.
FunctionExceptionor(Collection).
public static boolean or(boolean[] p_array)
throws FunctionException
or(Collection) that accepts an array.
FunctionExceptionor(Collection).
public static <P> boolean any(Function1<Boolean,P> p_function1,
Collection<? extends P> p_coll)
throws FunctionException
p_function1 - the function that represents the condition to be met.p_coll - the collection of elements to be tested against the condition.
true if at least one element of the collection satisfies
the condition; false otherwise.
FunctionException
public static <P> boolean any(Function1<Boolean,P> p_function1,
P[] p_array)
throws FunctionException
any(Function1,Collection) that accepts an array.
FunctionExceptionany(Function1,Collection).
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||