Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are some very useful 'higher order functions' which can do operations on lists using closures. Higher order functions are functions having 'function objects' as parameters.</p> <p>E.g. it is a very common operation to apply some transformation to every element in a list. This higher order function is commonly called 'map' or 'collect'. (See the *. spread operator of <a href="http://groovy.codehaus.org/Operators" rel="noreferrer" title="Groovy Operators">Groovy</a>).</p> <p>For example to square each element in a list without closures you would probably write:</p> <pre><code>List&lt;Integer&gt; squareInts(List&lt;Integer&gt; is){ List&lt;Integer&gt; result = new ArrayList&lt;Integer&gt;(is.size()); for (Integer i:is) result.add(i*i); return result; } </code></pre> <p>Using closures and map and the proposed syntax, you could write it like that:</p> <pre><code>is.map({Integer i =&gt; i*i}) </code></pre> <p>(There is a possible performance problem here regarding boxing of primitive types.)</p> <p>As explained by Pop Catalin there is another higher order function called 'select' or 'filter': It can be used to get all the elements in a list complying to some condition. For example:</p> <p>Instead of:</p> <pre><code>void onlyStringsWithMoreThan4Chars(List&lt;String&gt; strings){ List&lt;String&gt; result = new ArrayList&lt;String&gt;(str.size()); // should be enough for (String str:strings) if (str.length() &gt; 4) result.add(str); return result; } </code></pre> <p>Instead you could write something like</p> <pre><code>strings.select({String str =&gt; str.length() &gt; 4}); </code></pre> <p>using the proposal.</p> <p>You might look at the Groovy syntax, which is an extension of the Java language to support closures right now. See the chapter on collections of the <a href="http://groovy.codehaus.org/Collections" rel="noreferrer" title="Groovy User Guide">Groovy User Guide</a> for more examples what to do with closures.</p> <p>A remark:</p> <p>There is perhaps some clarification needed regarding the term 'closure'. What I've shown above are strictly spoken no closures. They are just 'function objects'. A closure is everything which can capture - or 'close over' - the (lexical) context of the code surrounding it. In that sense there are closures in Java right now, i.e. anonymous classes:</p> <pre><code>Runnable createStringPrintingRunnable(final String str){ return new Runnable(){ public void run(){ System.out.println(str); // this accesses a variable from an outer scope } }; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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