nolacoaster ([info]nolacoaster) wrote,
@ 2009-02-23 17:42:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Entry tags:java, nerds

How to Hack Java Like a Functional Programmer
I am currently working on a document. It's kind of a manual. Maybe more like a manefesto. But really, it's a collection of the ideas and strategies that I use when writing Java code. As it turns out, many of those ideas and strategies come from my attempts (and the attempts of other's that I have learned) to use Java in a style that is more like functional languages, particularly the ML languages with which I have some experience.

This document is really targetted at people with some Java experience, and some functional experience, but people who would not consider themselves experts. In particular, it's not targetted at academics, or at least not at PL people. But hopefully it may contain some ideas that you find cool.

Part of the reason that I am posting this now rather than after I have completed a first draft is due to my relatively slow progress. I am hoping that if I post what I've got, you guys may give me some feedback and some cooler ideas, and that this enthusiasm will be enough to help me complete this in a reasonable amount of time. That being said, after this post I will not put up any more blog posts until I at least have an entire draft. If you want to wait until then to read, please be my guest.

So without futher ado, the current draft of, "How to Hack Java Like a Functional Programmer" will be posted at the following URL, and will be updated just as often as I work on it until it is complete:
http://www.nelsbeckman.com/java_like_an_fp.pdf




(Read 11 comments) - (Post a new comment)


[info]nolacoaster
2009-02-24 04:23 pm UTC (link)
It kind of works:

abstract class Expr extends RuntimeException {
abstract int eval();
}

class Val extends Expr;

class Add extends Expr;

static void foo(Expr e) {
try {throw e;}
catch( Add a ) {
a.getE1();
}
catch( Val v ) {
v.getVal();
}
}


However, there were two issues. The "coverage checking" didn't quite work out the way that I had hoped. Unless you make Expr extend RuntimeException, you will get a compiler error if you don't catch "Expr" itself. In other words, it is not enough to catch every subtype of Expr, because of course with Java's type system you can always add new subtypes.

Then, I am forced to make Expr an abstract class rather than an interface, as I'd prefer, because Expr itself must be some subclass of Throwable.

(Reply to this) (Parent)(Thread)


[info]simrob
2009-02-25 05:32 am UTC (link)
Well I guess Python is better for this particular travesty, then. Support for :? style stuff is really a no-brainer:
static void foo(Expr E) {
  match e with 
    | :? Add -> e.getE1;
    | :? Val -> e.getVal();
...and I think all the fuss over Active Patterns (which are cool) obscures this point.

(Reply to this) (Parent)


(Read 11 comments) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…