-
Notifications
You must be signed in to change notification settings - Fork 465
[Belt] Add List.sort #2666
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
[Belt] Add List.sort #2666
Conversation
Apparently Array.prototype.sort() sorts everything like a string, and is what |
Oh no, definitely don't use Js.Array.sortInPlace. That behavior is famously confusing. Maybe |
I went ahead and adjusted it to use |
The asymmetry between SortArray and List.sort is a little weird. I'm wondering how we should arrange these |
What was the reasoning behind putting Sort functions on arrays in their own separate module? |
jscomp/others/belt_List.ml
Outdated
@@ -692,6 +692,13 @@ let setAssocU xs x k eq = | |||
|
|||
let setAssoc xs x k eq = setAssocU xs x k (fun [@bs] a b -> eq a b) | |||
|
|||
let sort xs cmp = | |||
let arr = ref (toArray xs) in |
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.
why a ref needed here
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.
sortU/sort
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.
Ok, I'll add a sortU version. I think I was confused about whether I could use let and it still be mutated by the in-place sort. It works fine without the ref.
jscomp/others/belt_List.ml
Outdated
let sort xs cmp = | ||
let arr = ref (toArray xs) in | ||
begin | ||
Belt_SortArray.stableSortInPlaceBy !arr cmp; |
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.
A.stableSortInPlaceBy -- it is already aliased in the beginning
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.
Belt_Array is aliased as A in this module. It doesn't have the sort functions from Belt_SortArray. Should Belt_SortArray be opened in Belt_Array?
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 fine then, sorry my mistake
Signed-off-by: Zach Ploskey <zach@ploskey.com>
Signed-off-by: Zach Ploskey <zach@ploskey.com>
0079ce3
to
e94d8c0
Compare
I addressed the concerns and rebased on master so that all the tests run. I'm not sure why we're seeing a build failure on Travis. It builds fine for me locally. Somehow it can't find |
Signed-off-by: Zach Ploskey <zach@ploskey.com>
That seems to have worked. Let me know if there are any other concerns. |
Signed-off-by: Zach Ploskey <zach@ploskey.com>
let arr = toArray xs in | ||
Belt_SortArray.stableSortInPlaceByU arr cmp; | ||
fromArray arr | ||
|
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.
In general, in belt, we should define sortU
first and wrap sort
like this
let sort xs cmp = sortU xs (fun[@bs] x y -> cmp x y)
The thing is in your sort
, you are calling stableSortInPlaceBy
instead of stableSortInPlaceByU
which is slower
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.
Fixed. Thanks!
Signed-off-by: Zach Ploskey <zach@ploskey.com>
Implements Belt.List.sort as specified in the issue.
Adds a couple of simple tests and an example.
Fixes #2586.