- Implementing simple functional-like paradigm in Java collections and type casting
- public interface Functionalizable<E> {
- public Collection<E> apply(Function<E> f);
- }
- public interface Function<E> {
- public E apply(E e);
- }
- public class FunctionArrayList<E> implements List<E>, Functionalizable<E> {
- private List<E> list;
- //implemented methods from `List` interface and ctors
- @Override
- public List<E> apply(Function<E> f) {
- List<E> applied = new FunctionArrayList<>(this.list.size());
- for (E e : this.list) {
- applied.add(f.apply(e));
- }
- return applied;
- }
- }
- List<Integer> listOfIntegersBefore = new FunctionArrayList<>();
- listOfIntegersBefore.add(-1);
- listOfIntegersBefore.add(0);
- listOfIntegersBefore.add(1);
- listOfIntegersBefore.add(2);
- listOfIntegersBefore.add(3);
- listOfIntegersBefore.add(4);
- System.out.println("Before<Integer>: " + listOfIntegersBefore.toString());
- List<Integer> listOfIntegersAfter = ((FunctionArrayList<Integer>) listOfIntegersBefore).apply(new Function<Integer>() {
- @Override
- public Integer apply(Integer e) {
- return (e + 1);
- }
- });
- System.out.println("After<Integer> : " + listOfIntegersAfter.toString());
- Before<Integer>: [-1, 0, 1, 2, 3, 4]
- After<Integer> : [0, 1, 2, 3, 4, 5]
- List<List<Integer>> listOfListOfIntegersBefore = new FunctionArrayList<>();
- List<Integer> temp = new FunctionArrayList<>();
- temp.add(1);
- listOfListOfIntegersBefore.add(temp);
- temp = new FunctionArrayList<>();
- temp.add(1);
- temp.add(2);
- listOfListOfIntegersBefore.add(temp);
- temp = new FunctionArrayList<>();
- temp.add(1);
- temp.add(2);
- temp.add(3);
- listOfListOfIntegersBefore.add(temp);
- temp = new FunctionArrayList<>();
- temp.add(1);
- temp.add(2);
- temp.add(3);
- temp.add(4);
- listOfListOfIntegersBefore.add(temp);
- List<List<Integer>> listOfListOfIntegersAfter = (List<List<Integer>>) ((Functionalizable<List<Integer>>) listOfListOfIntegersBefore).apply(new Function<List<Integer>>() {
- @Override
- public List<Integer> apply(List<Integer> e) {
- List<Integer> list = new FunctionArrayList<>(e);
- return ((FunctionArrayList<Integer>) list).apply(new Function<Integer>() {
- @Override
- public Integer apply(Integer e) {
- return (e + 1);
- }
- });
- }
- });
- System.out.println("Before<List<Integer>>: " + listOfListOfIntegersBefore);
- System.out.println("After<List<Integer>> : " + listOfListOfIntegersAfter);
- Before<List<Integer>>: [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]
- After<List<Integer>> : [[2], [2, 3], [2, 3, 4], [2, 3, 4, 5]]
- List<List<Integer>> listOfListOfIntegersAfter = (List<List<Integer>>) ((Functionalizable<List<Integer>>) listOfListOfIntegersBefore).apply(new Function<List<Integer>>() {
- ...
- }
- List<List<Integer>> listOfListOfIntegersBefore = new FunctionArrayList<>();
- [...]
- ((FunctionArrayList<Integer>) listOfIntegersBefore).apply
- public interface F<S, T> { T apply(S s); }
- public final class FunctionalStuff {
- private FunctionalStuff() {}
- public <S, T> static List<T> map(Collection<? extends S> collection,
- F<? super S, ? extends T> func) {
- final List<T> result = new ArrayList<T>(collection.size());
- for (S source : collection)
- result.add(func.apply(source));
- return result;
- }
- public <S, T> static List<S> filter(Collection<? extends S> collection,
- F<? super S, Boolean> predicate) {
- final List<T> result = new ArrayList<T>(collection.size());
- for (S source : collection)
- if (predicate.apply(source))
- result.add(source);
- return result;
- }
- // etc etc.
- }