Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

07 December 2009

Prefix notation via closures

For the programmers in the audience...

Recently I’d read yet again someone saying that prefix notation is one of the things keeping more people from using Lisp. As I’ve said before, I find it hard to believe that this is an obstacle in practice. Especially since so many languages limit infix notation to essentially numbers (and only built-in numeric types at that) and Boolean values.

(Keep in mind that I’m saying this as someone who was raised on C rather than Lisp.)

Then I started thinking about the way that method invocation in many object-oriented languages is infixish. Consider Smalltalk: "hello" at: 3 Or Java: "hello".charAt(3) Or even: bigInt.add(anotherBigInt)

So, by using the closure-as-object idiom in Scheme...

(define (number-object-ctor n)
(λ (op arg)
  (if op
      (number-object-ctor (op n (arg #f #f)))
      n)))

(define (print-number-object n)
(display (n #f #f))
(newline))

(define x (number-object-ctor 4))
(define y (number-object-ctor 5))

(print-number-object (x + y))
(print-number-object ((x * x) + (y * y)))

It’s not as practical as an infix macro. I just thought it was interesting, however, that prefix notation could be twisted into infix notation that way.

08 April 2008

Scheme in the browser

The creator of JavaScript on it’s creation:

Brendan’s Roadmap Updates: Popularity

As I’ve often said, and as others at Netscape can confirm, I was recruited to Netscape with the promise of “doing Scheme” in the browser.

Interesting. I wish he hadn’t also had the mandate to give it Java-esque syntax.

I think. Sometimes, though, I almost think that I enjoy JavaScript more than Scheme.

29 November 2007

Java

“Write once, run anywhere” I’ve always been a bit wary of that slogan. Sure, the Java virtual machine (like the many VMs that came before it) is fairly easy to port to any platform, but those platforms almost all have a C compiler first. (In fact, the JVM for most platforms is written in C.) So this doesn't really gain you much. The real issue for cross-platform development is standard APIs. That’s the hard part. The JVM itself always gets ported quick, but sometimes you never get all the core APIs ported. Oh, a standard ABI is nice, but that’s really just icing. Java is perhaps a bit better off than many languages when it comes to simply having cross-platform APIs to be ported. I think there are at least a few languages, however, that fare at least as well as Java in this area. In the end, Java is no more cross-platform than any other language. It happened that, for a while, enough people (or enough big companies) cared enough about Java that it did end up being a half-decent cross-platform option for a while. Because enough effort was expended, not because anything about it was particularly better suited to the task. I’m distant enough for the Java world now to not be sure how the situation has changed, but I sense that it may be changing.

19 October 2007

Inheritance is over-rated

Some more observations based on my experiences with EcmaScript (a.k.a. Javascript) as compared to C++ and Java:

(I suppose I could try to generalize and consider EcmaScript a representative of the Smalltalk tradition versus C++ and Java as representatives of the Simula tradition; but that probably opens a lot of other issues.)

It occurs to me that most classes I’ve written in C++ or Java have not needed inheritance. They provided only encapsulation. (Which I can do just fine in C—no object-orientation required.)

When I have needed inheritance, it has most often been for polymorphism. In EcmaScript, you don’t need inheritance for polymorphism.

(With C++, you can have polymorphism without inheritance, but that requires the complexity of templates. Likewise, in Java you can use reflection to achieve the same sorts of things, but you get more complexity with it.)

Sometimes I’ve abused inheritance in C++ or Java to work around limitations. Such as adding additional methods to an existing class. In EcmaScript, I can add methods to objects (or objects serving class-like roles) directly.

In EcmaScript, I only really need to use inheritance when I need to share an implementation, and that just doesn’t seem to come up as often in my experience.

(Moreover, I find closures—which EcmaScript has but C++ and Java lack—extremely useful.)

Now, there are—of course—trade-offs involved. EcmaScript isn’t all roses by any means. Give it a static debugger (like MrSpidey/MrFlow), and I think it could hold its own versus C++ or Java for many applications.

Give it hygienic macros (on the road-map for Javascript 3) and first-class continuations, and it starts to stack up well against Scheme as well.