What I find interesting, and I hope I am not out of my depth here, please correct, is that in one of the four* (?) approaches to a solution, the so-called Schwartzian transform (ST) is used. I think (as I learned about this trick not that long ago) I have spotted ST on tybalt89's, kcott's and AnomalousMonk's solutions.

What I mean is that sort() is passed something different than just the original array or just its indices in the form of additional/pre-processed information. In this case it is the index of each element *as well* as its value in the form of a tuple-ref. E.g. where map passes on to sort [ $_ => $x[$_] ] or () (another trick I learned when you want map to filter(=grep) too). And so, sort does not have to constantly ask for $arr[$a], instead it is given this value and does $a->[1] (which is comparable in overhead but let's ignore this for the sake of argument).

On the other hand, johngg manages without ST because it passes only the index of each element after filtering out non-whole numbers. And so sort() does this: $arr[$a] <=> $arr[$b].

I am not posting any benchmarks because they are totally irrelevant as this is a pet-scenario toy problem.

Now, consider that the problem was the following: For the same array, find the index of the element with the highest prime factor.

This is a blindingly obvious case for the use of ST because there is no contest between:

sort { $a->[1] <=> $b->[1] } map { $arr[$_] >= 0 ? [ $_, max_prime_factor($arr[$_] +) ] : () } 0 .. $#arr

and

sort { max_prime_factor($arr[$a]) <=> max_prime_factor +($arr[$b]) } grep { $arr[ $_ ] >= 0 } 0 .. $#arr

----

four* : 1) swartzian map/sort, 2) non-swartzian map/sort, 3) List::Util et al, 4) PDL


In reply to Re: How to get the index of smallest whole number in an array? by bliako
in thread How to get the index of smallest whole number in an array? by sohamsg90

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.