This should perhaps go ino a different section - and if a sufficiently blessed personage decides to move if then I have no problem with that

I have been thinking about the famed Schwartzian Transform as it is coming in extremely handy for some stuff I want to do. However, as seems to have been noted elsewhere*, one problem with the ST is that it is not exactly memory efficient in that you end up with a number of temporary copies of the array floating around requiring garbage collection. This is merely inconvenient for small arrays and small elements - but if you are sorting (as I am) large arrays of large complex structures (the results of XML::Simpled:XMLin() if you must know), then this can cause exciting disk swaps (and since I'm testing this on a windows PC even more exciting crashes when something fails to handle failed malloc()s properly) which rather defeats the point of the ST.

Once upon a time, I learned APL - a language which could perhaps be described as perl-like only terser - and APL has a built in array sort mchanism. The interesting thing about APL's sort routine (⍋ or ⍒ aka Del(ta) Stile) is that it returns you an array of indices into the original array rather than the sorted array itself. Thus, instead of doing (in perl notation)

my @sortedarray = sort @unsortedarray;
you actually do
my @sortedarray = @unsortedarray[sort @unsortedarray];
It occured to me that this idea could be used in the ST so that what gets passed around in the map{} sort{} map{} is in fact [$sort_target, $index] rather than [$sort_target, $entire_element] as this is much much smaller.

The code to do this is below and seems to both use less memory and be slightly quicker although I have yet to run this on a truly huge set of data

my @sorted = @unsorted[ map {$$_[1]} sort {$$b[0]<=>$$a[0]} map [expensive($unsorted[$_]), $_]; } 0..$#unsorted ];
Additional thought: (Inspired by diotalevi) There are probably a number of cases where you actually want to make further manipulations to the index to the data rather than the data itself. In such cases, the  @unsorted[ ] may be omitted.

Comments, (constructive) abuse, praise etc. welcome...

Dingus
*There is almost certainly a better discussion - (update) some others are noted in the replies below
(update misc typos fixed)


Enter any 47-digit prime number to continue.

In reply to An APL trick for the Schwartzian Transform by dingus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.