bem’s blog

 
Filed under

programming

 

Tail Call Optimization Decorator in Python

This function decorates a function with tail call optimization. It does this by throwing an exception if it is it's own grandparent, and catching such exceptions to fake the tail call optimization.

Sometimes I wonder why a whole bunch of Scheme loving folks use Python. Then, I see rather clever things like this, and it starts to make more sense. I do wonder how much of a performance penalty something like this incurs, though.

Filed under  //   geek   programming   python  

Comments [0]

Fun with constants in Java

The following code does not compile:

public class A {
public static void main(String[] args) {
int i = Integer.parseInt(args[0]);
switch (i) {
case X: System.out.println("ONE!"); break;
case Y: System.out.println("TWO!"); break;
default: System.out.println("ZERO!"); break;
}
}
public static final int X = B.getNumber("X");
public static final int Y = B.getNumber("Y");
}

class B {
public static int getNumber(String s) {
if (s.equals("X")) {
return 1;
}
else if (s.equals("Y")) {
return 2;
}
else {
return 0;
}
}
}

The compile error is:

A.java:5: constant expression required
case X: System.out.println("ONE!"); break;
^
A.java:6: constant expression required
case Y: System.out.println("TWO!"); break;
^
2 errors

I think I'll leave explaining why as an exercise for the reader. (Or for a future post. ;-)

Filed under  //   geek   java   programming  

Comments [0]

Potion Language

Potion is an object- and mixin-oriented (traits) language.

_why's been working on a new top secret project. Wish I had more time to play with all the neat languages out there. This one seems to have a lot of neat ideas.

Filed under  //   geek   languages   programming  

Comments [0]

Interesting talk on Scala


 
A good high level overview of a lot of the neat features and idomatic ways to do things in Scala. It would be nice if he went more indepth on some things, but I found it generally informative, and it's enough information that one could easily look up more detail in the documentation.
 
(Via the Scala blog)

Filed under  //   geek   programming   scala  

Comments [0]

Rawr: JRuby application packaging tool

Rawr, a packaging and deployment tool, is all you'll ever need for your JRuby projects. With Rawr, a simple, pre-generated configuration file turns your code into an executable jar, a .exe for Windows, and a .app for OS X.

It's kinda like Shoes in that it gives you an exe for Windows, an app for OS X, and a jar that you can use everywhere else. Cool stuff.

Filed under  //   geek   programming   ruby  

Comments [0]

JRuby lead developer expounds on useful JVM flags

I probably start up a JVM a thousand times a day. Test runs, benchmark runs, bug confirmation, API exploration, or running actual apps. And in many of these runs, I use various JVM switches to tweak performance or investigate runtime metrics. Here's a short list of my favorite JVM switches (note these are Hotspot/OpenJDK/SunJDK switches, and may or may not work on yours. Apple JVM is basically the same, so these work).

If you write any serious Java at all, I highly recommend that you click the link above and read his post. It is full of many amazing gems that I can see being really useful for optimizing and debugging Java applications.

Filed under  //   geek   java   programming  

Comments [0]

Rhodes framework - Open Source Ruby based Smartphone development

The Rhodes framework is an open source Ruby-based platform for building locally executing, device-optimized mobile applications. It is similar in concept to MVC frameworks such as Rails, Merb and Camping but much lighter weight (and hence executable on a mobile device) than any of these. Along the way of course, we had to implement Ruby for these device operating systems (iPhone, Windows Mobile, RIM and Symbian).

In general, developer productivity is much higher in Rhodes than writing to diverse native device operating systems and APIs since most of your UI customization can be done in HTML templates (ERB files). Rhodes also provides access to native device capabilities such as GPS and PIM data via an extended set of tags (e.g. <geolocation/>).

These applications are also optimized for interacting with hosted enterprise app (SaaS) backends . That is,  it allows mobile applications to work offline with synced local data by embedding a client for RhoSync. The Rhodes source tree contains sample apps for SugarCRM, Siebel Field Service and Ligthouse.

Rhodes is available for iPhone, Windows Mobile, Research in Motion (BlackBerry) and Symbian smartphones. Support for Android devices will be available by end of February 2009.

I just found out about Rhodes, and it looks crazy cool. While I don't currently have a smart enough phone to play with it myself, maybe they'll eventually make it so the thing can target J2ME. (Or, I'll end up getting a smart phone). Regardless, the idea of writing phone applications in Ruby (or another dynamic language) has lots of charm for me, and I've been idly contemplating implementing a interpreter for some small language so that I can do just that on my phone.

In related news, I got to play with my friend's developer G1 this past weekend, and I like. I'm pleasantly surprised at the size and how well it feels. Perhaps I'll get one in a year or so. (And maybe I'll end up with version 2 at that point. ;)

Filed under  //   geek   phone   programming   ruby  

Comments [2]

Timeout::Error in Ruby (Warning, language rant!)

There are bad design decisions, and there are horrible design decisions. The exception class Timeout::Error in Ruby's stdlib is one of those horrible things.

Ok, let me start from the beginning. In Ruby, the standard way to catch any recoverable error in your program is to catch StandardError. Anything that doesn't inherit from StandardError is generally the type of thing that you want to have crash your program. Now, Ruby has a library for having an exception thrown after a timeout. (We'll ignore for a second that this library is implemented in a provably unsafe manner as far as threads are concerned.) You just call the timeout method, and pass a number of seconds and a block. If the block doesn't terminate after the number to seconds, it is forcibly terminated, and the Timeout::Error exception is called. This would be all hunky dory except that Timeout::Error isn't a subclass of StandardError, it's a subclass of Interrupt. "So," one may say, "why even use this thing at all?" Well, because you kinda have to. Want to do something with HTTP? Well, the standard HTTP library happily uses the timeout library to do it's timeouts. And as a result, so does pretty much every other library that has to do something with HTTP. So you're going along in your nice, happy application, when you suddenly find out that even though you're rescuing StandardError so that a network failure doesn't bring down your application, well, that isn't quite true: the minute a network timeout happens, your entire application comes crashing down. The only answer I can find on the internet is that one should always just rescue Timeout::Error when doing anything that might call the timeout library. (rant from a developer that found this way before I did)

What I don't get is how this is still in the standard library. Why not do the obvious thing and just make Timeout::Error a subclass of StandardError? (Of course, the aforementioned problems with the timeout library should be fixed, but I can see that taking a long time) While I forsook Perl in favor of Ruby because of Perl's many gotchas, at least Perl's evil tends to be in my code, not hidden deep in a library that I'm using. And I still miss Perl's copious, usually well written documentation. (Even CPAN modules tend to be rather well documented)

Sometimes, the land of Python seems like it might be nice... (The grass is always greener on the other side, and there is always something hidden in the grass waiting to bite you.)

(I also have a whole rant about the stupidity of Java's properties object inheriting from Hashtable, but that's a lot less insidious.)

(Yet Another Parenthesized note: maybe I just need to go back to my warm, fuzzy, Perl security blanket.)

Filed under  //   geek   programming   rant   ruby  

Comments [0]

Stack Overflow

Joel Spolsky just announced the launch of Stack Overflow, which is free a question and answer site for programmers. As one of those that has tried to search for programming questions on google, only to get mostly useless answers most of the time (and half of them behind some stupid pay gate), this sounds like a totally awesome idea. (And you should read Joel's blog post, it does a much better job introducing the site than I.)

Filed under  //   geek   programming  

Comments [0]

Java: InvokeDynamic explained

Charles Oliver Nutter explains the whole InvokeDynamic thing in the JVM (And why it's really cool), in a way that those of us that aren't elite JVM hackers will understand. Awesome stuff.

Filed under  //   geek   java   programming  

Comments [0]