in reply to Schwartzian Transform
I'm trying to better understand the Schwartzian Tranform (sorting a list by computable field). I've read the explanation in the Perl Cookbook, 2d ed. recipe 4.16. I also enjoyed Randal's 1996 article in the Unix Review, which describes a sortable-hash technique before it goes on to show the map-sort-map transform technique. The sortable-hash technique intrigues me.
Other than coolness and nicer-looking code, what is the advantage of this:
over this:my @output = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [$_, expensive_func($_)] } @input;
foreach $_ (@input) { $result_for{$_} = expensive_func($_); } my @output = sort { $result_for{$a} cmp $result_for{$b} } @input;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Schwartzian Transform vs. Building Hash of Function Results, Then Sorting on Function Results
by merlyn (Sage) on Oct 16, 2006 at 19:27 UTC | |
Re: Schwartzian Transform vs. Building Hash of Function Results, Then Sorting on Function Results
by Limbic~Region (Chancellor) on Oct 16, 2006 at 19:10 UTC | |
by davebaker (Pilgrim) on Oct 16, 2006 at 19:35 UTC |
In Section
Cool Uses for Perl