title: Java as generic higher-order functional programming language
body: <p>I'm solving algorithmical problems, and i want to write custom functions and predicates that can be applied to collections. Recently, i started using Google Collections and it is just wonderful for this task. </p>
  
  <p>I want to use BigInteger and BigDecimal same way, as i would use any other numbers. Without pondering how it should be done, i decided to make an extra layer of abstraction(Class E). </p>
  
  <p>If it is unclear what I am trying to do, here is an example:</p>
  
  <pre><code>I.range(1,999).multiplication(I.range(1,999)).palindromes().max().echo(2); 
  </code></pre>
  
  <ol>
  <li>return collection of sequence 1:999 (x2)</li>
  <li>return 2 collections every item with method times() transform </li>
  <li>return collection of every item what passes palindromes filter </li>
  <li>return maximum element E&lt;?> of 3. result  </li>
  <li>return element E&lt;?> and invoke method toString with radix 2 (binary) and print it to screen</li>
  </ol>
  
  <p>Class E is defined as:</p>
  
  <pre><code>public class E&lt;T extends Number &amp; Comparable&lt;? super T&gt;&gt; extends Number implements Comparable&lt;E&lt;T&gt;&gt; {//...
  </code></pre>
  
  <p>Class C is defined as:</p>
  
  <pre><code>public class C&lt;T extends E&lt;NC&gt;, NC extends Number &amp; Comparable&lt;? super NC&gt;&gt; implements Collection&lt;T&gt; {
  </code></pre>
  
  <p>This is what i want to make working in class C.</p>
  
  <pre><code> public Collection&lt;T&gt; multiplication(T value) {
    return Collections2.transform(this, new Function&lt;T, T&gt;() {
     @Override
     public T apply(T in) {
      return in.times(value);
     }
    });
   }
  </code></pre>
  
  <p>Before i used following code, in class E</p>
  
  <pre><code> /** Multiplies 2 numbers */
   public E&lt;?&gt; times(E&lt;?&gt; elem) {
    if (this.value == null || elem.value == null) return E.Null();
    if (this.value instanceof Integer) {
     return E.with(I, this.intValue() * elem.intValue());
    } else if (this.value instanceof Long) {
     return E.with(L, this.longValue() * elem.longValue());
    } else if (this.value instanceof Float) {
     return E.with(F, this.floatValue() * elem.floatValue());
    } else if (this.value instanceof Double) {
     return E.with(D, this.doubleValue() * elem.doubleValue());
    } else if (this.value instanceof BigInteger) {
     return E.with(BI, this.BigIntegerValue().multiply(
      elem.BigIntegerValue()));
    } else if (this.value instanceof BigDecimal) { return E.with(BD,
     this.BigDecimalValue().multiply(elem.BigDecimalValue())); }
  
    return E.Null();
   }
  </code></pre>
  
  <p>What should i change, so that writing custom functions and predicates would involve minimal amount of <a href="http://www.urbandictionary.com/define.php?term=suckiness" rel="nofollow noreferrer">'suckiness'</a>.</p>
  
  <h2>Edit:</h2>
  
  <p>A well, changed C to:</p>
  
  <pre><code>public class C&lt;T extends E&lt;?&gt;&gt; extends ArrayList&lt;T&gt; {
  </code></pre>
  
  <p>And just suppressed generic wildcard casting warnings like</p>
  
  <pre><code>public Collection&lt;T&gt; multiplication(Collection&lt;T&gt; value) {
      C&lt;T&gt; result = new C&lt;T&gt;();
  
      for (T t : value)
          result.addAll(multiplication(t));
      return result;
  }
  
  public Collection&lt;T&gt; multiplication(final T value) {
      return Collections2.transform(this, new Function&lt;T, T&gt;() {
          @SuppressWarnings("unchecked")
          @Override
          public T apply(T in) {
              return (T) in.times(value);
          }
      });
  }
  </code></pre>
  
  <p>So if the types match, this works. </p>