1/8/08

Playing with SCALA: interoperation with Java

I'm toying with the Scala language and it looks like what the future Java could be (or a great candidate at least).
It feels like at the sweet spot between OO and functional paradigms.
It has first order functions, closures, comprehensions, type inference, mixing composition, Erlang-style concurrency, pattern matching, and more interesting stuff that makes coding easier and concise.
And the best thing is it compiles to .class files so it integrates seamlessly with Java: you have all your Java libraries when programming in Scala and you can call your Scala classes from your Java code as just another java class.
Of course, I'm not aware of anybody using it in a production environment, but looks VERY promising.
Having this kind of language makes less necessary to "bolt-in" those features in the core Java language.

So I installed the eclipse plugin and started to play with it.

I knew you could use Java libraries in Scala (even you can use Hibernate !) , so I was interested in the opposite way: calling Scala code from Java.
I created a Scala project and copied the QuickSort implementation:
(I put it in an "object", the Scala's singleton)


object MyScalaObj {
  def sort(xs: Array[Int]): Array[Int] =
    if (xs.length <= 1) xs else { 

      val pivot = xs(xs.length / 2) Array.concat(sort(xs filter (pivot >)), xs filter (pivot ==), sort(xs filter (pivot <))) 
    } 
}

After the project is compiled, I created a Java project and used the output path of the Scala project as a library, and voila! I could access the Scala classes from my Java code (even with code-completion). So my Java class is:


public class PruebaJavaScalaInterop {

  /**
  * @param args
  */
  public static void main(String[] args) {
    int[] org={4,2,5,3,1,9};
    int[] res=MyScalaObj.sort(org);
    System.out.println("Org:");
    dumpArray(org);
    System.out.println("Res:");
    dumpArray(res);
  }

  public static void dumpArray(int[] arr){
    for (int i=0;i < arr.length; i++ )
      System.out.print(arr[i]+" ");
    }
  }
}


The nice thing is the complex algorithms can be coded concisely in Scala using a Functional paradigm, and be called seamlessly from plain Java code.
I still need to learn more about it, but the possibilities seems very interesting, don't you think?

12/3/07

Quote of the month

"Object-oriented programming is like money, as the old joke has it: It's not everything, but it's way ahead of whatever's in second place."
Objects have not failed - Guy L. Steele Jr.

11/18/07

Javascript, the underdog

I'm back working on some JavaScript, just like the old times, adding client-side UI dynamics. In spite of still being hard to work and debug (at least in IE), it allows me to play with a different language. I realized the worst thing you can do with JavaScript is use it like Java, it works but you'll miss the interesting features like prototyping and higher order functions.
This week I found that Mozilla JavaScript implements useful iteration methods were you can pass functions to operate on the collection:
filter, forEach, every, map, and some:

filter
Selects the elements of the collection that satisfy the function

forEach
Applies a function to all the elements of the array

every
Returns true if every element of the array satisfies the function

some
Returns true if at least one element satisfies the function

map
Creates a new array with the results of calling a provided function on every element in this array.

As an example, you can easily write:

funtion doSomethig(element){ /*whatever*/}

and then:

aCollection.forEach(doSomething)

Suddenly, JavaScript coolness factor has increased for me :-)

Those functions are standard in Mozilla JavaScript, but they provide the source code, and is small enough to include it if you're using another browser. And as the objects are “open” and you create new ones by prototyping, you just need to add the code to the object you want (as long as is “array” like) it will work.

11/9/07

OOPSLA keynotes podcasts

Very interesting software engineering pioneers's podcasts from the OOPSLA 2007 conference:
David Parnas
John McCarthy
Frederick Brooks

I couldn't recommend them more!

9/19/07

IT Architect skills library


I’ve been in IASA’s IT Architects Regional Conference (ITARC) in Atlanta. Great event, with very interesting events with the topics which the architects want. Some of the presentations were very good, others not that good, but I got many good ideas from all of them. I’ll post on the topics later.
Meanwhile, I want to share IASA’s project of creating an IT Architect skills library: http://www.iasahome.org/web/home/skillset. Is an ongoing project, but is worthwhile to take a look at it.

8/24/07

Quote of the month

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
Martin Fowler, "Refactoring: Improving the Design of Existing Code"

8/21/07

Language "wars"


I'ts me or we are at "war" again?
As the old history goes, in the nineties, OO Warlocks fought each other from their towers, everyone claiming to have The Methodology, until some of they settled for the "Unified Philosopher Stone" or something like that... (I always thought it was strange that the outcome of a methodology war as a notation, but I digress)
After that, came the agile jihad, trying to burn the non-believers in the stake, all in the name of the eXtremellyPurity religion, causing great havoc in the process world (and every process guru claimed "I'm agile too!", probably to satisfy the angry mob... but agile waterfall got burned anyway). Is funny now how many experts now say "XP is very good, but if you do < insert your favorite common sense practice >, is even better!"

And now, the languages!
Well, is not really a war, I guess is more like in the OO-java-roman.net empire, encounters with the Rubyites or the functional programming tribes and similars(the Haskells, Lisperers, or Schemitas) are getting more common... their hordes always roamed in far away places, but they are getting closer and closer. Even the habitants of island of JavasCriptya turned out to be part of the FP tribes! Granted, the Enterprisey temples, aqueducts, and coliseums will not easily fall or be overrun by them, but I guess some interesting things will happen. Maybe with an unifying Domain Driven lingua franca we can get the best of each other, because frankly, Java (the kingdom of thousands frameworks) is hard
I forgot to mention the mythical lands of Translathia, where their habitants write using symbols instead of words, and through their magic, they materialize what they just described... they have all their stuff decorated with their hieroglyphs. Mythical as it sounds, I happen to know two respected sages from that land...


Now, seriously... I'm seeing a lot of noise/cool-stuff from the FP camp, probably thanks to the concepts introduced by Ruby, and I think it would be good to learn a new paradigm.