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

Re^3: What is "Schwartzian Transform"

by kelan (Deacon)
on Jul 21, 2005 at 12:28 UTC ( [id://476793]=note: print w/replies, xml ) Need Help??


in reply to Re^2: What is "Schwartzian Transform"
in thread What is "Schwarzian Transform" (aka Schwartzian)

The usual use is when the sort key must be derived from the data using a process that takes significant time/resources. The idea of the ST is to precalculate all of the sort keys before the sort operation so you only spend the time once:

@sorted = map { $_->[ 1 ] } sort { $a->[ 0 ] <=> $b->[ 0 ] } map { [ expensivefunc( $_ ), $_ ] } @data;
Doing a naive sort, you would be calling that expensive function twice for every comparison, which would end up being a lot more than when precalculating:
@sorted = sort { expensivefunc( $a ) <=> expensivefunc( $b ) } @data;

Replies are listed 'Best First'.
Re^4: What is "Schwartzian Transform"
by merlyn (Sage) on Jul 21, 2005 at 15:56 UTC
    In all recent examples, I've moved the "original key" to tuple element 0, so that the first step is consistently  map $_->[0] regardless of how many "expensive" keys are computed.

    Just a minor nit.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://476793]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-16 13:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found