Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Implement the 'reverse' option for sorted() #162

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion runtime/builtin_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ func builtinRepr(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
return s.ToObject(), nil
}

func builtinSorted(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
func builtinSorted(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that reverse can be passed positionally as well:

>>> sorted([1,2,3], None, None, True)
[3, 2, 1]

This is a little tricky to get right with this Args, KWArgs calling convention. You need to check that the argument is supplied in args, but not kwargs, or vice versa. CPython with the PyArg_Parse*() functions. We don't yet have such a utility.

One option that does exist now is to create a Code object for the builtin which will do the basic argument validation for you. It's a little more involved than newBuiltinFunction though:

var builtinSortedArgs = []FunctionArg{{"iterable", nil}, {"cmp", None}, {"key", None}, {"reverse", False.ToObject()}}
func builtinSorted(f *Frame, args []*Object) {
  // args is guaranteed to be a slice of length 4 with no nil elements
}
...
"sorted": NewFunction(NewCode("sorted", "<builtin>", builtinSortedArgs, 0, builtinSorted), nil).ToObject(),

This could probably be simplified with some kind of newBuiltinCodeFunction() helper or something.

So there's currently no great out of the box options for doing what you need to do, but it seems like we're going to need to establish a pattern here before long.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should not implement this in sorted(), but in listSort and just change the sorted implementation here to call

-func builtinSorted(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
+func builtinSorted(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
        // TODO: Support (cmp=None, key=None, reverse=False)
        if raised := checkFunctionArgs(f, "sorted", args, ObjectType); raised != nil {
                return nil, raised
@@ -579,7 +579,13 @@ func builtinSorted(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
        if raised != nil {
                return nil, raised
        }
-       toListUnsafe(result).Sort(f)
+       // Creating newArgs by replacing the first element with teh new list.
+       newArgs := args.makeCopy()
+       newArgs[0] = result
+       _, raised = listSort(f, newArgs, kwargs)
+       if raised != nil {
+               return nil, raised
+       }
        return result, nil
 }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

// TODO: Support (cmp=None, key=None, reverse=False)
if raised := checkFunctionArgs(f, "sorted", args, ObjectType); raised != nil {
return nil, raised
Expand All @@ -580,6 +580,14 @@ func builtinSorted(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
return nil, raised
}
toListUnsafe(result).Sort(f)
// Implement reverse.
reverse, raised := IsTrue(f, kwargs.get("reverse", None))
if raised != nil {
return nil, raised
}
if reverse {
toListUnsafe(result).Reverse()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you mention, stability of the results is a concern here. If we supported the cmp param, we could negate the result of that to achieve reversal. This would mean that listSorter would need to support cmp. Then sorted(... reverse=True) could wrap that cmp and negate the result (or provide a stock reverse cmp when cmp=None).

So maybe it makes sense to implement cmp first?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack. But I would prefer to implement this in listSort and changing sorted() to

-func builtinSorted(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
+func builtinSorted(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
        // TODO: Support (cmp=None, key=None, reverse=False)
        if raised := checkFunctionArgs(f, "sorted", args, ObjectType); raised != nil {
                return nil, raised
@@ -579,7 +579,13 @@ func builtinSorted(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
        if raised != nil {
                return nil, raised
        }
-       toListUnsafe(result).Sort(f)
+       // Creating newArgs by replacing the first element with teh new list.
+       newArgs := args.makeCopy()
+       newArgs[0] = result
+       _, raised = listSort(f, newArgs, kwargs)
+       if raised != nil {
+               return nil, raised
+       }
        return result, nil
 }

}
return result, nil
}

Expand Down
5 changes: 5 additions & 0 deletions runtime/builtin_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ func TestBuiltinFuncs(t *testing.T) {
{f: "sorted", args: wrapArgs(newTestDict("foo", 1, "bar", 2)), want: newTestList("bar", "foo").ToObject()},
{f: "sorted", args: wrapArgs(1), wantExc: mustCreateException(TypeErrorType, "'int' object is not iterable")},
{f: "sorted", args: wrapArgs(newTestList("foo", "bar"), 2), wantExc: mustCreateException(TypeErrorType, "'sorted' requires 1 arguments")},
{f: "sorted", args: wrapArgs(newTestList(1, 2, 0, 3)), kwargs: wrapKWArgs("reverse", False), want: newTestRange(4).ToObject()},
{f: "sorted", args: wrapArgs(newTestList(1, 2, 0, 3)), kwargs: wrapKWArgs("reverse", 0), want: newTestRange(4).ToObject()},
{f: "sorted", args: wrapArgs(newTestList(1, 2, 0, 3)), kwargs: wrapKWArgs("reverse", None), want: newTestRange(4).ToObject()},
{f: "sorted", args: wrapArgs(newTestList(1, 2, 0, 3)), kwargs: wrapKWArgs("reverse", True), want: newTestList(3, 2, 1, 0).ToObject()},
{f: "sorted", args: wrapArgs(newTestList(1, 2, 0, 3)), kwargs: wrapKWArgs("reverse", 1), want: newTestList(3, 2, 1, 0).ToObject()},
{f: "unichr", args: wrapArgs(0), want: NewUnicode("\x00").ToObject()},
{f: "unichr", args: wrapArgs(65), want: NewStr("A").ToObject()},
{f: "unichr", args: wrapArgs(0x120000), wantExc: mustCreateException(ValueErrorType, "unichr() arg not in range(0x10ffff)")},
Expand Down
20 changes: 13 additions & 7 deletions runtime/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ func (l *List) Append(o *Object) {
l.mutex.Unlock()
}

// Reverse the list in place.
func (l *List) Reverse() {
l.mutex.Lock()
halfLen := len(l.elems) / 2
for i := 0; i < halfLen; i++ {
j := len(l.elems) - i - 1
l.elems[i], l.elems[j] = l.elems[j], l.elems[i]
}
l.mutex.Unlock()
}


// SetItem sets the index'th element of l to value.
func (l *List) SetItem(f *Frame, index int, value *Object) *BaseException {
l.mutex.RLock()
Expand Down Expand Up @@ -354,13 +366,7 @@ func listReverse(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
return nil, raised
}
l := toListUnsafe(args[0])
l.mutex.Lock()
halfLen := len(l.elems) / 2
for i := 0; i < halfLen; i++ {
j := len(l.elems) - i - 1
l.elems[i], l.elems[j] = l.elems[j], l.elems[i]
}
l.mutex.Unlock()
l.Reverse()
return None, nil
}

Expand Down
3 changes: 3 additions & 0 deletions testing/builtin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ def __init__(self, x):
assert sorted(["a", "e", "c", "b"]) == ["a", "b", "c", "e"]
assert sorted((3, 1, 5, 2, 4)) == [1, 2, 3, 4, 5]
assert sorted({"foo": 1, "bar": 2}) == ["bar", "foo"]
assert sorted([1, 2], reverse=True) == [2, 1]
assert sorted([1, 2], reverse=42) == [2, 1]
assert sorted([1, 2], reverse=0) == [1, 2]

# Test zip

Expand Down