News for hackers. Stuff that matters.
Re:Perl6: Unary Sort by rmc on Monday December 23, 2013 @08:32AM (#6954138)
Python has this with the "key" argument to "sort".

    list.sort(key=lambda x: x.lower())
Re:Perl6: Unary Sort by maxerickson on Monday December 23, 2013 @12:36PM (#6955400)
You can also pass in the method from the str type:

    key=str.lower
It will be faster, but it will break on elements that aren't strings (which could be good or bad).
Re:Perl6: Unary Sort by koenigdavidmj on Monday December 23, 2013 @03:24PM (#6956331)
Then you can use

  key=operator.methodcaller("lower")
and handle any type.
Re:Perl6: Unary Sort by _ZeD_ on Monday December 23, 2013 @09:39AM (#6954429)
witch is a little different thing.

python can sort according to a function (using the "key" parameter) or using a custom comparator function (using the "cmp" parameter)

as a side note, Python offers also a sorted[0] function, applyable to any sequence

[0] http://docs.python.org/2/library/functions.html#sorted

Re:Perl6: Unary Sort by orblivion on Monday December 23, 2013 @09:42AM (#6954446)
I don't get the difference.
Re:Perl6: Unary Sort by _ZeD_ on Monday December 23, 2013 @12:42PM (#6955436)
They resolve differenti problems: say you have a list of complex object, and you wanna sort by an attribute, you can use

    mylist.sort(key=lambda element: element.attribute)
Elsewere, say you want to sort a non-omogeneous list, or according to a custom order (attribute1 descending, attribute2 ascending) cmp make this easy
Re:Perl6: Unary Sort by philh on Monday December 23, 2013 @01:05PM (#6955570)
I think ve was saying, ve doesn't see the difference between python's `sort(key=...)` and perl6's `sort { one-arg block }`. (At any rate, I don't see that difference.)
Re:Perl6: Unary Sort by rmc on Tuesday December 24, 2013 @12:23PM (#6960196)
Elsewere, say you want to sort a non-omogeneous list, or according to a custom order (attribute1 descending, attribute2 ascending) cmp make this easy

You can do this in python with the key function returning a list/tuple:

    mylist.sort(key=lambda element: (element.attribute1, -element.attribute2))
Re:Perl6: Unary Sort by _ZeD_ on Wednesday December 25, 2013 @04:56AM (#6962715)
I'm not sure you can do -"string" :)
Re:Perl6: Unary Sort by dded on Monday December 23, 2013 @12:47PM (#6955474)
The Python .sort() method sorts in-place. The sorted() function returns a sorted copy, leaving the original untouched.
Re:Perl6: Unary Sort by raiph on Monday December 23, 2013 @07:33PM (#6957542)
I realize this is drifting off topic, and I could well believe the current Rakudo implementation doesn't actually save the memory (I could believe it does, I just don't know) but P6, for any method, including sort:

    my $a = [2,1]; say $a.sort;  say $a # 1 2 2 1
    my $a = [2,1]; say $a.=sort; say $a # 1 2 1 2
This riffs off the general op= syntax:

    my $a = 1; say $a +  1; say $a # 2 1
    my $a = 1; say $a += 1; say $a # 2 2
Re:Perl6: Unary Sort by orblivion on Monday December 23, 2013 @01:06PM (#6955573)
I meant I didn't understand the difference between Python's sort(key=..) and what's described here for Perl.
Re:Perl6: Unary Sort by _ZeD_ on Monday December 23, 2013 @01:16PM (#6955626)
what is described for perl is python's sort(cmp=...)
Re:Perl6: Unary Sort by orblivion on Monday December 23, 2013 @04:48PM (#6956714)
Hmm? I thought the whole point of the article was "Most people know about sort(cmp=), which takes two parameters. But check out sort(key=), it only takes one parameter, which means the key function only has to run once per item", but for Perl.
Re:Perl6: Unary Sort by raiph on Monday December 23, 2013 @07:58PM (#6957630)
That's my understanding too.

Lisp had a nice idiom that Randal Schwartz translated in to Perl 5 in 1994 which became known as the Schwartzian Transform: http://en.wikipedia.org/wiki/Schwartzian_Transform

It seems from the comments here that Python's key and cmp args are Python's equivalent to the ST. I'm surprised by the comment that Python 3 removed cmp. Perhaps that's a temporary omission.

This HN is about an advent article for Perl 6, which covers the same territory. Based on a look at the Rakudo code, it only implements a single custom sort closure (equivalent to Python's cmp) OR a single key closure (equivalent to Python's key).

Re:Perl6: Unary Sort by orblivion on Tuesday December 24, 2013 @02:52PM (#6960854)
> I'm surprised by the comment that Python 3 removed cmp. Perhaps that's a temporary omission.

Guido and company insist that you can (almost?) always find a way supplying a key function. For better or worse, they decided it was the best way, and going by the Python philosophy they removed anything that isn't the one obvious right way to do it. Perhaps it's available in a library somehow, that's what they did to other things that are unnecessary or useful 99% of the time but may still be 1% of the time.

Similarly, Guido once almost removed lambda, but he faced an insurrection.

Re:Perl6: Unary Sort by jasomill on Tuesday December 24, 2013 @05:56PM (#6961450)
From the Python 3.2 standard library functools.py:

    def cmp_to_key(mycmp):
        """Convert a cmp= function into a key= function"""
        class K(object):
            __slots__ = ['obj']
            def __init__(self, obj, *args):
                self.obj = obj
            def __lt__(self, other):
                return mycmp(self.obj, other.obj) < 0
            def __gt__(self, other):
                return mycmp(self.obj, other.obj) > 0
            def __eq__(self, other):
                return mycmp(self.obj, other.obj) == 0
            def __le__(self, other):
                return mycmp(self.obj, other.obj) <= 0
            def __ge__(self, other):
                return mycmp(self.obj, other.obj) >= 0
            def __ne__(self, other):
                return mycmp(self.obj, other.obj) != 0
            __hash__ = None
        return K
Re:Perl6: Unary Sort by comex on Monday December 23, 2013 @02:40PM (#6956116)
And incomprehensibly, Python 3 removed the cmp argument, leaving only unwary sort.