Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: What is "Schwartzian Transform"

by kelan (Deacon)
on Jul 21, 2005 at 12:28 UTC ( #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? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2022-08-13 10:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?