|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectinfo.javelot.functionalj.Functions
The Functions class contains static methods that use functions to
perform various computations, which makes it possible to use functional
programming patterns.
Note about Folding
Folding consists of using a function that requires two more parameters,
or an object and one more parameter (in the latter case, this must be an
InstanceFunction), and applying it to a list of elements, each time using
the result of the previous function call as the first parameter to the next
function call.
Folding can be performed from the left or from the right.
Folding a list from the left using a function f, a starting
parameter p, and a list of elements [e1, e2, ..., en] is
equal to:
f(f(f(f(p, e1), e2), ...), en)
Folding a list from the right using a function f, a
starting parameter p, and a list 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 boolean |
all(Function p_function,
List p_list)
Determines if all of the elements of the list satisfy the condition represented by the given function. |
static boolean |
all(Function p_function,
Object[] p_array)
Determines if all of the elements of the array satisfy the condition represented by the given function, by converting the array to a list and calling all(Function,List). |
static boolean |
and(boolean[] p_array)
Returns the result of &&ing the boolean
values in the given array. |
static boolean |
and(Boolean[] p_array)
Returns the result of &&ing the Boolean
values in the given array. |
static boolean |
and(List p_list)
Returns the result of &&ing the Boolean
values in the given list. |
static boolean |
any(Function p_function,
List p_list)
Determines whether any of the elements of the list satisfy the condition represented by the given function. |
static boolean |
any(Function p_function,
Object[] p_array)
Determines whether any of the elements of the array satisfy the condition represented by the given function, by converting the array to a list and calling any(Function,List). |
static String |
concat(List p_list)
Concatenates the string representations of a list of objects to return a single string. |
static String |
concat(Object[] p_objects)
Concatenates an array of strings by first converting the array to a list, then calling concat(List). |
static List |
filter(Function p_function,
List p_list)
Filters a list to return only those elements for which the given function returns true. |
static List |
filter(Function p_function,
Object[] p_array)
Filters an array to return only those elements for which the given function returns true. |
static List |
filterNot(Function p_function,
List p_list)
Filters a list to return only those elements for which the given function returns false. |
static List |
filterNot(Function p_function,
Object[] p_array)
Filters a list to return only those elements for which the given function returns false. |
static Object |
foldl(Function p_function,
Object p_start,
List p_list)
Folds a list from the left using the given function and starting parameter. |
static Object |
foldl(Function p_function,
Object p_start,
Object[] p_array)
Folds an array from the left using the given function and starting parameter, first converting the array to a list and then calling foldl(Function,Object,List). |
static Object |
foldl1(Function p_function,
List p_list)
Folds a list from the left using the given function. |
static Object |
foldl1(Function p_function,
Object[] p_array)
Folds an array from the left using the given function, first converting the array to a list and then calling foldl1(Function,List). |
static Object |
foldr(Function p_function,
Object p_start,
List p_list)
Folds a list from the right using the given function and starting parameter. |
static Object |
foldr(Function p_function,
Object p_start,
Object[] p_array)
Folds an array from the right using the given function and starting parameter, first converting the array to a list and then calling foldr(Function,Object,List). |
static Object |
foldr1(Function p_function,
List p_list)
Folds a list from the right using the given function. |
static Object |
foldr1(Function p_function,
Object[] p_array)
Folds an array from the right using the given function, first converting the array to a list and then calling foldr1(Function,List). |
static List |
intersperse(Object p_object,
List p_list)
Intersperses the specified object into the given list, such that the object is inserted between each pair of elements of the list. |
static List |
intersperse(Object p_object,
Object[] p_array)
Intersperses the specified object into the given array, such that the object is inserted between each pair of elements of the array. |
static List |
map(Function p_function,
List p_list)
Maps a function to a list of parameters, by applying in turn each element of the list as a parameter to the function and calling it, and returns a list of the results. |
static List |
map(Function p_function,
Object[] p_array)
Maps a function to an array of parameters, first converting the array to a List and then calling map(Function,List). |
static boolean |
or(boolean[] p_array)
Returns the result of ||ing the boolean values in
the given array. |
static boolean |
or(Boolean[] p_array)
Returns the result of ||ing the Boolean values
in the given array. |
static boolean |
or(List p_list)
Returns the result of ||ing the Boolean values
in the given list. |
static List |
scanl(Function p_function,
Object p_start,
List p_list)
Scans a list to return successive reduced values from the left. |
static List |
scanl(Function p_function,
Object p_start,
Object[] p_array)
Scans a list to return successive reduced values from the left, first converting the array to a list and then calling scanl(Function,Object,List). |
static List |
scanl1(Function p_function,
List p_list)
Scans a list to return successive reduced values from the left, using the first element of the list as a starting parameter. |
static List |
scanl1(Function p_function,
Object[] p_array)
Scans a list to return successive reduced values from the left, first converting the array to a list and then calling scanl1(Function,List). |
static List |
scanr(Function p_function,
Object p_start,
List p_list)
Scans a list to return successive reduced values from the right. |
static List |
scanr(Function p_function,
Object p_start,
Object[] p_array)
Scans a list to return successive reduced values from the right, first converting the array to a list and then calling scanr(Function,Object,List). |
static List |
scanr1(Function p_function,
List p_list)
Scans a list to return successive reduced values from the right, using the first element of the list as a starting parameter. |
static List |
scanr1(Function p_function,
Object[] p_array)
Scans a list to return successive reduced values from the right, first converting the array to a list and then calling scanr1(Function,List). |
static int |
sum(List p_list)
Given a List of Numbers, returns the sum
of their int values. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
public static List map(Function p_function,
List p_list)
throws FunctionException
p_function - the function to be mapped.p_list - the list of objects or 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 List map(Function p_function,
Object[] p_array)
map(Function,List).
map(Function,List).
public static List filter(Function p_function,
List p_list)
throws FunctionException
true. Thus the function must accept one more parameter,
or have all parameters but no object (in the case of an InstanceFunction, and return a result of type boolean or Boolean.
p_function - the function to use to filter the list.p_list - the list to be filtered.
true. If the specified list is null or
empty, an empty list is returned.
FunctionException - thrown if the function is null, if
it does not return a result of type boolean or Boolean, or if an exception occurs while using the function.
public static List filter(Function p_function,
Object[] p_array)
true. After converting the array to a List, the filter(Function,List) method is called.
filter(Function,List)
public static List filterNot(Function p_function,
List p_list)
throws FunctionException
false. This is equivalent to calling filter(Function,List) with a ComposedFunction of Operators.not and the given function.
FunctionExceptionfilter(Function,List).,
Operators.not
public static List filterNot(Function p_function,
Object[] p_array)
false. After converting the array to a List, the filterNot(Function,List) method is called.
filterNot(Function,List)
public static Object foldl(Function p_function,
Object p_start,
List p_list)
throws FunctionException
p_function - the function to use while folding.p_start - the starting parameter to the function.p_list - the list of elements to be folded.
FunctionException - thrown if the function is null, or
if an exception occurs while using the function.
public static Object foldl(Function p_function,
Object p_start,
Object[] p_array)
foldl(Function,Object,List).
foldl(Function,Object,List).
public static Object foldl1(Function p_function,
List p_list)
throws FunctionException
p_function - the function to use while folding.p_list - the list of elements to be folded.
FunctionException - thrown if the list is null or empty.foldl(Function,Object,List)
public static Object foldl1(Function p_function,
Object[] p_array)
throws FunctionException
foldl1(Function,List).
FunctionExceptionfoldl1(Function,List).
public static Object foldr(Function p_function,
Object p_start,
List p_list)
p_function - the function to use while folding.p_start - the starting parameter to the function.p_list - the list of elements to be folded.
FunctionException - thrown if the function is null, or
if an exception occurs while using the function.
public static Object foldr(Function p_function,
Object p_start,
Object[] p_array)
foldr(Function,Object,List).
foldr(Function,Object,List).
public static Object foldr1(Function p_function,
List p_list)
throws FunctionException
p_function - the function to use while folding.p_list - the list of elements to be folded.
FunctionException - thrown if the list is null or empty.foldr(Function,Object,List)
public static Object foldr1(Function p_function,
Object[] p_array)
throws FunctionException
foldr1(Function,List).
FunctionExceptionfoldr1(Function,List).
public static List scanl(Function p_function,
Object p_start,
List p_list)
throws FunctionException
p_function - the function used for scanning.p_start - the starting parameter to the function.p_list - the list of elements to be scanned.
FunctionException - thrown if the function is null.
public static List scanl(Function p_function,
Object p_start,
Object[] p_array)
throws FunctionException
scanl(Function,Object,List).
FunctionExceptionscanl(Function,Object,List).
public static List scanl1(Function p_function,
List p_list)
throws FunctionException
FunctionExceptionscanl(Function,Object,List).
public static List scanl1(Function p_function,
Object[] p_array)
throws FunctionException
scanl1(Function,List).
FunctionExceptionscanl1(Function,List).
public static List scanr(Function p_function,
Object p_start,
List p_list)
throws FunctionException
p_function - the function used for scanning.p_start - the starting parameter to the function.p_list - the list of elements to be scanned.
FunctionException - thrown if the function is null.
public static List scanr(Function p_function,
Object p_start,
Object[] p_array)
throws FunctionException
scanr(Function,Object,List).
FunctionExceptionscanr(Function,Object,List).
public static List scanr1(Function p_function,
List p_list)
throws FunctionException
FunctionExceptionscanr(Function,Object,List).
public static List scanr1(Function p_function,
Object[] p_array)
throws FunctionException
scanr1(Function,List).
FunctionExceptionscanr1(Function,List).
public static boolean and(List p_list)
throws FunctionException
&&ing the Boolean
values in the given list. Thus if at least one value is false,
the result will be false.
p_list - the list of Boolean values to be
&&ed.
true if all the values of the list are
true; false otherwise.
FunctionException - thrown if not all elements of the list are of
Boolean type.
public static boolean and(Boolean[] p_array)
throws FunctionException
&&ing the Boolean
values in the given array. The array is converted to a list and
and(List) is called.
FunctionExceptionand(List).
public static boolean and(boolean[] p_array)
throws FunctionException
&&ing the boolean
values in the given array. The array is converted to a list and
and(List) is called.
FunctionExceptionand(List).
public static boolean all(Function p_function,
List p_list)
InstanceFunction for which the object has not been
specified, and return a boolean or Boolean
result.
p_function - the function that represents the condition to be met.p_list - the list of elements to be tested against the condition.
true if all of the elements of the list satisfy the
condition; false otherwise.
public static boolean all(Function p_function,
Object[] p_array)
all(Function,List).
all(Function,List).
public static boolean or(List p_list)
throws FunctionException
||ing the Boolean values
in the given list. Thus if at least one value is true, the result
will be true.
p_list - the list of Boolean values to be
||ed.
false if all the values of the list are
false; true otherwise.
FunctionException
public static boolean or(Boolean[] p_array)
throws FunctionException
||ing the Boolean values
in the given array. The array is converted to a list and or(List) is
called.
FunctionExceptionor(List).
public static boolean or(boolean[] p_array)
throws FunctionException
||ing the boolean values in
the given array. The array is converted to a list and or(List) is
called.
FunctionExceptionor(List).
public static boolean any(Function p_function,
List p_list)
InstanceFunction for which the object has not been
specified, and return a boolean or Boolean
result.
p_function - the function that represents the condition to be met.p_list - the list of elements to be tested against the condition.
true if at least one element of the list satisfies the
condition; false otherwise.
public static boolean any(Function p_function,
Object[] p_array)
any(Function,List).
any(Function,List).
public static List intersperse(Object p_object,
List p_list)
intersperse(", ", ["a","b","c","d"]) returns
["a",", ","b",", ""c",", ""d"]. Using concat(List) on
this last result will return "a, b, c, d".
p_object - the object to be interspersed into the list.p_list - the list to be interspersed.
public static List intersperse(Object p_object,
Object[] p_array)
intersperse(Object,List) is called.
intersperse(Object,List).public static String concat(List p_list)
p_list - the list of objects to be concatenated.
public static String concat(Object[] p_objects)
concat(List).
concat(List).public static int sum(List p_list)
List of Numbers, returns the sum
of their int values.
p_list - the list of Numbers to add up.
int values of the list.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||