Hacker News new | ask | show | jobs
by raiph 4564 days ago
The unary sort is useful for two reasons. It simplifies the code AND it does key caching.

But sometimes you need BOTH a key extraction closure (to get key caching for performance) AND a comparison closure (to specify a custom sort). In P6 you just specify both closures (P6 figures out which is which because one has one arg and the other has two).

Here's the P6 equivalent of your last couple lines:

    ["A","b","C"].sort: { .lc } # A, b, C
    ["A","b","C"].sort:  *.lc   # same thing
Here's a custom sort:

    ["A","b","C"].sort: { $^b.lc leg $^a.lc } # C, b, A
Now combining them:

    ["A","b","C"].sort: *.lc, { $^b leg $^a } # C, b, A
The latter will run faster.
1 comments

The latter doesn't appear to be implemented in any version of p6.
colomon is right. Thanks for pointing this out colomon.

Sorry HNers and P6ers.

I feel so embarrassed. I'm usually almost pathologically careful. I have an explanation if anyone wants it but I suspect an excuse is uninteresting so will skip that for now.

Instead I'll say I am now committed to trying to implement this (it's in the spec, just not yet implemented) by updating Rakudo's sort method:

https://github.com/rakudo/rakudo/blob/874e358fa5b09f94243003...

The good news is that it's (currently) about 10 lines of code. The bad news is they look very gutsy to me. Oh well. Open mouth, take responsibility, do your best and all that.