Category Archives: Uncategorized

Inspired by the latest Ruby Quiz: IP to Country: #139, I coded up a utility to map ip addresses to country codes in erlang. It’s available on google-code at http://code.google.com/p/ip2country-erlang/.

Some experiences while making this thing work:

  1. It took me the better part of six hours to get this running. I suspect if I’d done this with Ruby, I’d have completed an elegant, fast solution in less than a half hour. However I did make this harder for me (see below).
  2. It uses the in-memory database that Erlang has: mnesia. There’s functions in there to populate the mnesia table with the csv file and then perform lookups on it.
  3. Higher-order functions is the name of the game. For just about every useful thing from looping down to iteration, to mapping elements of a list. Coming from a non-functional background, I find this very hard to grasp still. Also: non-mutable state. Count +=1 doesn’t work. [So how do you implement a counter? Recurse with a parameter].
  4. I miss the Ruby Standard Library very very badly. Of all the languages I’ve used, Ruby’s is the most concise and clear and manages to have just about every function you need in there. String manipulation in Erlang esp. is very cumbersome. Somebody probably should port over all the functions asap. lists:join, string:delete, string:delete_if etc. would be invaluable.
  5. The comma, semi-colons and fullstop craze (along with proper indentation) is sometimes very confusing. The rules are actually easy to remember, but when I nest functions using funs, I find I lose track of where I am and what’s supposed to happen. I started programming this using Aquamacs but it crashed on me midway while using the inbuilt shell. I hate stuff which crashes so I switched to Textmate’s erlang mode which isn’t as functional but does a decent job of parenthesis matching and auto-indent.
  6. Mnesia is a joy to use. I spent a lot of time just playing around with many examples because it’s fun to make a record, dump it and use list comprehensions to perform selects. It’s way more intuitive than using SQL. Witness this fragment:
    
      qlc:q([X#ip_address_map.country_code || X <- mnesia:table(ip_address_map),
    		     X#ip_address_map.range_from =< Ip_address_as_integer,
    		     X#ip_address_map.range_to >= Ip_address_as_integer
    			]).
    
    

    That actually just says: get the country code for all X where X is an mnesia table of type ip_address_map and where X’s ip_ranges are within range_from and range_to. No SQL and exactly as you’d do within the language.

  7. It feels very awkward to get Erlang to be a shell scripting language. I’m not sure escript is even supported anymore but it does work. Erlang is not for scripting.
  8. The whole exercise reminded me of my extreme newbiness. I’ve been dabbling off and on in Erlang for over four months now and still I’m not at a stage where I can code in it off the top of my head (this hasn’t happened before with any language, but admittedly all the others were just procedural ones). The problem is I’m not using it for anything big. That should hopefully change someday. [Not much hope, but does somebody want to hire me to work on erlang or lisp? :-) ]
  9. The erlang mailing lists help a lot. Just being there you soak up a lot of info.
  10. Concurrent erlang is another ball game altogether.

Get started!

I’ve just debuted a new way to stop comment spam on this site (#5 on this list). It’s inaccessible for people with Javascript turned off, but even Akismet wasn’t helping enough for me.

So here’s a SlideShare/Twitter Mashup (command-line ruby code) that does these things:

  1. Gets buzzwords from Twitter. It does this by analyzing tweets and getting popular words (filtering out common ones) with the Twitter API.
  2. Gets the most popular buzz word and searches the SlideShare tag database with it (using the SlideShare API).
  3. Prints out the buzzword and the slideshows that’s associated with it.

The source code might not be a great example of filtering and getting popular words, but it’s a good demo of how simple the Twitter and SlideShare APIs are (REST yay) and how easy Hpricot makes parsing XML docs.

There’s a zip (with the source code, common_words.txt, twitter_words.txt) here: slideshare_twitter.zip. Enjoy. :-)

This is a talk I gave at Torque today. It’s meant to a very basic intro to SCM with Subversion and Trac.

Just an observation. If my bittorrent goes flat out, I can download a movie in ten hours. In a day, I can download two movies, which is about what a healthy adult should watch in a day. I wonder what’ll happen when my bandwidth doubles. (Oh, and people who only go by DVD-rips – or worse – can… wait.) So what did I watch recently? Bourne Ultimatum, Bridge to Terabithia, and Transformers. In reverse chronological order.

The new orkut look is frankly amazing. It’s got the Google sense of simplicity finally and looks much less crowded than even Facebook! (if it hasn’t rolled into your account yet, it will).

Ponder this:

fact (0) ->
	1;
fact (N) ->
	N * fact (N - 1).

versus this:

fact(N) ->
	fact_helper(N, 1).
fact_helper(1, T) ->
	T;
fact_helper(N, T) ->
	fact_helper(N - 1, T * N).

The advantage of learning Erlang (albeit very slowly, with lots of interruptions) is that it directly introduces a lot of concepts I’ve been marginally aware of before. For instance, the second example implements factorial using tail recursion. The advantage is that a compiler doesn’t have to implement a call-stack when playing with arguments. See wikipedia entry.

Yesterday, I got into a Vallam and traveled from Malakkara to Aranmula and back. It was wonderful.

Since I’m a newbie at everything from wearing a mundu (dhothi) properly to holding the nayambu (oar), it was a pretty interesting experience at first. You have to madakikutthu the mundu (fold the mundu over so that it looks like a knee-length skirt) so that you can sit comfortably in it and after a few tries, I managed to do that. [aside: I'm pretty sure true-blood Malayalis would wince at this point. My only advice: please don't read on]. The vallam itself is about twenty to thirty metres long and seems to weigh a ton, but it’s constructed so well that just a finger-touch can move it on the water (trust me, I tried it). When about forty people get onto it, it remains amazingly steady and sure, although the getting in (and out) is an experience that’ll rival a roller coaster.

There are planks that go cross-wise the breadth and parallel to it from the edges so that you don’t usually step into the wet bottom of the boat. You sit at the very edge locking your legs back and across and it’s probably the closest I’ve come to the water without getting wet. It was also interesting learning how to row properly. The nayambu is a thick wooden oar with a longer handle. There’s a circular piece at the top that you’re supposed to grip with one hand, the other holding the top of the flaring edge firmly. And then you row. Easier said than done of course :-) . After splashing my fellow boatmates about a thousand times, I managed to get the hang of it. A chetan sitting behind me (who bore much of the brunt of my attack on water) commented on the return-trip that for a newbie I did it very well indeed [this made my day :-) ].

What distinguishes the Aranmula Vallam from its variations across Kerala (and the world) is the Vallapattu (boat-song). Resonating exactly with the rhythm of the oars, it’s a beautiful counterpoint to the entire experience: if the song moves faster, you instinctively row faster, and it’s so constructed (and sung) so that there are periods of relative inactivity and bursts of speed. It’s uber-wonderful.

The most common verse is this:

Thithithara thithithey thithey theke they they tho[..]om

The ‘th’ is a sound which is not in english: a cross between the t in ‘them’ and ‘tarrif’, also notice the alliteration. The [..] stands for a longer -o-, lengthening with an increase in exuberance.

It took around forty-five minutes to travel upriver and about ten minutes less to travel down. Not exactly fast (takes about ten minutes by car). I’ll estimate the top speeds to be around 25kmph, although this was not a race. I’m pretty sure though that more than the rowing, it’s the people who stand at the very behind of the boat (at an increased height) and who steer it – esp. the person who holds the odanayambu – who holds the key to a victory in a race. The rowers pretty much row as they please and unlike canoeing, it’s a game more of enthusiasm still rather than skill. I expect someone enterprising will change this soon: just off the top of my head, introducing pacing techniques and regular practice will increase the average speed of a boat considerably. Of course, then, it’ll cease to be fun and be serious sport, which is not the idea.

Me? I had a lot of fun.

Also see all the photos at Flickr, provides a really nice glimpse of the Onam world.