Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 3.82 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Implementing simple functional-like paradigm in Java collections and type casting
  2. public interface Functionalizable<E> {
  3.     public Collection<E> apply(Function<E> f);
  4. }
  5.        
  6. public interface Function<E> {
  7.     public E apply(E e);
  8. }
  9.        
  10. public class FunctionArrayList<E> implements List<E>, Functionalizable<E> {
  11.     private List<E> list;
  12.  
  13.     //implemented methods from `List` interface and ctors
  14.  
  15.     @Override
  16.     public List<E> apply(Function<E> f) {
  17.  
  18.         List<E> applied = new FunctionArrayList<>(this.list.size());
  19.  
  20.         for (E e : this.list) {
  21.             applied.add(f.apply(e));
  22.         }
  23.  
  24.         return applied;
  25.     }
  26. }
  27.        
  28. List<Integer> listOfIntegersBefore = new FunctionArrayList<>();
  29.     listOfIntegersBefore.add(-1);
  30.     listOfIntegersBefore.add(0);
  31.     listOfIntegersBefore.add(1);
  32.     listOfIntegersBefore.add(2);
  33.     listOfIntegersBefore.add(3);
  34.     listOfIntegersBefore.add(4);
  35.  
  36.     System.out.println("Before<Integer>: " + listOfIntegersBefore.toString());
  37.  
  38.     List<Integer> listOfIntegersAfter = ((FunctionArrayList<Integer>) listOfIntegersBefore).apply(new Function<Integer>() {
  39.  
  40.         @Override
  41.         public Integer apply(Integer e) {
  42.             return (e + 1);
  43.         }
  44.     });
  45.  
  46.     System.out.println("After<Integer> : " + listOfIntegersAfter.toString());
  47.        
  48. Before<Integer>: [-1, 0, 1, 2, 3, 4]
  49. After<Integer> : [0, 1, 2, 3, 4, 5]
  50.        
  51. List<List<Integer>> listOfListOfIntegersBefore = new FunctionArrayList<>();
  52.  
  53.     List<Integer> temp = new FunctionArrayList<>();
  54.     temp.add(1);
  55.     listOfListOfIntegersBefore.add(temp);
  56.  
  57.     temp = new FunctionArrayList<>();
  58.     temp.add(1);
  59.     temp.add(2);
  60.     listOfListOfIntegersBefore.add(temp);
  61.  
  62.     temp = new FunctionArrayList<>();
  63.     temp.add(1);
  64.     temp.add(2);
  65.     temp.add(3);
  66.     listOfListOfIntegersBefore.add(temp);
  67.  
  68.     temp = new FunctionArrayList<>();
  69.     temp.add(1);
  70.     temp.add(2);
  71.     temp.add(3);
  72.     temp.add(4);
  73.     listOfListOfIntegersBefore.add(temp);
  74.  
  75.     List<List<Integer>> listOfListOfIntegersAfter = (List<List<Integer>>) ((Functionalizable<List<Integer>>) listOfListOfIntegersBefore).apply(new Function<List<Integer>>() {
  76.  
  77.         @Override
  78.         public List<Integer> apply(List<Integer> e) {
  79.             List<Integer> list = new FunctionArrayList<>(e);
  80.  
  81.             return ((FunctionArrayList<Integer>) list).apply(new Function<Integer>() {
  82.  
  83.                 @Override
  84.                 public Integer apply(Integer e) {
  85.                     return (e + 1);
  86.                 }
  87.             });
  88.         }
  89.     });
  90.     System.out.println("Before<List<Integer>>: " + listOfListOfIntegersBefore);
  91.     System.out.println("After<List<Integer>> : " + listOfListOfIntegersAfter);
  92.        
  93. Before<List<Integer>>: [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]
  94. After<List<Integer>> : [[2], [2, 3], [2, 3, 4], [2, 3, 4, 5]]
  95.        
  96. List<List<Integer>> listOfListOfIntegersAfter = (List<List<Integer>>) ((Functionalizable<List<Integer>>) listOfListOfIntegersBefore).apply(new Function<List<Integer>>() {
  97.     ...
  98. }
  99.        
  100. List<List<Integer>> listOfListOfIntegersBefore = new FunctionArrayList<>();
  101. [...]
  102. ((FunctionArrayList<Integer>) listOfIntegersBefore).apply
  103.        
  104. public interface F<S, T> { T apply(S s); }
  105.  
  106.  
  107. public final class FunctionalStuff {
  108.   private FunctionalStuff() {}
  109.  
  110.  
  111.   public <S, T> static List<T> map(Collection<? extends S> collection,
  112.                                    F<? super S, ? extends T> func) {
  113.     final List<T> result = new ArrayList<T>(collection.size());
  114.     for (S source : collection)
  115.       result.add(func.apply(source));
  116.     return result;
  117.   }
  118.  
  119.  
  120.   public <S, T> static List<S> filter(Collection<? extends S> collection,
  121.                                       F<? super S, Boolean> predicate) {
  122.     final List<T> result = new ArrayList<T>(collection.size());
  123.     for (S source : collection)
  124.       if (predicate.apply(source))
  125.         result.add(source);
  126.     return result;
  127.   }
  128.  
  129.   // etc etc.
  130. }