Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing simple functional-like paradigm in Java collections and type casting
    text
    copied!<p>I've recently started working in Scala and that was my first real contact with a functional paradigm. Although I'm a big fan of Java and, I admit, it lacks functional paradigm sometimes. </p> <p>That's why I recently started a a mini pet-project to see if such thing, to a degree, could be achieved in Java. </p> <p>I started with a simple modification of an array list, and this is what I have so far:</p> <hr> <p>An interface any collection needs to implement in order to offer applying functions to it's elements:</p> <pre><code>public interface Functionalizable&lt;E&gt; { public Collection&lt;E&gt; apply(Function&lt;E&gt; f); } </code></pre> <p>An interface that defines a method for applying the function on a single element:</p> <pre><code>public interface Function&lt;E&gt; { public E apply(E e); } </code></pre> <p>A concrete class backed by an array list that allows applying functions on it's elements:</p> <pre><code>public class FunctionArrayList&lt;E&gt; implements List&lt;E&gt;, Functionalizable&lt;E&gt; { private List&lt;E&gt; list; //implemented methods from `List` interface and ctors @Override public List&lt;E&gt; apply(Function&lt;E&gt; f) { List&lt;E&gt; applied = new FunctionArrayList&lt;&gt;(this.list.size()); for (E e : this.list) { applied.add(f.apply(e)); } return applied; } } </code></pre> <p>I've written s small test method for Integer and it works OK: </p> <p>Code:</p> <pre><code> List&lt;Integer&gt; listOfIntegersBefore = new FunctionArrayList&lt;&gt;(); listOfIntegersBefore.add(-1); listOfIntegersBefore.add(0); listOfIntegersBefore.add(1); listOfIntegersBefore.add(2); listOfIntegersBefore.add(3); listOfIntegersBefore.add(4); System.out.println("Before&lt;Integer&gt;: " + listOfIntegersBefore.toString()); List&lt;Integer&gt; listOfIntegersAfter = ((FunctionArrayList&lt;Integer&gt;) listOfIntegersBefore).apply(new Function&lt;Integer&gt;() { @Override public Integer apply(Integer e) { return (e + 1); } }); System.out.println("After&lt;Integer&gt; : " + listOfIntegersAfter.toString()); </code></pre> <p>Output:</p> <pre><code>Before&lt;Integer&gt;: [-1, 0, 1, 2, 3, 4] After&lt;Integer&gt; : [0, 1, 2, 3, 4, 5] </code></pre> <hr> <p>However, when I try a tad more complex thing with List, I end up with much type-casting, which I don't like (and I'd like to avoid it as much as possible).<br> Code:</p> <pre><code> List&lt;List&lt;Integer&gt;&gt; listOfListOfIntegersBefore = new FunctionArrayList&lt;&gt;(); List&lt;Integer&gt; temp = new FunctionArrayList&lt;&gt;(); temp.add(1); listOfListOfIntegersBefore.add(temp); temp = new FunctionArrayList&lt;&gt;(); temp.add(1); temp.add(2); listOfListOfIntegersBefore.add(temp); temp = new FunctionArrayList&lt;&gt;(); temp.add(1); temp.add(2); temp.add(3); listOfListOfIntegersBefore.add(temp); temp = new FunctionArrayList&lt;&gt;(); temp.add(1); temp.add(2); temp.add(3); temp.add(4); listOfListOfIntegersBefore.add(temp); List&lt;List&lt;Integer&gt;&gt; listOfListOfIntegersAfter = (List&lt;List&lt;Integer&gt;&gt;) ((Functionalizable&lt;List&lt;Integer&gt;&gt;) listOfListOfIntegersBefore).apply(new Function&lt;List&lt;Integer&gt;&gt;() { @Override public List&lt;Integer&gt; apply(List&lt;Integer&gt; e) { List&lt;Integer&gt; list = new FunctionArrayList&lt;&gt;(e); return ((FunctionArrayList&lt;Integer&gt;) list).apply(new Function&lt;Integer&gt;() { @Override public Integer apply(Integer e) { return (e + 1); } }); } }); System.out.println("Before&lt;List&lt;Integer&gt;&gt;: " + listOfListOfIntegersBefore); System.out.println("After&lt;List&lt;Integer&gt;&gt; : " + listOfListOfIntegersAfter); </code></pre> <p>Output:</p> <pre><code>Before&lt;List&lt;Integer&gt;&gt;: [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]] After&lt;List&lt;Integer&gt;&gt; : [[2], [2, 3], [2, 3, 4], [2, 3, 4, 5]] </code></pre> <hr> <p>As I have already mentioned, I would like to avoid casting. Plus, Eclipse warns me about <code>Type safety: Unchecked cast from List&lt;List&lt;Integer&gt;&gt; to Functionalizable&lt;List&lt;Integer&gt;&gt;</code> on this line:</p> <pre><code>List&lt;List&lt;Integer&gt;&gt; listOfListOfIntegersAfter = (List&lt;List&lt;Integer&gt;&gt;) ((Functionalizable&lt;List&lt;Integer&gt;&gt;) listOfListOfIntegersBefore).apply(new Function&lt;List&lt;Integer&gt;&gt;() { ... } </code></pre> <p>Is there an elegant way to accomplish that?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload