Archive for category Uncategorized

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: , , , , ,

Twitter on Scala Interview

There is a nice interview with the Twitter development team on Artima about using Scala in production on Twitter code. The team talks about some issues and facilities regarding the chose of Scala to develop Twitter’s queueing system and how Scala affected the team’s programming style.

My personal highlights:

And Ruby, like many scripting languages, has trouble being an environment for long lived processes. But the JVM is very good at that, because it’s been optimized for that over the last ten years..

So Scala provides a basis for writing long-lived servers…Another thing we really like about Scala is static typing that’s not painful. Sometimes it would be really nice in Ruby to say things like, here’s an optional type annotation

And because Ruby’s garbage collector is not quite as good as Java’s, each process uses up a lot of memory. We can’t really run very many Ruby daemon processes on a single machine without consuming large amounts of memory

In some cases we just decided to burrow down and use the Java collections from Scala, which is a nice advantage of Scala, that we have that option..

As I’ve learned more Scala I’ve started thinking more functionally than I did before. When I first started I would use the for expression, which is very much like Python’s. Now more often I find myself invoking map or foreach directly on iterators..

The reason you should care about immutability is that if you’re using threads and your objects are immutable, you don’t have to worry about things changing underneath you..

It’s very worth read. Post your comments here when you read it.

Tags: , , , ,

Twitter Ecosystem Tools

I’m very impressed of how many applications are being built on top of Twitter. This simple communication tool bas became so popular that people use it to expand their business relationships, promote themselves, discover new friends, talk about stuff they like, et cetera..

Here are some tools (maybe mashups?) which use Twitter:

Here you can find other 100 Twitter Tools

Tags: , ,

Bean Validation – Emmanuel Bernard on JSR 303

JavaLobby released an interesting interview with Emmanuel Bernard, the spec lead of of JSR-303: Bean Validation..

One of the important goals of the Bean Validation spec is, as Emmanuel Bernard says, is

to provide a default runtime engine that is used to validate the constraints around the domain model, in Java.

The development team decided to use annotations to express the constraints applied at the domain model. They argued that with annotations the constraints are put directly at the properties in the code, expressing the constraints associated with the property.

I highlighted some interesting parts of the interview:

Bean Validation lets you standardize the way you declare the constraints. So an application developer will just know one way to declare constraints, and just forget about all the different models. There’s no duplication around the validation on the layers.

If you think about a constraint, a constraint is really some kind of an extension of the type system.

Bean Validation lets you validate an object graph using the @Valid annotation. Basically saying, ‘Well, when you validate this object, make sure the associated object is validated as well.’ By the way, even if Bean Validation is primarily annotation focused, there is the equivalent in XML.

Any framework that is having a need to use validation, to apply validation, can either use the runtime engine of Bean Validation or go extract the metadata and play with that. So the user declares it once, in one place, and every framework in the Java space can actually go and either apply the validation routine …

You can listen the podcast of the interview also. It’s very noteworthy to read or listen the interview and see what’s next on validation in Java. Before the Bean Validation API become public to all of us, which framework do you use to validate constraints on the Java platform?

Tags: , , ,

Installing Vista..and restoring Grub from a live Kubuntu cd..

Somedays ago I installed Vista for the first time at my home desktop (I was very worried about the performance of this OS, so I took a long time to give it a try) and because of my disattention I erased my MBR, so my good old grub startup screen no longer appeared.
I couldn’t boot from my Kubuntu anymore ( I prefer KDE rather than Gnome 🙂 ). So, I googled a little bit and found at the Ubuntu forum how to restore grub from a Ubuntu live cd. I restored it with some steps and all went fine! I hope you enjoy this tip!

Tags: , , , , , ,

Joshua Block on How to Design a Good API & Why it Matters

In this talk (recorded at Javapolis), Joshua Block presents guidelines about how to design good APIs. I highlighted what i think are the most important parts of the talk:

  • Functionality should be easy to explain: If it’s hard to name, that’s generally a bad sign
  • Good names drive development
  • Be amenable to splitting and merging modules (If names are nasty, take a step back and make things easy to describe)
  • When in doubt, leave it out
  • You can always add, but tou cannot take it out
  • Implementation Should Not Impact API
  • Always omit implementation details
  • Inhibit freedom to change implementation
  • Don’t let implementation details “leak” into API (For example: Serializable, hash functions)
  • Minimize Accessibility of Everything (This maximizes information hiding)
  • Public classes should’nt have public fields
  • API should be easy to learn, read and use: It should be consistent, it’s a little language
  • Documentation matters (Example: Method contract between it and it’s clients)
  • Think of preconditions, postconditions, side-effects
  • Don’t Transliterate API’s
    • What’s the problem it solves?
    • What shoud abstractions did it use?
  • Don’t Make the Client Do Anything The Module Could do
  • Throw Exceptions to Indicate Exceptional Conditions

When you see the talk, post your comments here.

Tags: , , , ,

Erich Gamma Discusses Jazz, Eclipse, JUnit and Design Patterns

Good interview from Erich Gamma at QCon London 2008, where he discusses, among other things, the JUnit framework, the Gang of Four book about Design Patterns and the Jazz project. I’ll try to summarize some of his interesting advices and responses here:

Why Eclipse is so successful:

“We focus on stable APIs, so we understood it was a commitment up front and we maintained our APIs, so we tried to really avoid breaking our community..”

About JUnit:

“We always said it would just be as simple as writing System.out.println() but fully automated….I think the key was it makes writing tests as simple as writing this debug statement….”

About Design Patterns:

“In Design Patterns we talked a lot about abstract coupling, that you can couple things by an abstract class, that the reference is only to an abstract class interface..”

“Never use the class names we give in the pattern for it – that’s wrong. Use the domain-specific names, make it very specific to what your use..”

He also gives advices in how to identify design patterns:

  • Something non-obvious
  • The same kind of structure
  • Confidence on multiple uses..

About Dependency Injection:

“I think it could be captured as a pattern. There are a lot of tradeoffs in there, it would fit into the whole creational realm..”

Check the whole presentation on InfoQ here. It’s worthwhile to see.

Tags: , , , ,

My first impressions about LaTex

There’s been a good time I have wanted to try the http://en.wikipedia.org/wiki/LaTeX. So many times some masters / doctorate friends told about it for me very well. When I started doing my post-graduate course in Software Engineering at ITA, I knew it was time to test this powerful too. And this moment has come. For those who don’t know LaTex, it was created by Leslie Lamport in the 80’s and it’s built on top of the TeX tool, which allows the author of academic works obtain high-quality typeface. The TeX is a typography software developed by Donald Knuth in the 70’s for the publication of texts with great layout and typographical quality, particularly those with focus on mathematical equations. Knuth, at that time, developed the TeX to write his most famous books’ collection named The Art of Computer Programming, because he didn’t find a decent typesetting system at the time. Basically, the LaTeX adds a set of commands in the text that define how the system’s processing TeX will format it.
The text is entered within several LaTeX commands, as chunks of code in any programming language. These commands define the font type, the text formatting, chapters, sections, special characters, margin size, and so on. This makes the system different from the WYSIWYG methodology, in which the text is written exactly as it will be seen in the final result. All LaTex command begins with a backslash (\). LaTeX files are created with the .tex extension and the text should be compiled and the result file could be a binary DVI or PDF file. The fact of having to compile the text may represent a difficulty to the editor of the text, but the final result is very good, with high-quality typeface, as if the text had been edited by someone profession on the area.
The major advantages that I see in using LATEX instead of a WYSIWYG common editor, such as Word or OpenOffice, are:

  1. The LATEX lets you define how the text should be presented and formatted in the same way as the CSS works with HTML / XHTML. The idea is that you don’t worry about how the text should be formatted. Leave this work to TEX!
  2. Mathematical formulas, displayed by LATEX, are extremely elegant.
  3. LATEX manages all the numbering of chapters, sections, lists, figures and tables, footers, etcetera..
  4. TEX is portable and free, so that means it works in most existing hardware.
  5. You can tell the LATEX additional libraries to specify, for example, references for a second rule.
  6. Do not worry about the layout, only within the structure of the document!

As a quick example, we would have the following construction in LATEX:

\documentclass[a4paper]{article}
\begin{document}
Your text here….
\end{document}

The code sets up a minimum LaTex file, which is defined using the article class in a A4 size paper. The tags \begin{document} and \end{document} must contain the text to be entered. These commands, when compiled by Tex within the .pdf output document option already produce an excellent final result, very different from the Word one. You can find good references on the package here. And I leave here a challenge: even if you are a fan of Word, try to create a document in LaTex once and see the final result! I bet then you won’t stop writing your texts without it!

Portuguese version: http://log4dev.com/2008/07/29/minhas-primeiras-impressoes-sobre-o-latex/

Tags: , , ,

The Fan programming language

Cedric has showed us an interesting programming language called Fan, which has a lot of useful features. The ones I liked most are:

  • Familiar Syntax: Java and C# programmers will feel at home with Fan’s curly brace syntax.
  • Concurrency: Tackle concurrency with built-in immutability, message passing, and REST oriented transactional memory.
  • Static and Dynamic Typing: Don’t like the extremes – take the middle of the road.
  • Object Oriented: Everything is an object.
  • Functional: Functions and closures are baked in.

Here are some code chunks, showing its closures syntax:

// find files less than one day old
files := dir.list.findAll |File f->Bool|
{
    return DateTime.now – f.modified < 1day
}

// print the filenames to stdout
files.each |File f|
{
    echo("$f.modified.toLocale: $f.name")
}

I haven’t tried the Fan language a lot yet (I’ll post my comments here when I do it), but I congratulate the Fan authors for being possible to run it on both the JVM and .Net. I agree with Cedric when he said about the possibility to declare constructors with arbitrary names (although they must be prefixed with the new keyword) and invoke it as static methods of the class. From designer of the class point of view, it’s easy to identify the constructor (it’s highlighted by the new keyword), but from the client it’s difficult because there’s no way to differentiate a constructor from a static method call. It’s a bit odd to be able to do that. Just take a look at the code:

// Using an arbitrary name as a constructor
class Person
{
    new create(Int age) { this.age = age; }
    Int age
}
p = Person.create(20)

Anyway, I think it’s a good work to make the language have both object-oriented and functional constructs and be portable to both the Java VM and the .NET CLR. Good work guys!

Tags: , , ,