Perhaps a better example of scala making life easier.  Here I'm use "for
expressions" along with "operator syntax" and "lambda expresisons" on the
Scala XML dom to parse out all link URLs from a web document that end with
rpm

val urls = for {  link <-   webpageDom \\ "a"
                     location <-  link.attribute("href").map(_.text)
                     if location.text.endsWith("rpm")
               } yield location.text


Here's similar java (but using a different path)...

List<String> urls = new ArrayList<String>();
NodeList links = webpageDom.getElementsByTagName("a");
for(int i =0; i < links.getLength(); i++) {
    Node link = links.item(i);
    if(link instanceof Element) {
          Element linkAsElement = (Element)link;
          if(linkAsElement.hasAttribute("href") &&
linkAsElement.getAttribute("href").endsWith("rpm")) {
              urls.add(linkAsElement.getAttribute("href");
          }
    }
}

Groovy provides an "XmlParser" for nicer syntax as well....  However, I'll
let a Groovy expert show the simplified code for this (I'm pretty sure it's
close to a one liner using a depthFirst call with a closure filter.


Lastly, here's a JavaScript example (using jQuery):

var links = [];
$('a').each( function(idx, link) {
                 if(link.href && link.href.match(/.*rpm/)) {
                      links.push(link.href);
                 }
});
//Note we are not using map because we want to filter based on link ending!


Anyway, on to the point:   Which of these has the least signal to noise
ratio?  Which one is the clearest?  I leave that for you to decide.


I also want to point out that this expressiveness (of other languages vs.
java langauges) is not limited to XML parsing.  I would also be remiss if
not mentioning that I neglect to include a C version of the XML code because
I just didn't feel like taking the time to write one, and then make sure it
compiled/ran.  I'd love to see a JavaFX script version of this code, as I
have a sinking suspicision it will still have higher SNR than Java.

-Josh

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to