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

Consolidate Vector and Array methods #6218

Merged
merged 18 commits into from
Apr 11, 2023
Merged

Conversation

JaroslavTulach
Copy link
Member

@JaroslavTulach JaroslavTulach commented Apr 6, 2023

Pull Request Description

Fixes #5011 by making sure the same methods that are on Vector are also available on Array.

Important Notes

Checklist

Please ensure that the following checklist has been satisfied before submitting the PR:

  • The documentation has been updated, if necessary.
  • All code follows the
    Java,
  • All code has been tested:
    • Unit tests have been written where possible.

@JaroslavTulach JaroslavTulach requested a review from 4e6 as a code owner April 6, 2023 16:57
Copy link
Member

@radeusgd radeusgd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code and tests look good.

Unless there is a reason, I think we need to add doc comments to the methods on the Array. It's not a PRIVATE type. If it is to be private - OK, but then we should probably mark all the methods at PRIVATE. In general I think that in our libraries we should not have methods that are neither marked PRIVATE nor have at least one sentence of documentation.

@enso-bot enso-bot bot mentioned this pull request Apr 6, 2023
7 tasks
Copy link
Member Author

@JaroslavTulach JaroslavTulach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready for your reviews @hubertp, @4e6, @radeusgd and @jdunkerley.

@@ -225,7 +225,7 @@ type Vector a

## Combines all the elements of the vector, by iteratively applying the
passed function with the next element of the vector. After each step the
value is stored resulting in a new Vector of the same size as self.
value is stored resulting in a new vector of the same size as self.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever there is vector in lowercase, it is replaced by array when comparing the documentation in the Array type.

@@ -499,12 +499,12 @@ type Vector a
map self function =
Vector.new self.length i-> function (self.at i)

## Applies a function to each element of the vector, returning the vector
## Applies a function to each element of the vector, returning the `Vector`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes we don't want to replace the Vector word in the Array documentation. I am then writing the Vector with capital first letter and quoting it.

Copy link
Member

@radeusgd radeusgd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

I'd appreciate a short note on ArraySlice.createOrNull explaining what is the meaning of it returning null (we have discsused this here, but no on reading the code will know to look around this PR discussion for details, and it would make reading it much easier).

Comment on lines +128 to +135
var head = arrayFns.filter((av) -> av instanceof IR$Function$Binding ab && ab.name().name().equals(b.name().name())).head();
var ad = documentation.get(head);
assertNotNull("No documentation for Array." + name, ad);
var exp = d.doc()
.replace("]", "].to_array")
.replace("a vector", "an array")
.replace("vector", "array");
assertEquals("Bad documentation for Array." + name, exp, ad.doc());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow 😀 neat

Copy link
Member

@jdunkerley jdunkerley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good - I do wonder if the return type of the methods should be Array instead of Vector.

@JaroslavTulach
Copy link
Member Author

Looks good - I do wonder if the return type of the methods should be Array instead of Vector.

Usually not. Usually the return type should be Vector. Only in situations where we return self, we may return Array. In all other cases the method delegates to Vector.method and that one is going to produce a Vector.

Copy link
Contributor

@4e6 4e6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Engine part looks good 👍

@JaroslavTulach JaroslavTulach added the CI: Ready to merge This PR is eligible for automatic merge label Apr 11, 2023
Copy link
Contributor

@hubertp hubertp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about Array_Proxy and ArrayOverBuffer?
Plus minor nits in type signatures.

btw I'm guessing it is intentional that Array methods often return Vector? It feels a bit weird that one operates on the former and always gets the latter.

Concatenate two single-element arrays.

[1].to_array + [2].to_array
+ : Vector Any -> Vector Any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type should be an Array?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The in type can be either Vector or Array. The output type is Vector.

of both elements.

[1, 2, 3].to_array.zip [4, 5, 6].to_array == [[1, 4].to_array, [2, 5].to_array, [3, 6].to_array].to_array
zip : Vector Any -> (Any -> Any -> Any) -> Vector Any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
zip : Vector Any -> (Any -> Any -> Any) -> Vector Any
zip : Array Any -> (Any -> Any -> Any) -> Vector Any

Copy link
Member Author

@JaroslavTulach JaroslavTulach Apr 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again. The proper input type is either Vector or Array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so if I was a real PITA I would say that this should be (Array Any | Vector Any) :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moreover that applies to the whole codebase. Everywhere Vector is used, Array can be used as well!

Rather than modifying all the type signatures, let's hope further consolidation renders these two types obsolete and allows them to be merged into one. Because Array is basically Vector in Input - e.g. Vector that can change due to changes made elsewhere and may need to be read (Inputed) again.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a good idea for the future.

Alternatively, I'd keep the Vector/Array distinction but make the type signatures rely on some interface/typeclass - like IndexedSequence - and then both Array and Vector would 'implement' this interface/typeclass, so they could be used where it is expected.

@JaroslavTulach JaroslavTulach removed the CI: Ready to merge This PR is eligible for automatic merge label Apr 11, 2023
@JaroslavTulach
Copy link
Member Author

What about Array_Proxy and ArrayOverBuffer? Plus minor nits in type signatures.

I had to fix one test because of ArrayOverBuffer. I assume the rest works just fine. I had no issues with Array_Proxy.

btw I'm guessing it is intentional that Array methods often return Vector?
It feels a bit weird that one operates on the former and always gets the latter.

The (philosophical) difference between Array and Vector is: Array can potentially change due to external misdoings. Vector shall (almost) never change. The new Array methods delegate to Vector counterparts and those are known to create something that doesn't change - e.g. Vector. Hence the return type is (almost) always Vector and not Array.

@JaroslavTulach JaroslavTulach added the CI: Ready to merge This PR is eligible for automatic merge label Apr 11, 2023
@mergify mergify bot merged commit 5b3cf6f into develop Apr 11, 2023
@mergify mergify bot deleted the wip/jtulach/Consolidate_5011 branch April 11, 2023 16:20
@JaroslavTulach JaroslavTulach mentioned this pull request Dec 6, 2023
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI: Ready to merge This PR is eligible for automatic merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consolidate Vector & Array
5 participants