Skip to content

Instantly share code, notes, and snippets.

@0racle
Last active June 9, 2017 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0racle/134921795a6d91168f31cd554492d824 to your computer and use it in GitHub Desktop.
Save 0racle/134921795a6d91168f31cd554492d824 to your computer and use it in GitHub Desktop.
Sort Gotcha

Sort Gotcha

> <matthew mark luke john>.unique(as => *.chars).sort(by => *.chars)
(mark matthew)

Ahh good, my code works... except it's not really sorting by chars here because unlike unique, sort takes a code block not a named parameter. However there is no error to indicate this is not DWIM. It just silently sorts Naturally.

If your data happens to sort Naturally in the same order your by option would have sorted it - and you don't notice - you now have a bug laying dormant in your code that could go unnoticed indefinitely.

Sort should either handle this syntax, or otherwise throw up a warning;

I brought this up on #perl6 on 2016-04-10 and 2016-04-11 but no solution proposed.

@0racle
Copy link
Author

0racle commented Jun 9, 2017

This has confused another person here

moritz said

back in the days, we allowed calling positional arguments by name
during these bad old days, calling it with by => ... would have worked

Maybe unique needs a multi that accepts a positional &as, considering it's used way more than :&with, and it would make it more consistent with other List-y methods like sort, map, grep, classify, etc.

> < alpha beta gamma delta epsilon >.unique(as => *.chars)
(alpha beta epsilon)
> < alpha beta gamma delta epsilon >.unique(*.chars)
Cannot resolve caller unique(List: WhateverCode); none of these signatures match:
    ($: *%_)
    ($: :&as!, :&with!, *%_)
    ($: :&as!, *%_)
    ($: :&with!, *%_)
  in block <unit> at <unknown file> line 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment