Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Schwartzian Transform as a module?

by Anonymous Monk
on Sep 30, 2002 at 20:04 UTC ( #201822=perlmeditation: print w/replies, xml ) Need Help??

After recommending the Schwartzian Transform to someone recently (and directing them to as a reference) I meditated that someone has probably written a module to encapsulate the Schwartzian Transform. I looked but couldn't see one.

I was thinking of something like:

$file_size_sort = new Sort::Schwartzian left => '-s $a', op=>'<=>', right =>'-s $b';
... later ...

@sorted = $file_size_sort->(@files);
$sort_by_score = new Sort::Schwartzian left => '$b->score', op=>'<=>', right =>'$a->score';

If such a thing doesn't exist I'm suggesting someone write one. Volunteers?

Replies are listed 'Best First'.
•Re: Schwartzian Transform as a module?
by merlyn (Sage) on Sep 30, 2002 at 21:32 UTC
Re: Schwartzian Transform as a module?
by chromatic (Archbishop) on Sep 30, 2002 at 21:02 UTC

    I don't think this is a good idea. A module should encapsulate complexity. Ideally, I shouldn't have to understand the inner workings to use it. I don't think this proposal achieves either goal.

    Given the interface you propose, I'd have to understand the Schwartzian transform to provide the correct arguments. Once I know that, it's more efficient (especially given the fact you're using a string eval to build the transformators) and shorter just to write it inline.

Re: Schwartzian Transform as a module?
by sauoq (Abbot) on Sep 30, 2002 at 20:55 UTC

    I don't think that a Schwartzian Transform module really makes sense. It's a simple technique. Trying to modularize it wouldn't effectively abstract anything because the hard part, the transformations themselves, are completely dependent on the data being sorted.

    In fact, you would likely code yourself into a corner if you tried to modularize the ST. In your suggestion, for instance, your Sort::Schwartzian object takes an "op" argument. That suggests that it wouldn't be easy to sort on two different fields with your module.

    "My two cents aren't worth a dime.";
Re: Schwartzian Transform as a module?
by Sweeper (Pilgrim) on Oct 01, 2002 at 05:38 UTC
    Take this example from the Cookbook (page 120)
    print map { $_->[0] }
          sort {
              $a->[1] <=> $b->[1]
              $a->[2] <=> $b->[2]
              $a->[3] cmp $b->[3]
          map { [ $_, (split /:/)[3, 2, 0] ] }
          qx(cat /etc/passwd);
    How do you modularize a multi-criterion sort? Of course, it can be done, but the resulting module invocation would be more cumbersome and less readable than an inline Schwartzian transform.
      sub ST(&@){ my $metric=shift; map {$_->[0]} sort {$a->[1] cmp $b->[1]} map {[$_,&{$metric}]} @_ } print ST{sprintf"%9d%9d%s",(split/:/)[3,2,0]} qx(cat /etc/passwd);
Re: Schwartzian Transform as a module?
by Silicon Cactus (Scribe) on Oct 01, 2002 at 14:41 UTC
    Oh thankyouthankyouthankyouthankyou!!!!

    The link you mentioned FINALLY explained both map and a schwarztian transform in a way that my little luddite brain can comprehend!

    I so very much wish that you had been logged on, because you deserve so MANY ++'s for that. I had just decided today that before I left my job for the day, I would understand map and the ST!

    Again, thank you from the bottom of my heart.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://201822]
Approved by grinder
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2020-10-31 00:44 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (286 votes). Check out past polls.