With proper indenting it looks a lot better and doesn't need "end foreach"-like comments (if you ever feel you need comments that say what you're ending, fix your style instead!!).

Rule of thumb: always let the thing that causes the indentation be the last on a line. Another rule of thumb: align closing parens/brackets with the indentation of the line that caused indentation.
So don't use:

somefunction( foo(bar, "Hello World", ) // end of foo ); // end of somefunction
But instead use:
somefunction( foo( bar, "Hello World", ) );
You may or may not like it, but I find it much better readable.

import java.util.ArrayList; import java.util.Comparator; import java.util.List; import perlforjava.control.Looper; import perlforjava.functions.Arrays; import perlforjava.functions.Mapper; import perlforjava.lang.Block; public class TheSchwartz { public static void main(String[] args) { // Create a new List of values to sort ArrayList in = new ArrayList(); in.add("foobarbazquux"); in.add("foo"); in.add("foobarbaz"); in.add("foobar"); /* Sort based upon length of string by mapping each string to a List where the first element is the string and the second + is its length. Sort on the latter and return a new List of str +ings. */ List out = Mapper.map( new Block() { // pull the first element from each List and return it protected Object process(Object elem) { return ((List)elem).get(0); } }, Arrays.sort( // run the comparison of Integer values to determine o +rder new Comparator() { public int compare(Object a, Object b) { return ((Integer)((List)a).get(1)). compareTo(((Integer)((List)b).get(1))); } }, // map each string into an ArrayList of string => leng +th Mapper.map( new Block() { protected Object process(Object elem) { ArrayList temp = new ArrayList(); temp.add(elem); temp.add(new Integer(((String)elem).length +())); return temp; } }, in ) ) ); // Loop over the results to view them Looper.foreach( out, new Block() { protected Object process(Object elem) { System.out.println("ELEM " + elem.toString()); return null; } } ); } }

But indeed, Perl is much nicer towards programmers anyway.

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }


In reply to Re: (OT) The Schwartzian Transform in Java by Juerd
in thread (OT) The Schwartzian Transform in Java by djantzen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.