Skip to content

Commit 854cdd4

Browse files
AStepanov25crusso
authored andcommitted
fix List.sort/sortInPlace (acc. to #405) and declare implicits
1 parent d90dcc7 commit 854cdd4

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Add `PriorityQueue` (#392).
66
* Add support for Weak references (#388).
77
* Clarify difference between `List` and `pure/List` in doc comments (#386).
8+
* **Breaking:** Rename `sort` to `sortInPlace`, add `sort` (#405).
89

910
## 1.0.0
1011

src/List.mo

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,15 +588,15 @@ module {
588588
/// List.add(list, 3);
589589
/// List.add(list, 1);
590590
/// List.add(list, 2);
591-
/// List.sort(list, Nat.compare);
591+
/// List.sortInPlace(list, Nat.compare);
592592
/// assert List.toArray(list) == [1, 2, 3];
593593
/// ```
594594
///
595595
/// Runtime: O(size * log(size))
596596
///
597597
/// Space: O(size)
598598
/// *Runtime and space assumes that `compare` runs in O(1) time and space.
599-
public func sort<T>(list : List<T>, compare : (implicit : (T, T) -> Order.Order)) {
599+
public func sortInPlace<T>(list : List<T>, compare : (implicit : (T, T) -> Order.Order)) {
600600
if (size(list) < 2) return;
601601
let arr = toVarArray(list);
602602
VarArray.sortInPlace(arr, compare);
@@ -605,6 +605,31 @@ module {
605605
}
606606
};
607607

608+
/// Sorts the elements in the list according to `compare`.
609+
/// Sort is deterministic, stable, and in-place.
610+
///
611+
/// Example:
612+
/// ```motoko include=import
613+
/// import Nat "mo:core/Nat";
614+
///
615+
/// let list = List.empty<Nat>();
616+
/// List.add(list, 3);
617+
/// List.add(list, 1);
618+
/// List.add(list, 2);
619+
/// let sorted = List.sort(list, Nat.compare);
620+
/// assert List.toArray(sorted) == [1, 2, 3];
621+
/// ```
622+
///
623+
/// Runtime: O(size * log(size))
624+
///
625+
/// Space: O(size)
626+
/// *Runtime and space assumes that `compare` runs in O(1) time and space.
627+
public func sort<T>(list : List<T>, compare : (implicit : (T, T) -> Types.Order)) : List<T> {
628+
let array = toVarArray(list);
629+
VarArray.sortInPlace(array, compare);
630+
fromVarArray(array)
631+
};
632+
608633
/// Finds the first index of `element` in `list` using equality of elements defined
609634
/// by `equal`. Returns `null` if `element` is not found.
610635
///

test/List.test.mo

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ run(
811811
[
812812
test(
813813
"sort",
814-
do { list.sort(Nat.compare); list.toArray() },
814+
do { list.sortInPlace(Nat.compare); list.toArray() },
815815
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |> M.equals(T.array(T.natTestable, _))
816816
)
817817
]
@@ -1134,9 +1134,15 @@ func testReverse(n : Nat) : Bool {
11341134
};
11351135

11361136
func testSort(n : Nat) : Bool {
1137-
let vec = List.fromArray<Int>(Array.tabulate<Int>(n, func(i) = (i * 123) % 100 - 50));
1138-
vec.sort(Int.compare);
1139-
vec.equal(List.fromArray<Int>(Array.sort(Array.tabulate<Int>(n, func(i) = (i * 123) % 100 - 50), Int.compare)), Int.equal)
1137+
let array = Array.tabulate<Int>(n, func(i) = (i * 123) % 100 - 50);
1138+
let vec = List.fromArray<Int>(array);
1139+
1140+
let sorted = vec.sort(Int.compare);
1141+
vec.sortInPlace(Int.compare);
1142+
1143+
let expected = List.fromArray<Int>(Array.sort(array, Int.compare));
1144+
1145+
vec.equal(expected, Int.equal) and sorted.equal(expected, Int.equal)
11401146
};
11411147

11421148
func testToArray(n : Nat) : Bool {

validation/api/api.lock.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,8 @@
572572
"public type Self<T>",
573573
"public func singleton<T>(element : T) : List<T>",
574574
"public func size<T>(list : List<T>) : Nat",
575-
"public func sort<T>(list : List<T>, compare : (implicit : (T, T) -> Order.Order))",
575+
"public func sort<T>(list : List<T>, compare : (implicit : (T, T) -> Types.Order)) : List<T>",
576+
"public func sortInPlace<T>(list : List<T>, compare : (implicit : (T, T) -> Order.Order))",
576577
"public func toArray<T>(list : List<T>) : [T]",
577578
"public func toPure<T>(list : List<T>) : PureList.List<T>",
578579
"public func toText<T>(list : List<T>, f : (implicit : (toText : T -> Text))) : Text",

0 commit comments

Comments
 (0)