Posts Tagged closures

Using Scala to update LiveJournal tags – Part I

Some days ago I started to use the Scala programming language to update my Livejournal tags using its XML-RPC protocol reference. First I had to check if some tags of mine were entered wrong, so I’ve done this Scala program to list all of them:

   1:import org.apache.xmlrpc.client.XmlRpcClient;
   2:import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
   3:import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;
   4:import java.net.URL;
   5:import java.util.Map
   6:import java.util.HashMap
   7:import scala.collection.immutable.TreeSet
   8:
   9:object LJListTag {
  10:     def main(args: Array[String]) {
  11:         val config = new XmlRpcClientConfigImpl()
  12:         config.setEnabledForExtensions(true);
  13:         config.setServerURL(new URL("http://www.livejournal.com/interface/xmlrpc"))
  14:         val client = new XmlRpcClient()
  15:         client.setConfig(config)
  16:         val params = new HashMap[String, String]
  17:         params.put("username", "user")
  18:         params.put("password", "password")
  19:         var paramsToServer = new Array[Object](1)
  20:         paramsToServer(0) = params
  21:         val results = client.execute("LJ.XMLRPC.getusertags", paramsToServer).asInstanceOf[Map[String, String]];
  22:         printEachTag(results)
  23:     }
  24:    
  25:     def printEachTag(results: Map[String, String]) {
  26:        var allTags = new TreeSet[String]
  27:        val iterator = results.values().iterator()
  28:           while(iterator.hasNext()) {
  29:             val resultFromRPCData = iterator.next().asInstanceOf[Array[Any]]
  30:             resultFromRPCData.foreach(singleResult => allTags += extractTag(singleResult))
  31:           }
  32:        allTags.foreach(tag => println(tag))
  33:     }
  34:    
  35:    def extractTag(singleResult: Any): String = {
  36:        val tag = singleResult.asInstanceOf[HashMap[String, String]]
  37:        return tag.get("name")
  38:    }
  39:}

Just fill your user and password to have all of your LiveJournal tags printed on the standard output. The experience was so amazing, since you can use all the Java libraries (Scala is fully interoperable with Java and runs on top of the JVM). I used a TreeSet because I wanted print my tags sorted according its alphabetical order. I’m continuously studying Scala and its API, so the code above doesn’t use any advanced Scala constructs. If you have any suggestion about the code or how to use better the Scala constructs, post your comments here. It will be all appreciated.

Tags: , , , , ,

“Salute #{@Ruby}!”

Some times ago I had to parse a XML messages’ file to produce some i18n properties files.

I decided to try it with Ruby, mainly because of two reasons:

  1. I’m continuosly exploring some dynamically-typed languages, like Python and Ruby
  2. I wanted to try the conciseness of programming with closures

So, I used the REXML API to process the XML file. The result code looks like the one below:

   1:require 'rexml/document'
   2:include REXML
   3:englishFile = File.new('englishFileName', 'w+')
   4:spanishFile = File.new('spanishFileName', 'w+')
   5:portugueseFile = File.new('portugueseFileName', 'w+')
   6:errorMessagesFile = File.new("errorMessages.xml")
   7:document = Document.new(file)
   8:root = document.root
   9:root.each_element("Property") do |propertyTag|
  10:  id = propertyTag.attributes['id']   
  11:  propertyTag.each_element("element") do |elementTag|
  12:      elementAttr = elementTag.attributes['otherTag']
  13:      error = elementTag.text == nil ? "" : "#{id} = #{elementTag.text}\n"
  14:      if elementAttr = "pt"
  15:          portugueseFile << error
  16:      elsif elementAttr == "es"
  17:          spanishFile << error
  18:      else 
  19:          portugueseFile << error
  20:      end
  21:  end
  22:end
  23:errorMessagesFile.close()
  24:englishFile.close()
  25:spanishFile.close()
  26:portugueseFile.close()
  27:  

I like to solve this kind of tasks with programming languages (mainly the dynamically-typed ones..) I don’t know very much because it’s an opportunity to put my hands on them! This way I could experience Ruby’s closures syntax, it was really nice and I’m gonna try something new with it often!

*Updated: line 13

Tags: , , , , ,

Effective Java Programming with Joshua Bloch

Nice video from Joshua Block, Chief Java Architect at Google, talking about his Effective Java™ Programming Language Guide book and also about the consequences of adding new features on the Java language, like the closures’ proposals. He also offers an advice to the programmers, when designing API’s, classes or methods:

“When in doubt, leave it out”.

Tags: , , , ,

Is Java on a evolutionary dead end way?

Artima is running an article where Bruce Eckel talks about about Java’s objective on backwards compatibilities and the problem of combinatorial complexity when you combine a new feature in every possible way with the other language features already present.

It’s the combinatorial complexity that you get when you combine a new feature in every possible way with the other language features. The combinatorial complexity can produce horrifying surprises, typically after the feature is added when it’s too late to do anything about it.

It’s clear Eckel is mentioning the Java closures’ proposals to become part of the language on the Java 7 and the major improvements occured when Java 5 appeared.

If Java is unwilling to break backwards compatibility, then it is inevitable that it will acquire both unproductive complexity and incomplete implementation of new features. I’ve made the case in Thinking in Java 4e that Java Generics are only a pale imitation of real generics, and one of the more appealing suggestions for closures is an incomplete implementation of true closures, but it would actually be preferable to a complete implementation because it produces clearer, more straightforward code.

I particularly agree with him, when he also says

Fundamental new features should be expressed in new languages, carefully designed as part of the ecosystem of a language, rather thanbeing inserted as an afterthought.

He also considers Scala as an alternative for the Java language. Eckel also declares

Java values backward compatibility over the clarity of its abstractions.

I don’t agree with him at this later point, the Collections’ framework is an example of good API design rules with a lot of well designed abstractions by Joshua Block, separating things that change from things that stay the same.

And you? What do u think about it? Should Scala be considered as an exit for Java?

Tags: , , , ,