-
Notifications
You must be signed in to change notification settings - Fork 646
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sorted
implementation seems reasonable to me. Some tests in testing/builtin_test.py
to exercise sorted
from the Python side would be nice too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is the correct implementation. reversed
is not actually a builtin function. It is a constructor for a builtin type. The builtin reversed type must also support the __reverse__
method: https://docs.python.org/2.7/reference/datamodel.html#object.__reversed__
Thanks @meadori, I didn't realize there is tests for python side. Looks like I shouldn't include reversed in this PR. I didnt know about there is |
Yeah, I would break the |
@meadori, Thank you, removed reversed |
if raised := checkFunctionArgs(f, "sorted", args, ObjectType); raised != nil { | ||
return nil, raised | ||
} | ||
result, raised := listNew(f, ListType, Args{args[0]}, KWArgs{}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ListType.Call(f, Args{args[0]}, nil)
@@ -541,6 +541,18 @@ func builtinRepr(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) { | |||
return s.ToObject(), nil | |||
} | |||
|
|||
func builtinSorted(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) { | |||
if raised := checkFunctionArgs(f, "sorted", args, ObjectType); raised != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorted()
is a little more complicated than this:
sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
A TODO is fine. Then that functionality could be added to list.sort() and then passed when you call list.sort() below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that as well, but didn't explicitly mention it b/c @S-YOU put Add builtin sorted, without cmp, key, and reverse options.
in the change request message.
@@ -228,6 +228,13 @@ func TestBuiltinFuncs(t *testing.T) { | |||
{f: "repr", args: wrapArgs(NewUnicode("abc")), want: NewStr("u'abc'").ToObject()}, | |||
{f: "repr", args: wrapArgs(newTestTuple("foo", "bar")), want: NewStr("('foo', 'bar')").ToObject()}, | |||
{f: "repr", args: wrapArgs("a", "b", "c"), wantExc: mustCreateException(TypeErrorType, "'repr' requires 1 arguments")}, | |||
{f: "sorted", args: wrapArgs(NewList()), want: NewList().ToObject()}, | |||
{f: "sorted", args: wrapArgs(newTestList("foo", "bar")), want: newTestList("bar", "foo").ToObject()}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe some test cases that are other iterable types like dict and tuple.
Updated following
Please take a look, @meadori, @trotterdylan |
@@ -541,6 +541,19 @@ func builtinRepr(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) { | |||
return s.ToObject(), nil | |||
} | |||
|
|||
func builtinSorted(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) { | |||
//TODO: Support (cmp=None, key=None, reverse=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space after //
in comments:
// TODO: ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great! one last nit.
@trotterdylan, added space after comment //, thank you very much. |
Add builtin sorted, without cmp, key, and reverse options.