miércoles, 25 de agosto de 2010

Using AUTOLOAD to build html code

Last week I found a new blog about Io. It's been a long time since I used Io, but maybe this blog will make me look at Io Language again.

In his last post, Dennis Ferron comments on _why's web server yown written in Io. One thing he comments is the slot 'forward'.

As he says, forward is a kind of catch that gets called when a message has been sent to an object and that object didn't have a selector with that name. _why exploits this feature to write an html builder based on that.

What he doesn't mention (I think he's leaving this for another post) is about lazyness. The way _why wrote forward method.

If you define a method without arguments but you call it with some, you can still access them, but with the difference that they won't be evaluated by default. That's called lazyness, and allows you to define the evaluation order of things.

So in the code

title("O HAI")

forward will trap html message before title or strong.

In Perl, there's a 'lightning rod' function too, called AUTOLOAD. You can do pretty much the same, but the difference is that perl will ALWAYS evaluate parameters sent to a function before calling it, so the same code would trigger AUTOLOAD on the 'title' call, then 'strong', and last, 'html' and you have to fill the string from the inner tag to the most generic one.

Here's a gist code that emulates Yown Builder.io in Perl 5.

Good luck Dennis with your new blog!


lunes, 16 de agosto de 2010

Memoizer in common lisp

Last weekend I finished a fast-read of Ansi Common Lisp. I didn't do the exercices but I did pay attention to code examples (those are what make me really understand things).

Overall I find it pretty good, but a bit simple provided you already know something related to the lisp world.

After finishing it, I tried a couple of tricks in lisp, and was surprised how easy it was to come with working codes of a memoized function. Once you have a memoized function, why not convert it to a memoizer function?

Here it is. (If you're reading it on google reader you won't be able to see it, better read blogposts from the original site)

(setf memofib (memoizer #'fib))
(funcall memofib 10)

And you have now a faster fib than before.

Question: how should I change it to make a generic memoizer that memoizes not only unary functions.
In perl I'd have to just pass @_, I suppose changing arg to '&rest arg' and change funcall to apply would do the trick... I have to try it. Later.

More readings: Scheme R5RS.
I read the part I hadn't read before. I have to say even the scheme spec is minimalist, well written, and didactic. Following the language philosophy even at docs level. :)

And a bit more: A paper on Scheme macros. I think I get them, but I don't see much use for them. I suppose I'm still an average blub programmer.

jueves, 12 de agosto de 2010

Sharing interests with others

Here's another of those shitty trackback posts that add nothing to the original post:

I've read a blog entry that explains what I think about sharing interests with others better than I could ever have written.

I found it in HN, so it talks about sharing prorgamming interests with your partner.

Another funny post of the same kind, in a Bug report format. Hilarious too.