-
Notifications
You must be signed in to change notification settings - Fork 323
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
sort handles incomparable values #5998
Changes from 34 commits
299c9d4
b8a9c9e
38b6b8a
3f66d8a
e57c4ae
603ab95
95c0878
0df66b1
b5692b8
75d3178
f8deb71
82ac86b
e695e1f
219aa4a
38057d7
8bd3e00
1360be6
4fbdc0d
8dfb873
1528446
80c69d0
e0881f4
19d2d6f
9278734
af9dfa3
7f2af25
ed97b04
c726753
92a10a8
8f78728
e2075d9
6983769
85be93d
a2bfc8b
310f1df
fee458a
7f933dd
ddcdacb
08f0e31
5bc344e
a8091d1
d4b487e
b39ea29
2a41993
9e04935
cc1f527
715fe94
8268e02
254d579
8174ca6
672afb0
6b8f147
d9a12a0
8718c99
1f23d83
ab2f95a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,7 +4,6 @@ import project.Data.Filter_Condition.Filter_Condition | |||||||||
import project.Data.List.List | ||||||||||
import project.Data.Map.Map | ||||||||||
import project.Data.Numbers.Integer | ||||||||||
import project.Data.Ordering.Ordering | ||||||||||
import project.Data.Ordering.Sort_Direction.Sort_Direction | ||||||||||
import project.Data.Pair.Pair | ||||||||||
import project.Data.Range.Range | ||||||||||
|
@@ -19,10 +18,17 @@ import project.Error.Error | |||||||||
import project.Errors.Illegal_Argument.Illegal_Argument | ||||||||||
import project.Function.Function | ||||||||||
import project.Math | ||||||||||
import project.Meta | ||||||||||
import project.Nothing.Nothing | ||||||||||
import project.Panic.Panic | ||||||||||
import project.Random | ||||||||||
import project.Warning.Warning | ||||||||||
|
||||||||||
import project.IO | ||||||||||
|
||||||||||
# We have to import also conversion methods, therefore, we import all from the Ordering | ||||||||||
# module | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think the convention is to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw. I think this raises a good point that we may want to have a way to import conversion methods without necessarily importing I guess one way to do so should be to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But maybe a more explicit way for this would be good too. Just a random thought, low priority. Definitely independent of this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't know. I guess it depends on whether the current behavior is the expected one, i.e., if I import a type with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly, I don't know. My intuition would be that if I import a type, probably it's extension methods are imported too... But probably only its and not others. I'd appreciate a way to import only extension methods from a file, but that's just my subjective view. Also definitely not something of high priority, as we are doing fine with current state of the import system, I think. |
||||||||||
from project.Data.Ordering import all | ||||||||||
from project.Data.Boolean import Boolean, True, False | ||||||||||
from project.Data.Index_Sub_Range import Index_Sub_Range, take_helper, drop_helper | ||||||||||
|
||||||||||
|
@@ -841,13 +847,16 @@ type Vector a | |||||||||
Arguments: | ||||||||||
- order: The order in which the vector elements are sorted. | ||||||||||
- on: A projection from the element type to the value of that element | ||||||||||
being sorted on. | ||||||||||
being sorted on. If set to `Nothing` (the default argument), | ||||||||||
identity function will be used. | ||||||||||
- by: A function that compares the result of applying `on` to two | ||||||||||
elements, returning an Ordering to compare them. | ||||||||||
elements, returning an an `Ordering` if the two elements are comparable | ||||||||||
or `Nothing` if they are not. If set to `Nothing` (the default argument), | ||||||||||
`Ordering.compare _ _` method will be used. | ||||||||||
Must be a static method. | ||||||||||
|
||||||||||
By default, elements are sorted in ascending order. | ||||||||||
|
||||||||||
By default, elements are sorted in ascending order, using the comparator | ||||||||||
acquired from each element. A custom compare function may be passed to | ||||||||||
the sort method. | ||||||||||
|
||||||||||
This is a stable sort, meaning that items that compare the same will not | ||||||||||
have their order changed by the sorting process. | ||||||||||
|
@@ -865,6 +874,18 @@ type Vector a | |||||||||
is partially sorted. When the vector is randomly ordered, the | ||||||||||
performance is equivalent to a standard mergesort. | ||||||||||
|
||||||||||
? Multiple comparators | ||||||||||
Elements with different comparators are incomparable by definition. | ||||||||||
This case is handled by first grouping the `self` vector into groups | ||||||||||
with the same comparator, recursively sorting these groups, and then | ||||||||||
merging them back together. The order of the sorted groups in the | ||||||||||
resulting vector is based on the order of fully qualified names of | ||||||||||
the comparators in the `self` vector, with the exception of the group | ||||||||||
for the default comparator, which is always the first group. | ||||||||||
|
||||||||||
Additionally, a warning will be attached, explaining that incomparable | ||||||||||
values were encountered. | ||||||||||
|
||||||||||
It takes equal advantage of ascending and descending runs in the array, | ||||||||||
making it much simpler to merge two or more sorted arrays: simply | ||||||||||
concatenate them and sort. | ||||||||||
|
@@ -878,16 +899,21 @@ type Vector a | |||||||||
Sorting a vector of `Pair`s on the first element, descending. | ||||||||||
|
||||||||||
[Pair 1 2, Pair -1 8].sort Sort_Direction.Descending (_.first) | ||||||||||
sort : Sort_Direction -> (Any -> Any) -> (Any -> Any -> Ordering) -> Vector Any ! Incomparable_Values | ||||||||||
sort self (order = Sort_Direction.Ascending) (on = x -> x) (by = (Ordering.compare _ _)) = | ||||||||||
comp_ascending l r = by (on l) (on r) | ||||||||||
comp_descending l r = by (on r) (on l) | ||||||||||
compare = if order == Sort_Direction.Ascending then comp_ascending else | ||||||||||
comp_descending | ||||||||||
|
||||||||||
new_vec_arr = self.to_array.sort compare | ||||||||||
if new_vec_arr.is_error then Error.throw new_vec_arr else | ||||||||||
Vector.from_polyglot_array new_vec_arr | ||||||||||
|
||||||||||
> Example | ||||||||||
Sorting a vector with elements with different comparators. Values 1 | ||||||||||
and My_Type have different comparators. 1 will be sorted before My_Type | ||||||||||
because it has the default comparator, and warning will be attached to | ||||||||||
the resulting vector. | ||||||||||
|
||||||||||
[My_Type.Value 'hello', 1].sort == [1, My_Type.Value 'hello'] | ||||||||||
sort : Sort_Direction -> (Any -> Any)|Nothing -> (Any -> Any -> (Ordering|Nothing))|Nothing -> Vector Any ! Incomparable_Values | ||||||||||
sort self (order = Sort_Direction.Ascending) on=Nothing by=Nothing = | ||||||||||
comps = case on == Nothing of | ||||||||||
True -> self.map it-> Comparable.from it | ||||||||||
False -> self.map it-> Comparable.from (on it) | ||||||||||
compare_funcs = comps.map (it-> it.compare) | ||||||||||
self.sort_builtin order.to_sign comps compare_funcs by on | ||||||||||
|
||||||||||
## UNSTABLE | ||||||||||
Keeps only unique elements within the Vector, removing any duplicates. | ||||||||||
|
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.
(.start)
is parsed toUnresolvedSymbol<start>
, and I found no easy way how to call that. Whereas(_.start)
is parsed intoFunction
. I created issue #6726 to track that.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.
#6276 *