-
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
Make ArrayOverBuffer behave like an Array/Array.sort no longer mutates the Array #4022
Conversation
engine/runtime/src/main/java/org/enso/interpreter/runtime/data/ArrayOverBuffer.java
Outdated
Show resolved
Hide resolved
engine/runtime/src/main/java/org/enso/interpreter/runtime/data/ArrayOverBuffer.java
Show resolved
Hide resolved
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.
I don't like Array.sort
as it breaks referential transparency. But we have got it - e.g. good job on making the sort
work on various types of arrays.
engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/mutable/SortNode.java
Outdated
Show resolved
Hide resolved
engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/mutable/SortNode.java
Outdated
Show resolved
Hide resolved
engine/runtime/src/main/java/org/enso/interpreter/runtime/data/DisplayArrayUtils.java
Show resolved
Hide resolved
engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/mutable/SortNode.java
Outdated
Show resolved
Hide resolved
7a547df
to
0de2ac7
Compare
engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/mutable/SortNode.java
Outdated
Show resolved
Hide resolved
arr = vec.map .x . to_array | ||
sorted = arr.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.
This is not a Python array anymore, right? After a Vector.map
operation, this will be an Enso allocated Java array, no?
Still, a good test but this part is not really testing what it says - only the part below is actually invoking our sort on a Python array instance.
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.
Indeed, yes.
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.
Looks good, just the comment should be updated before the merge as it can be misleading (we do not do in-place sort anymore, comment is suggesting that)
Most of the problems with accessing `ArrayOverBuffer` have been resolved by using `CoerceArrayNode` (#3817). In `Array.sort` we still however specialized on Array which wasn't compatible with `ArrayOverBuffer`. Added a specialization to `Array.sort` to deal with that case. Because one cannot use a custom comparator with primitive (Java) arrays there is a conversion needed. It's a necessary penalty if we want to keep `ArrayOverBuffer` around. Also fixed an example in `Array.enso` by providing a default argument.
Sorting in place would be impractical to support for Arrays coming from JS or Python. That is why `Array.sort` now returns a copy of the original Array(-like) structure, sorted. This eliminates the need for createing a copy of Array for `Vector.sort`. Applied DRY to displaying Array-like structures.
Skip ArrayOverBuffer specialization as it is now handled by a generic `hasArrayElements` one.
0de2ac7
to
99134ff
Compare
…ion/builtin/mutable/SortNode.java Co-authored-by: Radosław Waśko <[email protected]>
Pull Request Description
Most of the problems with accessing
ArrayOverBuffer
have been resolved by usingCoerceArrayNode
(#3817). InArray.sort
we still however specialized on Array which wasn't compatible withArrayOverBuffer
. Similarly sorting JS or Python arrays wouldn't work.Added a specialization to
Array.sort
to deal with that case. A generic specialization (withhasArrayElements
) not only handlesArrayOverBuffer
but also polyglot arrays coming from JS or Python. We could have an additional specialization forArrayOverBuffer
only (removed in the last commit) that returnsArrayOverBuffer
rather thanArray
although that adds additional complexity which so far is unnecessary.Also fixed an example in
Array.enso
by providing a default argument.Checklist
Please include the following checklist in your PR:
Scala,
Java,
and
Rust
style guides.