Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hi All. The following is a simple but demonstrative example of the Schwartzian Transform written in Java. It should compile and run with the latest version of Perl Idioms for Java. Enjoy!

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 = Block() { protected Object process(Object elem) { return ((List)elem).get(0); } // pull the first element from each List and return it }, Arrays.sort(new Comparator() { public int compare(Object a, Object b) { return ((Integer)((List)a).get(1)). compareTo(((Integer)((List)b).get(1))); } // run the comparison of Integer values to determin +e order }, Block() { protected Object process(Object elem) { ArrayList temp = new ArrayList(); temp.add(elem); temp.add(new Integer(((String)elem).length())); return temp; } // map each string into an ArrayList of string +=> length }, in) // end second map ) // end sort ); // end first map // 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; } } ); // end foreach } }

The equivalent Perl code is rather more brief, demonstrating the claim all programs will eventually be reduced to Perl one-liners :^p Update: added parentheses, though not required, to better see the similarities between implementations1.

use strict; use warnings; my @in = qw/foobarbazquux foo foobarbaz foobar/; my @out = map( { $_->[0] } sort( { $a->[1] <=> $b->[1] } map( { [$_, length($_)] } @in ) ) ); foreach (@out) { print "$_\n"; }

To be fair however, the verbosity of the Java code is a requirement of (at least) its strong typing which necessitates all that casting, and the prohibition against method overloading on the basis of method return value. Sloppier import statements can also reduce the line count but I've opted for clarity. And of course the biggest reason is the lack of user defined blocks except through anonymous inner classes.

1. The original perl code was: my @out = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, length($_)] } @in;

"The dead do not recognize context" -- Kai, Lexx

In reply to (OT) The Schwartzian Transform in Java by djantzen

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2023-10-04 07:20 GMT
Find Nodes?
    Voting Booth?

    No recent polls found