Wednesday, September 14, 2011

On self containment

I like systems that are self contained. No unnecessary dependencies.

I woke up today and decided that lispdown needs an implementation. After all, the rules are simple; should be a shell script, right?

True; but I couldn't help thinking: shouldn't I write lispdown in Lisp? Whoever needs it (lispdownville: pop 1) obviously will have lisp - and that's all they'll need. Sure they most probably will have bash, but why use it at all?

This is the same kind of thought process that led me to build cuke4jas. I found joy in the fact that the application being built and the tools being used to build it are both from the same base platform/language.

I know everyone has to be a grasshopper first, but...

... could I do it matrix "I know Kung-fu" style , please?

I bull-in-a-china-shopped my way through SICP so that I can get to the metacircular evaluator bit as soon as possible, but now I see even more clearly that each chapter firmly builds on the previous one in this book.

As I read the implementations of eval and apply I realize I have to go back and read the foundational material before I can expect to just grok them. My kingdom for the knowledge packs from the Matrix :)! 

Sunday, September 11, 2011

On a personal note: Remembering 9/11

It's been 10 years.

Like many in our generation, I remember where I was and what I was doing that day. I know Latur probably had greater deaths, as probably did Bhopal; but this is the one that I was closest to.

I lived in Parsippany, NJ then and remember heading out to my car to go to work only to be told that the towers were bombed. I remember watching in disbelief as people jumped from one certain death to another. And then the videos of the plans actually crashing. And then the short messages people sent their dear ones from the planes. I remember a nation coming together immediately.

I remember walking into our regular diner the weekend after 9/11 and being asked: "You're indian, right?" by the guy who'd seated us every time before. If I were him, I'd ask too. I remember the Indians touting American flags on their cars for the next few months.

I remember feeling sad that this tragedy of epic proportions was becoming a media circus, yet being so part of it that I couldn't look away. I remember going to Ground Zero a month or so after 9/11 and feeling viscerally affected - the scene left enough clues as to how it happened: from the huge gaping hole with twisted steel and crumbled slabs of concrete to the charred buildings nearby I could imagine the myriad personal horrors that the people who were there on that day went through. I remember being annoyed by the street hawkers who had already started selling touristy swag nearby, but was numbed enough by the event and the place themselves to ignore such pettiness.

I remember going to Ground Zero last year and still feeling the same as before. I remember being a tad ticked off that the scene didn't look much different from before - maybe it had, but where was the monument to all the people who died that day? Where was the imposing answer that showed that we as humans will not bow down to terrorism?

I remember the firemen being nice to my kids and posing for pictures (they asked to sit on the fire engine), but couldn't ignore the thought that maybe some of them had friends who were no more. And I still couldn't understand the picknicky people swarming the site nor the hawkers with their "I've been to Ground Zero" T-shirts and mugs.

Fiercely apolitical, I have actively shunned news media in all its forms since my college days, but I couldn't get enough of the coverage of the Bin Laden capture. Real or not, it gave me closure to those events from a decade ago - and I didn't even lose any dear ones.

I cannot remember  a date to save my life, but I remember going to the World Trade Center on July 4th 2001.  I was badly hung over from the previous night and the meal from the restaurant at the top of the tower set me right so I could enjoy the view. I remember there being barbed wire surrounding the deck to prevent jumpers - seems kinda pointless now.

I kept the ticket stub to the towers and joke to my friends that I'm keeping it for its antique value, but whenever I happen on it while sorting my keepsakes, I'm taken back to this day, ten years ago.

Thursday, September 08, 2011

A personal exploration on Java's Enum declaration

What:

A personal exploration on why Enum is declared as Enum<E extends Enum<E>>

Why

I stumbled upon this interesting question a while ago when a co-worker showed it to me. Googling it provied enough links to satisfy my initial curiosity, but recently the question surfaced again; so I tried to implement my own version of the explanation provided online to "see for myself".

What follows is largely based on Angelika Langer's FAQ and can be thought of as my worked-out version of that page. My enum class is called Enum1 to avoid clash with the SDK's version; and has been tested with JDK 1.7.0_07.

The exploration

Say you're the Java team and you want to support syntax like:

    enum Color(RED, GREEN,BLUE);

... so that it could be used like so:

  • As constants : Color.RED ... which means that public static constants must be available
  • In variables : Color color=Color.BLUE ... which means that it must of type Color
  • In comparison : color==Color.BLUE ... which means it must implement compareTo()

Further, say you intend to add this feature with as little syntactic sugar as possible; so you want to convert the basic enum syntax alone via a special operation in the compiler and generate everything else using standard Java language constructs.

The generated code would look something like so:

    public class Color implements Comparable<E>{
        private static final Color RED;
        private static final Color BLUE;
        private static final Color GREEN;
        private static final Color $VALUES[];
        static{
          RED = new Color("RED",0);
          BLUE = new Color("BLUE",1);
          GREEN = new Color("GREEN",2);
          $VALUES = (new Color[]{ RED, BLUE, GREEN });
        }

        private final String name;
        public final int ordinal;

        //private since nobody outside needs to use this
        private Color(String name, int ordinal){
            this.name = name;
            this.ordinal = ordinal;
        }

        public final int compareTo(Color c){
            return this.ordinal-c.ordinal;
        }
    }

If you wrote more than one of these, you'd have quite a bit of logic that's common and could be extracted out:

  • Each item in the enumeration has a name and an ordinal.
  • Each item is built privately and the instances built statically are made available publicly as constants and via an array
  • The constants have comparable values (optionally internal)

So you could write a base class like so:

    public class Enum1<E> implements Comparable<E>{ //line1
        protected static Enum1 $VALUES[]={};

        private final String name;
        public final int ordinal;

        protected Enum1(String name, int ordinal){
            this.name = name; 
            this.ordinal = ordinal;
        }

        public final int compareTo(E e){
            return this.ordinal-e.ordinal;  //line 2
        }
    }

... where Enum1 needs to be defined with a generic type E because any implementation of Comparable needs to be generic. Doing that, however will cause the compiler to throw a warning that line 2 above may be using unsafe operations. This is because Enum1<E> is equivalent to Enum1<E extends Object>; so we could potentially send in any subclass of Object into compareTo().

To fix that, we'll need to constrain the type of E to Enum1s or its subclasses. This can be done by changing the type parameter in line 1 to : Enum1<E extends Enum1>. The modified code thus becomes:

    public class Enum1<E extends Enum1> implements Comparable<E>{   //line1
        protected static Enum1 $VALUES[]={};

        private final String name;
        public final int ordinal;

        protected Enum1(String name, int ordinal){ 
            this.name = name;
            this.ordinal = ordinal;
        }

        public final int compareTo(E e){
            return this.ordinal-e.ordinal;  //line 2
        }
    }

Now Color can be written as:

    public class Color extends Enum1<Color>{
        public static final Color RED;
        public static final Color BLUE;
        public static final Color GREEN;

        static{
          RED = new Color("RED",0);
          BLUE = new Color("BLUE",1);
          GREEN = new Color("GREEN",2);
          $VALUES = (new Color[]{ RED, BLUE, GREEN });
        }

        protected Color(String name, int ordinal) 
            { super(name,ordinal); }
    }

...and test code can be written like so:

    public class TestEnum1{

        public static void main(String[] args){
            Color c1=Color.RED;
            Color c2=Color.RED;
            Color c3=Color.BLUE;
            Color c4=Color.GREEN;

            System.out.println((c1==c2));       // 0
            System.out.println(c1.compareTo(c3));   //-1
            System.out.println(c4.compareTo(c3));   // 1
        }
    }

We can even write another enum like so:

    public class Vehicle extends Enum1<Vehicle>{
        public static final Vehicle BIKE;
        public static final Vehicle CAR;
        public static final Vehicle BUS;

        static{
          BIKE = new Vehicle("BIKE",0);
          CAR = new Vehicle("CAR",1);
          BUS = new Vehicle("BUS",2);
          $VALUES = (new Vehicle[]{ BIKE, CAR, BUS });
        }

        protected Vehicle(String name, int ordinal) 
            { super(name,ordinal); }
    }

... and enhance the test code to check out things like the ordinals of two Enum1-derived enumerations not matching even though the numerical values are same:

    public class TestEnum1{
        public static void main(String[] args){
            Color c1=Color.RED;
            // ...

            Vehicle v1=Vehicle.BIKE;
            // fails with error: incomparable types
            System.out.println((c1==v1));
        }
    }

Pending Question

So I've still not figured out what the final <E> in:

    public class Enum1<E extends Enum1<E>>

... is for, considering the code above works without it.

Thursday, September 01, 2011

Jack: Allow WIP pseudo code

You know how you sometimes mock up code with some keywords to represent the structure of the logic but then put in hand-wavy text for rest of it because you've not fleshed it out yet?

Jack should support that directly. Something like:
if --calamity happens--
    --dont panic--
    --call 911--
end
Just a hint of an idea at this time. I'm not sure if -- is the right sigil/delimiter to use. "<<" seems like a good one from personal experience, but it has bad web mojo :).