Skip to content
Merged
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
7 changes: 5 additions & 2 deletions docs/lexicon.html
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,12 @@ <h2 class="title-2" id="python-builtins">
<span>
<code class="code"
><span class="fn-name">sorted</span><span class="fn-p">(</span
><span class="fn-arg">seq</span><span class="fn-p">)</span></code
><span class="fn-arg">seq</span>[,<span class="fn-arg">key</span
>]<span class="fn-p">)</span></code
>
- returns a copy of the given list with the contents sorted.
- returns a copy of <code class="code">seq</code> with the contents
sorted. <code class="code">key</code> is a function that is applied
to each item before comparison.
</span>
</li>
<li>
Expand Down
2 changes: 1 addition & 1 deletion rules/builtins.build_defs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def glob(include:list|str, exclude:list|str&excludes=[], hidden:bool=CONFIG.BAZE
def package():
pass

def sorted(seq:list) -> list:
def sorted(seq:list, key:function=None) -> list:
pass

def reversed(seq:list) -> list:
Expand Down
26 changes: 23 additions & 3 deletions src/parse/asp/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,10 +798,30 @@ func dictCopy(s *scope, args []pyObject) pyObject {
}

func sorted(s *scope, args []pyObject) pyObject {
l, ok := args[0].(pyList)
s.Assert(ok, "unsortable type %s", args[0].Type())
l, isList := args[0].(pyList)
key, isFunc := args[1].(*pyFunc)
s.Assert(isList, "Argument seq must be a list, not %s", args[0].Type())
l = l[:]
sort.Slice(l, func(i, j int) bool { return s.operator(LessThan, l[i], l[j]).IsTruthy() })
if key == nil {
sort.Slice(l, func(i, j int) bool {
return s.operator(LessThan, l[i], l[j]).IsTruthy()
})
} else {
s.Assert(isFunc, "Argument key must be callable, not %s", args[1].Type())
sort.Slice(l, func(i, j int) bool {
iKey := key.Call(s, &Call{
Arguments: []CallArgument{{
Value: Expression{optimised: &optimisedExpression{Constant: l[i]}},
}},
})
jKey := key.Call(s, &Call{
Arguments: []CallArgument{{
Value: Expression{optimised: &optimisedExpression{Constant: l[j]}},
}},
})
return s.operator(LessThan, iKey, jKey).IsTruthy()
})
}
return l
}

Expand Down
3 changes: 2 additions & 1 deletion src/parse/asp/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ func TestInterpreterSlicing(t *testing.T) {
func TestInterpreterSorting(t *testing.T) {
s, err := parseFile("src/parse/asp/test_data/interpreter/sorted.build")
require.NoError(t, err)
assert.Equal(t, pyList{pyInt(1), pyInt(2), pyInt(3)}, s.Lookup("y"))
// N.B. sorted() sorts in-place, unlike Python's one. We may change that later.
assert.Equal(t, pyList{pyInt(1), pyInt(2), pyInt(3)}, s.Lookup("r1"))
assert.Equal(t, pyList{pyString("ONE"), pyString("THREE"), pyString("two")}, s.Lookup("r2"))
}

func TestReversed(t *testing.T) {
Expand Down
8 changes: 6 additions & 2 deletions src/parse/asp/test_data/interpreter/sorted.build
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
x = [3, 2, 1]
y = sorted(x)
l1 = [3, 2, 1]
r1 = sorted(l1)

# key parameter test
l2 = ["ONE", "two", "THREE"]
r2 = sorted(l2, key=lambda s: s.lower())
Loading