Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added List.sort(comp) to List module #802

Merged
merged 14 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 25 additions & 0 deletions doc/site/modules/core/list.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,31 @@ System.print(["a", "b", "c"].removeAt(1)) //> b

It is a runtime error if the index is not an integer or is out of bounds.

### **sort**(), **sort**(comp)

Sorts the elements of a list in-place; altering the list.

<pre class="snippet">
var list = [4, 1, 3, 2].sort()
System.print(list) //> [1, 2, 3, 4]
</pre>

A comparison function `comp` can be provided to customise the element sorting. The comparison function must return a boolean value specifying the order in which elements should appear in the list. The default behaviour uses a `<` comparison.

<pre class="snippet">
var list = [9, 6, 8, 7]
list.sort {|a, b| a < b}
System.print(list) //> [6, 7, 8, 9]
</pre>

It is a runtime error if `comp` is not a function.

<pre class="snippet">
System.print(["a", "b", "c"].removeAt(1)) //> b
</pre>

It is a runtime error if the index is not an integer or is out of bounds.
mathewmariani marked this conversation as resolved.
Show resolved Hide resolved

### **[**index**]** operator

Gets the element at `index`. If `index` is negative, it counts backwards from
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions src/vm/wren_core.wren
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,42 @@ class List is Sequence {
return other
}

sort() {
sort {|a, b| a < b }
}

sort(comp) {
if (!(comp is Fn)) {
Fiber.abort("Comp must be a function.")
mathewmariani marked this conversation as resolved.
Show resolved Hide resolved
}
quicksort_(0, count - 1, comp)
}

quicksort_(a, b, comp) {
if (a < b) {
mathewmariani marked this conversation as resolved.
Show resolved Hide resolved
var p = partition_(a, b, comp)
quicksort_(a, p - 1, comp)
quicksort_(p + 1, b, comp)
}
}

partition_(a, b, comp) {
var p = this[b]
var i = a - 1
for (j in a..(b-1)) {
if (comp.call(this[j], p)) {
i = i + 1
var t = this[i]
this[i] = this[j]
this[j] = t
}
}
var t = this[i+1]
this[i+1] = this[b]
this[b] = t
return i+1
}

toString { "[%(join(", "))]" }

+(other) {
Expand Down
36 changes: 36 additions & 0 deletions src/vm/wren_core.wren.inc
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,42 @@ static const char* coreModuleSource =
" return other\n"
" }\n"
"\n"
" sort() {\n"
" sort {|a, b| a < b }\n"
" }\n"
"\n"
" sort(comp) {\n"
" if (!(comp is Fn)) {\n"
" Fiber.abort(\"Comp must be a function.\")\n"
" }\n"
" quicksort_(0, count - 1, comp)\n"
" }\n"
"\n"
" quicksort_(a, b, comp) {\n"
" if (a < b) {\n"
" var p = partition_(a, b, comp)\n"
" quicksort_(a, p - 1, comp)\n"
" quicksort_(p + 1, b, comp)\n"
" }\n"
" }\n"
"\n"
" partition_(a, b, comp) {\n"
" var p = this[b]\n"
" var i = a - 1\n"
" for (j in a..(b-1)) {\n"
" if (comp.call(this[j], p)) { \n"
" i = i + 1\n"
" var t = this[i]\n"
" this[i] = this[j]\n"
" this[j] = t\n"
" }\n"
" }\n"
" var t = this[i+1]\n"
" this[i+1] = this[b]\n"
" this[b] = t\n"
" return i+1\n"
" }\n"
"\n"
" toString { \"[%(join(\", \"))]\" }\n"
"\n"
" +(other) {\n"
Expand Down
10 changes: 10 additions & 0 deletions test/core/list/sort.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var list = [4, 1, 3, 2].sort()
System.print([4, 1, 3, 2].sort()) // expect: [1, 2, 3, 4]
mathewmariani marked this conversation as resolved.
Show resolved Hide resolved

var l = [10, 7, 8, 9, 1, 5]
l.sort{|a, b| a < b }
System.print(l) // expect: [1, 5, 7, 8, 9, 10]
l.sort{|a, b| a > b }
System.print(l) // expect: [10, 9, 8, 7, 5, 1]

[10, 7, 8, 9, 1, 5].sort(3) // expect runtime error: Comp must be a function.