Sunday, November 28, 2010

Schwartzian transform explained

Did you google for "explain schwartzian transform"?

If yes, then you've come to the right place. If no, then you've come to the right place (I wouldn't want to loose out on a prospective reader now, would I?). Please read on - you will find this worth your time.

This post aims to dissect the Schwartzian Transform, and explains it through an example. And while doing this, it shows how cool Perl is.

But what is the Schwartzian Transform? - A way to efficiently sort a list of items.

Why the name? - Legend has it that Randal "Merlyn" Schwartz (wiki page) demonstrated a Perlish version of a Lisp idiom to speed up sorting. And since that day, every time you use the word "Schwartzian Transform", somewhere far beyond the distant seas, the Randal smiles.

How does it speed up sorting? - Patience my child. The blooming of a flower is an analog process, revealing each petal gracefully unlike the open-throw-catch-roll-close process of a frog's tongue. (Yummy?)

Why do I need to know it? - Because it's fun.

Will each line be preceded by a question in the bold font? - Not from now on.

Suppose you have an array of salesman objects - @salesmen, each of which consists of the following attributes:

1. name           # salesman name
2. base_salary    # base salary
3. n_cust_conned  # number of customers conned
4. comm_per_sale  # commission per sale

And the sub routine which calculates the total salary of the salesman is:

sub get_total_sal {
    my ($obj) = @_;
    my $total_salary = $obj->{base_salary} + 
                       $obj->{n_cust_conned} * $obj->{comm_per_sale};
    return $total_salary;
}

If you were told to sort this array based on the total salary, what would be your code snippet which does the sort?

Without prior knowledge of the transform, maybe something like this:

my @sorted_salesmen = sort { $a->get_total_sal() 
                             <=>
                             $b->get_total_sal() } @salesmen;

This would do the job. But what limitation does it induce in your code?

Think about it.

Perl program to simulate tail -f

Created a github account to share my bin with the rest of the world.

Had written a perl program to simulate tail -f quite some time ago. Put it up there. Enjoyed writing it. Hope you feel the same while playing with it.

Here it is: simtail.pl

Bounced back to blogspot

#!/usr/bin/perl
use strict;
use warnings;
print "Because you cannot install plugins on wordpress.com blogs";
print "Which means I cannot install this cool plugin which";
print "lets me indent and syntax highlight my Perl code\n";
exit(0);

Wordpress.com has the <code> tag - which for the average programmer means that I can use this tag to copy paste my code on the blog and it will show up all nice and indented. But from the usage, code tag just "encodes" HTML i.e. you don't need to escape < tag in #include <stdio.h>

The code tag doesn't indent. It doesn't highlight. It just converts the text to code font. The forums tell you to post your code in pre tags. Pre tags work but not always. As per Shuvam's comment below - the quirk might be with the specific template I am working with. So I created a test page on wordpress -

Test page to check code layout on wordpress.com

and tried it out with a 10-15 initial templates. None of them did the trick. I am not going to try out the 100+ templates that they offer and neither would I compromise on the rest of the layout just because some theme shows the pre tags cleanly.

Wednesday, November 17, 2010

Moved to wordpress

Wanted to play with wordpress for quite some time and also wanted to see how difficult/easy it is to migrate posts from blogspot to wordpress.

Verdict: It don't get any simpler than this. Wordpress has an inbuilt import feature. All you need to do is to point it to the blogspot blog url and it asks whether you are allowing this or not. If you give it a nod, it does what's needed. Clean.

Point and click - Here