Wednesday, February 25, 2009

Preprocessor directives for ruby (not!:)

At first I thought to do some more link-whoring - as I found precisely the search terms that are in the title of this post to lead to this blog... But then I though I will limit the on-topic information on the matter:

1) You don't need a preprocessor for Ruby
2) If you think you do, read up on metaprogramming - _Why's article will be a good start to spend a good time thinking.

But, this lead me back to the CPP - the preprocessor. Of course, beyond the Obfuscated C contest entries that are of parlor entertainment value only, there is some more to that.

Take a look at this one.

Then get it for infinite fun:

cvs login
cvs -z3 co -P chaos-pp
cvs -z3 co -P order-pp

And then go check out and mess with the examples:

the .c file looks like:

int main(void) {
("The 500th Fibonacci number is "
return 0;

// Unless you are already convinced, you should check, by
// preprocessing this example, that the actual parameter to
// `printf' in the above code is just a single string that
// contains the 500th Fibonacci number in base 10.
// If you know the order of growth of Fibonacci numbers, you
// probably realized that the result isn't exactly small.

Now, let's try it (stderr forwarded to the null for brevity, as it seems to spit some errors):

:~/tmp/order-pp/example$ cpp -I../inc fibonacci.c 2>/dev/null | tail
return fib_iter(n, 0, 1);
# 63 "fibonacci.c"
int main(void) {
("The 500th Fibonacci number is "
return 0;

Now, let's look at another example - average.c

you define the macro AVERAGE as:

#define AVERAGE(...) \
((ORDER_PP(8seq_for_each_with_delimiter \
(8put, \
8emit(8quote(+)), \
8tuple_to_seq(8quote((__VA_ARGS__)))))) / \

and use it in your C code like:

int main(void) {
printf(AVERAGE(3, 1, 4, 1, 5, 9, 2, 6, 5) == 4
? "OK.\n"
: "ERROR!\n");

return 0;

The code post-cpp looks like:

int main(void) {
printf(((3 + 1 + 4 + 1 + 5 + 9 + 2 + 6 + 5) / 9) == 4
? "OK.\n"
: "ERROR!\n");

return 0;

Of course, it's only simple things like this that are so nice looking... Any nontrivial definitions cause giant single-liners - the worst nightmare for the debuggers and maintainers - so I would personally torture anyone who used this in the production code.. :)

Nonetheless - pretty impressive stuff.

The FONC project & potion

While messing around looking if anyone did anything with potion language, that I wrote about a few days ago, found this interesting link: - The Fundamentals OF New Computing. Has some interesting reading on research in programming languages.

Potion itself is rather fun, though of course my microbenchmark was a bit of an over-joyous :)

Yeah, while it looks to be pretty damn fast, LuaJIT is only about 1.7 times slower. Nonetheless, I'll play with it more, it is an interesting thing to mess around with - at least the internals. Syntax I am not so sure...

Foo = Object delegated
Foo def("xx", (x): "foo\n".)

Bar = Foo delegated
Bar def("yy", (x): "123\n".)

a = Foo allocate
b = Bar allocate

a xx print
b xx print
b yy print

Running this with potion will give you three lines "foo", "foo", and "123" - the middle one being the method of "Bar", "inherited" from "Foo". Did not dig on how to glue multiple mixins - _why wrote that it's incomplete, I'll see maybe some other day.

Saturday, February 21, 2009

What the world is interested in...

The other day, while searching for a "how to" on something, I became amused with the autosuggestions I got after typing in the "how to" into the search input editor.

Assuming the results there are sorted by the interest from the users (which is something pretty obvious to do), this is the apparent hit parade of the things
that the people are wondering about:

1. how to tie a tie

Ha. I wonder whether this stems from the fact that the people don't really wear a tie every day, so this knowledge gets into the dark corners of the forgotten, or the science of tying the tie is as complicated as actually spelling it. I've last had to use this search maybe half a year ago when I in fact needed to wear the tie to some place. The caching is a good optimization here - tie the tie once, and keep it tied. Saves a lot of time.

2. how to kiss

Something is wrong. This one was supposed to land on the first position IMO - as the potential audience for the ties is at most is half as big as the potential audience for the kisses. So, I can conclude that the sets are actually very much intersecting and it's the same folks who don't know how to tie a tie. Others either already know it or are practicing it off-line. Indeed, practice is the key here. Instead of optimizing, just brute-force it. The process is fun.

3. how to get pregnant

Now the mystery of the bias for the previous one is probably starting to dissolve - that second half is actually not wasting the time on paying too much attention to the preamble, and is much more result driven. I'd think that here the practice is also a key. Also, there's something about the days of the month and such. But with sufficient practice the distribution is going to be across all the days of the month, so it should not be that important.

4. how to make money

assuming your looks are ok - after all, the tie is an important part of it! - indeed, money might be of use sometimes. I think it's a simple economics - find the area with most demand that is unsatisfied, and provide a supply there. Even better - create the demand out of thin air - that's how you make new markets. The most creative and spectacular that readily comes up, is the one million pixel page. In a nutshell - create 1000x1000 image, and start selling it in blocks of 10x10. Hundred bucks each. I am in awe.

5. how to lose weight

I won't comment much on this one - it's outside of my experience. Although, a full long weekend of dedicated nonstop gaming with not much food inbetween gets quite a few kilos off. I accidentally tried that once - the result was pretty funny (needless to say I was dead tired after that). But I think that's not something anyone should follow - talk to your doctor, or something. Eat lighter food, do sports, and such.

6. how to draw

I wonder if this is about drawing as in a form of art or it about making sure that no-one wins. My best advise - find an artist nearby who is willing to bear with you, and use the results from the (4) above to pay for the lessons. Practice makes miracles. But it aint going to be easy, if you want to get decent that is.
My wife's an artist and she literally spent years doing that thing. (oh, if you wanna get lessons, maybe I can ask, drop me a note :)

7. how to make a website

Hire a professional.

8. how to cook a turkey

Ah, this is a much more appropriate one. Though, no opinion here - I'm in europe and prefer chicken anyway. Or beef, for that matter.

9. how to write a resume

I'd do it in a couple of steps: first achieve a bunch of great things. Then write them down. Though, it's not a quick process. But should surely work.

10. how to lose friends and alienate people

Oh, that's easy - just write something outright silly like I did in this post, and it might work. Or go watch the film and learn from there.

Wednesday, February 18, 2009

Sketching network diagrams - entirely in javascript, in your browser!

This is a bit of shameless self-advertising - I figured that if I don't advertise a little bit, I never know if there are some people that might benefit of this thing.

Basically, if you want to scratch a network with a couple of routers and a cloud, now you do not need to start visio, dia, or anything else for that matter: just go to, draw the network, and snap the picture from the screen.

Of course, it's all web2.x enabled and jazz, so you can save the layout as JSON, and load it later. Even, in a demonstration of a blatant violation of the security, you can pull in the JSON from another domain.

A little bit of history: as I use linux at work pretty much exclusively, I was always curious to have something simple to draw the diagrams. Dia is an excellent program, but I wanted something even simpler.

And so when I saw the Draw2D library I thought "ah-ha!".

The editor is not way too much more than a combination of the existing components - you can see the ingredients on its page.

Well, enough of me talking, go try it and let me know :-)

Monday, February 16, 2009

Potion for x86

Looking around, I stumbled on something interesting. Potion.
The source has an example:

fib = (n):
if (n <= 1): 1. else: fib (n - 1) + fib (n - 2)..
fib (40)

Interesting. Looks a bit like OCaml/Haskell. Sort of.
A microbenchmark for calculating fibonacci number in a very inefficient way :-)
Let's time it:

$ time ./potion example/
real 0m17.924s
user 0m17.909s
sys 0m0.008s

Now I'll try the similar-looking lua code:

function fib(n)
local ret = 1;
if (n > 1) then
ret = fib(n-1) + fib(n-2)
return ret

Timing it:

$ time lua fib.lua
real 2m18.566s
user 2m18.089s
sys 0m0.048s

Now, with all my non-love to microbenchmarks, this does look quite interesting.
If this baby has zero-based arrays, I might get seduced :-)

Tuesday, February 10, 2009

One-shot makefile to install lua, prosody, and associated libs...

I've a couple of systems that I do not want to touch the "system-wide" packages on, so I hacked the following makefile to do a one-shot install of prosody. Pretty darn ugly, but does its job. Maybe could be of use to someone...(NB: beware of horribly long lines that are wrapped by the display!)


moved it to as blogger is much better suited for text.

Monday, February 9, 2009

The FOSDEM'09 memories of a yellow T-shirt...

...or - "how we had a lot of fun and a little bit of pressure at FOSDEM 2009".

I planned to attend Fosdem, but I was pretty delighted to hear that Jerome managed to get our employer Cisco to provide the wireless networking gear for the event, so jumped in to grab the opportunity to give back to the community, and meet the new friends.

It was a lot of fun - thanks to Jerome, Peter, Seb, Antonio, and of course to Fosdem organizers, staff, and indeed the visitors - folks, you rock! :)

We had about 60 access points scattered around the area, so the wireless controller proved to be a very handy beast (it reduced the number of entities to actually "manage" to 3).

Of course, being first time and all, it was quite a few hiccups - ranging anywhere from cables to configs to users "helping" by misconfiguring their gear to do an Ad-Hoc network with the same SSID, and me being slow like a snail in the morning :)

The hardest things for me were escaping from the office at 4:30pm on Friday, and waking up before 10am on Saturday.

But hopefully we might repeat the experiment next year and try some improvements - the first on the wishlist being the faster-than-halfduplex-10mbps uplink to internet - if we manage to borrow the toys again :-)

Correction: "60" was the number of "unknown/rogue APs", we had only 37.

Sunday, February 8, 2009

It's a small world after all...

When we were enjoying the last hours of warm spanish weather in Barcelona on 29th January after Networkers, a few of us went to Parc Guell. It was pretty much closed, so we had a beer in a cafe which was conveniently situated here to take the profit from the wave of the visitors. And then on the way back I heard a man playing medieval Lute. Inside the ancient column-rich area - so the acoustic qualities of the area were fantastic.

Having devoted a nonzero portion of my life to the classical guitar, I can fully appreciate the richness of this instrument - and maybe given its specific non-mainstream nature, the only music I get a chance to hear with it has quite a quality. As we did not have the time to listen, I bought a CD right there. And today I finally got to slide it in... and it's pure joy. So, looking at the cover of the CD I find the name of the person, and punch it in the facebook - "Hey, maybe you do not remember me. You play great. How can I get more?". Amazing how simple it is.

Possibly some 10 years from now we will be able to get connected with someone via even a less material token of information - maybe just by recalling their image from our memory. And the world might become a much happier place then.

Or not.

Friday, February 6, 2009

Refactoring - when I don't want to do something else


With my hobby code, I've caught myself thinking refactoring mostly when I realise that the next chunk of code I am going to write is going to be inherently ugly or large and boring. A very good recipe which I found against that is to put the thing on the shelf for a week or two, and let the brain work in the background - the power of the subconscious "coprocessor" is very well suited for that. Usually in this kind of time frame I'd have the "eureka" moment which unlocks the process and I happily hum along till the next "bore-block".

Maybe that't the reason why I don't code for food.

Sunday, February 1, 2009

Finally getting a bit more of IPv6...

While I was at Networkers, Eric has sent me a nice teaser - a Facebook app to test the IPv6 connectivity. I've played with "getting IPv6" a while ago, but wanted to give it another try. Yesterday I spent a long while mucking with OpenVPN + a tunnel to my internet host that would connect to V6 via 6to4. Slow and painful - if running 6to4, I'd rather run it locally... Alas - the DSL box does not have enough memory to run a V6-enabled IOS.

As a result, I've now split the single-head setup into dual-head, with one (older) router functioning as a modem + bridge, and the other one running the PPPoE client and doing all the cool IPv6 stuff.

I'll write up in a bit more details on how I have it in the upcoming days (leave comment if I want it sooner!), meantime a menu of nice links: