|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjedi.functional.FunctionalPrimitives
public class FunctionalPrimitives
I provide operations of the kind found in Functional Programming languages. This allows you to remove a great deal of clutter from production code. Ideally, you will never need to write another 'for' loop again, and a great deal of explicit conditional logic should be removable as well.
Generally, functions that transform collections (using functors or whatever) preserve the iteration order of the given collection in the result.
| Method Summary | ||
|---|---|---|
static
|
addAll(Collection<T> list,
Iterable<? extends T> iterable)
Add all the elements of an iterable to a collection. |
|
static
|
append(Collection<? extends T>... collections)
Append all of the elements in all of the given collections
into one list. |
|
static
|
append(Iterable<? extends Iterable<? extends T>> collections)
Append all of the elements in all of the given collections
into one list. |
|
static
|
collect(Iterable<T> items,
Functor<? super T,R> functor)
Apply functor to each element of items and
return the list of results. |
|
static
|
collect(T[] items,
Functor<? super T,R> functor)
Apply functor to each element of items and
return the list of results. |
|
static
|
drop(int n,
Iterable<T> items)
Get all but the first n elements of items |
|
static
|
dropRight(int n,
Iterable<T> items)
Get all but the last n elements of items. |
|
static
|
first(Iterable<? extends T> collection)
An alias for head |
|
static
|
firstOption(Iterable<T> items)
Get the first item (in iteration order) from a collection as an Option. |
|
static
|
flatten(Iterable<T> items,
Functor<? super T,? extends Iterable<? extends R>> functor)
Suppose there is a collection of items (c1, c2, c3), each of which contains a collection i.e. (c1 = (c1_1, c1_2, ...), c2=(c2_1, c2_2, ...). |
|
static
|
fold(I initialValue,
Iterable<T> collection,
Functor2<R,? super T,R> functor2)
Fold passes each item of a collection with an accumulated value to a functor. |
|
static
|
foldPowerset(I initialValue,
Collection<T> all,
Functor2<R,List<? super T>,R> functor2)
Fold over all powersets of the give collection, in no particular order. |
|
static
|
forEach(Iterable<T> items,
Command<? super T> command)
Iterate over a collection of items applying the given
command to each |
|
static
|
group(Iterable<V> toGroup,
Functor<? super V,K> keyFunctor)
A synonym for partition(Iterable, Functor) Group the elements of
a collection such that all elements that are mapped to the same value by
a given keyFunctor are in the same group. |
|
static
|
hasItems(Iterable<T> iterable)
|
|
static
|
head(Iterable<T> items)
Get the first item (in iteration order) from a collection. |
|
static
|
headOption(Iterable<T> items)
Get the first item (in iteration order) from a collection as an Option. |
|
static
|
headOrDefaultIfEmpty(Iterable<? extends T> items,
T defaultValue)
Get the first item (in iteration order) from a collection or defaultValue (which may be null) if the collection is empty. |
|
static
|
headOrNullIfEmpty(Iterable<T> items)
Get the first item (in iteration order) from a collection or null if the collection is empty. |
|
static
|
inject(I initialValue,
Iterable<T> collection,
Functor2<R,? super T,R> functor2)
An alias for fold. |
|
static
|
isEmpty(Iterable<T> iterable)
|
|
static String |
join(Iterable<?> items)
Join, with default delimiter (empty string) |
|
static String |
join(Iterable<?> items,
String delimiter)
|
|
static String |
join(Object[] items,
String delimiter)
Returns a string created by converting each element of an array to a string, separated by delimiter. |
|
static
|
last(Iterable<? extends T> items)
Get the last item (in iteration order) from a collection. |
|
static
|
lastOption(Collection<? extends T> items)
Get the last item (in iteration order) from a collection as an Option. |
|
static
|
lastOrDefaultIfEmpty(Iterable<? extends T> items,
T defaultValue)
Get the last item (in iteration order) from a collection or defaultValue (which may be null) if the collection is empty. |
|
static
|
lastOrNullIfEmpty(Iterable<? extends T> items)
|
|
static
|
listTabulate(int n,
Functor<Integer,R> functor)
Returns an n-element list. |
|
static
|
longest(Iterable<T> collections)
Find the biggest collection in a collection of collections |
|
static
|
makeList(int n,
T fill)
Returns an n-element list, whose elements are all the value fill. |
|
static
|
only(Collection<T> items)
Return the one and only item in the given collection. |
|
static
|
partition(Iterable<T> items,
Filter<T> filter)
Partition a collection of items into two sublists using the given filter. |
|
static
|
partition(Iterable<V> toPartition,
Functor<? super V,K> keyFunctor)
A synonym for group(Iterable, Functor) Partition the elements of
a collection such that all elements that are mapped to the same value by
a given keyFunctor are in the same partition. |
|
static
|
pop(Iterable<T> items)
Removes the last item (in iteration order) from a collection. |
|
static
|
popOption(Iterable<T> items)
Removes the last item (in iteration order) from a collection as an Option. |
|
static
|
powerset(Collection<T> all)
Return the set of all subsets of the provided collection. |
|
static
|
powerset(Collection<T> all,
Command<List<? super T>> command)
Iterate all powersets of the give collection, in no particular order. |
|
static
|
produce(Iterable<T> left,
Iterable<U> right,
Functor2<? super T,? super U,R> factory)
Create the Cartesian product of two collections, using a functor as a factory of objects to represent the pair-wise products. |
|
static
|
reject(Iterable<T> items,
Filter<? super T> filter)
Filter a collection of items, returning only those that do
not match a given filter, this is the inverse of select. |
|
static
|
reverse(Iterable<T> items)
|
|
static
|
select(Iterable<T> items,
Filter<? super T> filter)
Filter a collection of items, returning only those that
match a given filter, this is the inverse of reject. |
|
static
|
sequence(Command<? super T>... commands)
Produce a single command that executes each of the given commands in sequence |
|
static
|
shortest(Iterable<T> collections)
Find the shortest list in a list of lists |
|
static List |
slice(int n,
Iterable<List> items)
Create a list from the nth elements of the given lists. |
|
static List<String> |
split(String item,
String regex)
Create a list of string by splitting on a regex. |
|
static
|
tabulate(Iterable<T> list,
int length)
|
|
static
|
tail(Iterable<T> items)
Get all item's (in iteration order) from a collection except the first. |
|
static
|
take(int n,
Iterable<T> items)
Get the first n elements of items. |
|
static
|
takeMiddle(int start,
int n,
Iterable<T> items)
Get n middle elements of an Iterable. |
|
static
|
takeRight(int n,
Iterable<T> items)
Get the last n elements of an Iterable. |
|
static List |
zip(Iterable<List> lists)
Zip interleaves a collection of lists. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static <T> List<T> append(Iterable<? extends Iterable<? extends T>> collections)
collections
into one list. All of the elements of the first item in
collections are appended first, then the items in the second
collection, etc.
append(Collection...)public static <T> List<T> append(Collection<? extends T>... collections)
collections
into one list. All of the elements of the first item in
collections are appended first, then the items in the second
collection, etc. Equivalent to append(list(collections))
append(Iterable)
public static <T,R> List<R> collect(Iterable<T> items,
Functor<? super T,R> functor)
functor to each element of items and
return the list of results.
collect(Object[],Functor)
public static <T,R> List<R> collect(T[] items,
Functor<? super T,R> functor)
functor to each element of items and
return the list of results. The iteration order of items is
preserved in the returned list.
Equivalent to collect(functor, asList(items))
collect(Iterable, Functor)
public static <T> List<T> drop(int n,
Iterable<T> items)
items. See SRFI-1
public static <T> List<T> dropRight(int n,
Iterable<T> items)
items. See SRFI-1
public static <T,R> List<R> flatten(Iterable<T> items,
Functor<? super T,? extends Iterable<? extends R>> functor)
items - The collection of items containing the collection of leavesfunctor - Given an element of the 'top' collection, this can obtain the
collection of 'leaf' objects to accumulate
public static <T> Collection<T> addAll(Collection<T> list,
Iterable<? extends T> iterable)
public static <T,R,I extends R> R fold(I initialValue,
Iterable<T> collection,
Functor2<R,? super T,R> functor2)
Functor2<Integer, Integer> summer = new Functor2<Integer, Integer>() {
public Integer execute(Integer accumulator, Integer value) {
return accumulator + value;
}
};
fold(10, summer, list(1, 2, 3, 4)) will return 20 (initial value of 10 + the sum of 1 .. 4)
For a more comprehensive description, see SRFI-1
public static <T> Iterable<T> forEach(Iterable<T> items,
Command<? super T> command)
items applying the given
command to each
public static <K,V> Map<K,List<V>> group(Iterable<V> toGroup,
Functor<? super V,K> keyFunctor)
partition(Iterable, Functor) Group the elements of
a collection such that all elements that are mapped to the same value by
a given keyFunctor are in the same group. The groups are
returned as a map in which each value is a collection of values in one
group and whose key is the value returned by the keyFunctor
for all items in the group.
public static <K,V> Map<K,List<V>> partition(Iterable<V> toPartition,
Functor<? super V,K> keyFunctor)
group(Iterable, Functor) Partition the elements of
a collection such that all elements that are mapped to the same value by
a given keyFunctor are in the same partition. The groups are
returned as a map in which each value is a collection of values in one
partition and whose key is the value returned by the
keyFunctor for all items in the group.
public static <T> T head(Iterable<T> items)
AssertionError will be thrown.
AssertionError - if the collection is emptyonly(Collection),
headOrNullIfEmpty(Iterable),
headOrDefaultIfEmpty(Iterable, Object)
public static <T> T headOrDefaultIfEmpty(Iterable<? extends T> items,
T defaultValue)
defaultValue (which may be null) if the collection is empty.
defaultValue if
the collection is emptyonly(Collection),
head(Iterable),
headOrNullIfEmpty(Iterable)public static <T> T headOrNullIfEmpty(Iterable<T> items)
null if the collection is empty.
only(Collection),
head(Iterable),
headOrDefaultIfEmpty(Iterable, Object)public static <T> Option<T> headOption(Iterable<T> items)
Option.
items -
Some or None if the collection is empty.public static <T> T last(Iterable<? extends T> items)
AssertionError will be thrown.
AssertionError - if the collection is emptyonly(Collection),
lastOrNullIfEmpty(Iterable),
lastOrDefaultIfEmpty(Iterable, Object)
public static <T> T lastOrDefaultIfEmpty(Iterable<? extends T> items,
T defaultValue)
defaultValue (which may be null) if the collection is empty.
defaultValue if
the collection is emptyonly(Collection),
last(Iterable),
lastOrNullIfEmpty(Iterable)public static <T> T lastOrNullIfEmpty(Iterable<? extends T> items)
public static <T> Option<T> lastOption(Collection<? extends T> items)
Option.
Some or None if the collection
is empty.public static <T> T first(Iterable<? extends T> collection)
head(Iterable)public static <T> Option<T> firstOption(Iterable<T> items)
Option.
items -
Some or None if the collection is empty.
public static <T,R,I extends R> R inject(I initialValue,
Iterable<T> collection,
Functor2<R,? super T,R> functor2)
fold(Object, Iterable, Functor2)public static String join(Iterable<?> items)
join(Object[], String)
public static String join(Iterable<?> items,
String delimiter)
public static String join(Object[] items,
String delimiter)
public static <R> List<R> listTabulate(int n,
Functor<Integer,R> functor)
n - the length of the listfunctor - the functor taking an integer and returning an public static <U,T extends Collection<? extends U>> T longest(Iterable<T> collections)
collections -
public static <T> List<T> makeList(int n,
T fill)
fill. For a more comprehensive description, see SRFI-1
public static <T> T only(Collection<T> items)
AssertionError - if the collection contains less or more than one itemhead(Iterable),
headOrNullIfEmpty(Iterable)
public static <T,U,R> List<R> produce(Iterable<T> left,
Iterable<U> right,
Functor2<? super T,? super U,R> factory)
functor as a factory of objects to represent the pair-wise products.
public static <T> List<T> reject(Iterable<T> items,
Filter<? super T> filter)
items, returning only those that do
not match a given filter, this is the inverse of select.
select(Iterable, Filter)public static <T> List<T> reverse(Iterable<T> items)
public static <T> List<T> select(Iterable<T> items,
Filter<? super T> filter)
items, returning only those that
match a given filter, this is the inverse of reject.
reject(Iterable, Filter)public static <T> Command<T> sequence(Command<? super T>... commands)
commands in sequence
public static <U,T extends Collection<? extends U>> T shortest(Iterable<T> collections)
collections -
public static <T> boolean hasItems(Iterable<T> iterable)
public static <T> boolean isEmpty(Iterable<T> iterable)
public static List slice(int n,
Iterable<List> items)
public static List<String> split(String item,
String regex)
String.split(java.lang.String, int)public static <T> List<T> tail(Iterable<T> items)
AssertionError will be thrown.
AssertionError - if the collection contains less or more than one itemonly(Collection),
headOrNullIfEmpty(Iterable),
headOrDefaultIfEmpty(Iterable, Object)
public static <T> List<T> take(int n,
Iterable<T> items)
public static <T> List<T> takeRight(int n,
Iterable<T> items)
public static <T> List<T> takeMiddle(int start,
int n,
Iterable<T> items)
public static List zip(Iterable<List> lists)
lists -
public static <T> List<List<T>> tabulate(Iterable<T> list,
int length)
length.public static <T> T pop(Iterable<T> items)
AssertionError will be thrown.
Emulates Array.pop in Ruby.
AssertionError - if the collection contains less or more than one itemonly(Collection),
headOrNullIfEmpty(Iterable),
headOrDefaultIfEmpty(Iterable, Object)public static <T> Option<T> popOption(Iterable<T> items)
Option.
Some or None
if the list is empty.
public static <T> List<List<T>> partition(Iterable<T> items,
Filter<T> filter)
items -
public static <T> Set<List<T>> powerset(Collection<T> all)
For example, if all is the list ("x","y","z"), the return will be set containing 8 lists as follows:
See http://en.wikipedia.org/wiki/Power_set
powerset(Collection, Command),
foldPowerset(Object, Collection, Functor2)
public static <T> void powerset(Collection<T> all,
Command<List<? super T>> command)
powerset(Collection),
foldPowerset(Object, Collection, Functor2)
public static <T,R,I extends R> R foldPowerset(I initialValue,
Collection<T> all,
Functor2<R,List<? super T>,R> functor2)
powerset(Collection),
powerset(Collection, Command),
fold(Object, Iterable, Functor2)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||