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

html(
title("O HAI")
strong("KTHXBYE")
)


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!

--
raig

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.

martes, 27 de julio de 2010

Rakudo * is next door



Yes.
Rakudo *, the first official distribution of stable (yet incomplete) perl6 interpreter rakudo. Devs haven't focused on optimizations but features, and it's quite useful as it is IMHO. It brings lots of the crazy Perl 6 features announced in the synopsis like hyperoperators, metaoperators, lazyness, junctions, introspectable OOP, and regex and grammars as first class citizens in the language. That last thing means you can plug a new parser for your program in lexical scope. Welcome DSL world. Early adopters, language geeks, perl hackers, you have no excuse now not to try rakudo :) .

Myself I haven't toyed enough with Perl 6 to do anything useful, but I have done quite a few conceptual tests, and, although I'm still baby-talking Perl 6, it's quite nice to hang around at #perl6 and see perl6 heroes stressing rakudo's features till the unthought limits.

More good news: Audrey Tang appeared a few days ago on #perl6 (I think thanks to masak's post), and started hacking on niecza (sorear's perl6 implementation focused on compiler and interpreter optimization techniques).

I tried to solve one of moritz's perl6 Challenge, implementing Str.trans (á la p5 tr// ) in perl6. Mine, is the (really incomplete babyperly) submitted on a gist pastie. I didn't win the rakudo T-shirt.

More on Perl6. A couple of perl6 related posts reached HN and reddit top page and stayed more than 1 day there. buzz buzz buzz buzz :)

And last but not least, Barcelona Perl Mongers are living a celebration, on July 29th of July, the R* Day (and being the last thursday of the month, which is the usual meeting day) will join at UPC Campus Nord, and Alex Muntada will give a Perl6 introductory talk. Of course, you're all invited. Unfortunately, I won't be able to be there as I'll be in Malta. O HAI Holidays!

Happy 1st anniversary



martes, 13 de julio de 2010

Back to the future?

9:00
Llego al currele, y mientras estoy haciendo mi navegacion de
reconocimiento, encuentro porahí un link interesante: Google’s
Do-It-Yourself App Creation Software
. Alguna frikada de google
labs... ya verás. Total, que empiezo el articulo y leo que el
proyecto esta liderado por un tal "Harold Abelson". H.Abelson en
google? (pues se ve que se ha pillao un sabático del MIT). Bueno, el
proyecto va encaminado a que todo el mundo pueda ser programador de
sus miniaplicaciones.

17:00
Recibo un mail en una maillist Que pone "se nos acabóo el trabajo".
Coño! Esto es scratch!!!
Reflexión: Los Schemers chupando de smalltalk? Bueno, puede ser,
entre comunidades pioneras... pero en google, hacen algo en
scheme/smalltalk? Bueno, de hecho, scratch tambien sale del MIT, creo.

17:30
Llego a la pagina oficial del proyecto y efectivamente al final de la pagina.
(...) Open Blocks visual programming is closely related to the Scratch
programming language, a project of the MIT Media Laboratory's Lifelong
Kindergarten Group.

The compiler that translates the visual blocks language for
implementation on Android uses the Kawa Language Framework and Kawa's
dialect of the Scheme programming language, developed by Per Bothner
and distributed as part of the Gnu Operating System by the Free
Software Foundation. (...)

18:00
Me pregunto si contestar al mail de la maillist contando de donde
viene todo esto, quien es Abelson, y entro en otra
batalla épica? Respondo con un : "joer, estos de google son la ostia,
estas cosas, solo las pueden inventar ellos...", me callo?.


En fin... Esta claro que poco a poco van alcanzándonos, pero la
verdad es que me encanta estar en este mundillo (pionero después de
casi 30 años), y estas aproximaciones que hacen 'los grandes' le dan a
uno la confirmación de la sospecha: "yeah, voy un paso por delante,
que se jodan" :D

lunes, 12 de julio de 2010

Wasn't there a command to do XYZ?...

At $work, many times I'm asked about ways to optimize little annoying processes of other people (I'm a kind of office enabler).

There's a typical dialog of some $coworker, and $self.

- Hey Rai, I've got a little question for you.
- Ok, go on.
- Wasn't there a linux command to put lines of two files in one file, one line after the other? Like a columnwise join.
- M... yeah... I think so. Was it 'join'? Nah, man doesn't seem to say anything about it... Well, you can use Perl, you know, the swiss knife.
- Yeah, but I don't think a script is needed, For now, I'm doing it with excel, you can copypaste each file in different columns, and then save as CSV, then you just have to remove the commas, using vim.
- ¬_¬ .... Gimme 30 secs.

echo 'a' >a
echo 'a' >>a
echo 'a' >>a
echo 'b' >b
echo 'b' >>b
echo 'b' >>b
perl -e 'open $a,shift;open $b,shift; while(chomp($_=<$a>)){print $_, scalar(<$b>)}' a b

output:
ab
ab
ab