Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • My vote for the meaning of sort in scalar context is to return whether the list was already sorted.

    unless( sort @list ) {
        # @list is not sorted, deal with it...
        ...
    }

    If you want to go beyond a simple boolean, the index of the first out of order element sounds a lot more useful than the proprtion of sorted neighbours - and, like the boolean result, it can short circuit the scan of the list as soon as an out of order pair is found.

    • to return whether the list was already sorted

      An is_sorted @list predicate would be an interesting builtin/XS/Inline function, and should run faster than sort (if coded equally well). It should run about the same speed as a least @list sort-variant optimized to return (sort @_)[0].

      However, I'm not sure is_sorted should be called scalar sort, that doesn't seem any better than calling least as scalar sort. Changing sort from verb-transitive to verb-predicate when going from list to scalar context is rather drastic. Someone who expected to get the size of the list -- not knowning the list returned by the function that code containing return sort soemthingelse @_ is returned by sort -- might foolishly trust the 0 and 1 as list size. In merlyn's context of return sort @somelist, I suggest the right answer is either what "everyone" expects (list ops return size in scalar context unless something else is monstrously obvious, however monstrously wrong that assumption is) is the only valid option other than the current undef-and-warn-if-warnings, which is highly defensible, it at least announces it's broken (unless -w is off, in which case the caller is borked anyway).

      --
      Bill
      # I had a sig when sigs were cool
      use Sig;