Ugo Landini random thoughts on technology and life
Ulog, the Ugol's Weblog
Archives
« agosto 2005 »
lunmarmergiovensabdom
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today
XML
Search

Links
 

Today's Page Hits: 19

All | General | Java
« Previous day (Aug 30, 2005) | Main | Next day (Aug 31, 2005) »
20050831 mercoledì agosto 31, 2005
Vacation and blocks...
I'm back from vacation, but the awful weather just followed us!

I really didn't do anything, because it rained cats and dogs almost all the time. Here you can see our camping, Parco delle piscine, which is considered one of the best in Europe with its three wonderful thermal pools. It's located in Sarteano, in the wonderful Tuscany: unfortunately the mineral water is cold (did you know the difference between frigidarium and calidarium?), so swimming it's not exactly the funniest thing to do when external temperature is 15° C

Here you can see another wonderful thermal pool we visited (calidarium, i.e. hot water at 37° C!) Bagni S.Casciano. This one is really a pleasure, especially in cold days...

By the way, I had a lot of time to think so in the freetime I just studied. One of my recurring geek propositions is to learn a new language every year, and last year I studied Objective C to be fair with my new iMac. This year I finally had the time to approach Ruby, which I have always heard to be as one of the best modern Object Oriented languages.

I studied Ruby core on the famous pickaxe book (the second edition), and I have also studied Rails, the most famous Ruby based web framework. Well... I am a java guy, but I have always been fascinated by Smalltalk: Ruby seems to me an *excellent* language, and I am just in love with some of its features:

- Ruby blocks. I want blocks in Java, they're incredibly useful. Why didn't we put them in Java 1.5? We did the new enhanced for loop, which is only one step away from internal iterators, which in turn is one step from blocks...
- Everything is an object, also numbers. Writing 1.upto(10) it's a joy for an OO lover...
- no more spoonfeeding the compiler with ; ( and so on.
- Rails is incredibly productive, thanks to Ruby but in particular to intelligent defaults. We should learn some lessons here... and I have seen that something is going on in the java community

Waiting for blocks support in Java :-), here is a naive block implementation in java I immediately typed. Obviously, don't use this in production code :-), it's only an example. But also this simple internal iterators should show how powerful (ie expressive) a block can be, if supported straight in the vm

//For.java

import java.util.Collection;

public class For {

  public static void each(Collection c, Block block) {
    for (Object o : c) {
    block.execute(o);
   }
  }

  public static Object find(Collection c, Block block) {
   for (Object o : c) {
    if (block.execute(o)) {
     return o;
    }
   }
   return null;
  }
}

//Block.java

public interface Block {

  boolean execute(Object o);

}

//Main.java

import java.util.ArrayList;

public class Main {

  public static void main(String[] args) {

   String s1 = "Hello";
   String s2 = "cruel";
   String s3 = "world";

   List items = new ArrayList();
   items.add(s1);
   items.add(s2);
   items.add(s3);

   For.each(items, new Block() {
    public boolean execute(Object o) {
     String s = (String)o;
     System.out.println(s.toUpperCase());
     return false;
    }
   });

   String x = (String)For.find(items, new Block() {
    public boolean execute(Object o) {
     return (o.equals("cruel"));
    }
   });

   System.out.println(x);
  }

}



ago 31 2005, 01:37:07 AM CEST Permalink Comments [0]