Skip to content

Add Array.removeInPlace #7321

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

Merged
merged 8 commits into from
Mar 21, 2025
Merged

Conversation

DZakh
Copy link
Member

@DZakh DZakh commented Mar 7, 2025

Splice returns an array of removed elements. I don't remember where I saw an issue, but I think we decided to change the binding to return an array.

@@ -103,11 +103,14 @@ external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int) => array<
@send external shift: array<'a> => option<'a> = "shift"

@variadic @send
external splice: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => unit = "splice"
external splice: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => array<'a> = "splice"
Copy link
Member

Choose a reason for hiding this comment

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

I think we willingly ignore the results of functions that mutate their arguments to make it clear that they have side effects, I'd likely rename it to something more explicit otherwise

Suggested change
external splice: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => array<'a> = "splice"
external spliceInPlace: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => array<'a> = "splice"

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree. What others think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, non breaking and the current bindings do have a purpose. So I agree with this comment.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean to keep the current splice as it is and add a new bindings with the return type?

Copy link
Member

Choose a reason for hiding this comment

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

yeah I'd add a new external with the return type.

Copy link

Choose a reason for hiding this comment

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

Is it OK to have the version in rescript breaks some backward compatibility? If so I think making Array.splice behave as JS does would provide a smoother experience. Unlike Array.sort the current binding of Array.splice is more restricting and less powerful. For Array.sort one can often fall back on toSorted but splice is different.

Copy link
Member Author

Choose a reason for hiding this comment

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

I removed the change from the PR. It's ready for review now.

@DZakh DZakh force-pushed the remove-in-place branch from 78badc1 to 1d2fa81 Compare March 10, 2025 16:48
@DZakh DZakh changed the title Add Array.removeInPlace & Update splice binding Add Array.removeInPlace Mar 10, 2025
@DZakh DZakh requested review from zth and tsnobip March 10, 2025 16:52
Comment on lines 304 to 307

@send
external removeInPlace: (array<'a>, int, @as(1) _) => unit = "splice"

Copy link
Member

Choose a reason for hiding this comment

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

nitpicking, but wouldn't it be better to move the tests here as docstring tests? At least they would help documenting the function.

Whatever we decide to do, I'd add some documentation here anyway, at least to document that the second argument is an index. Maybe it's a sign we should use a labelled argument here (~start, ~index?) like the other functions with indices here, what do you guys think? @zth ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a documentation comment 👍

Regarding labeled arguments I think it's consistent for the array API to not use it when we have a single argument which is an index. But it would definitely make sense if we had more arguments like:

@send
external removeMultipleInPlace: (array<'a>, ~start: int, ~number: int) => unit = "splice"

Copy link
Member

Choose a reason for hiding this comment

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

many functions with just one int parameter in the Stdlib.Array module are labeled, it might be worth making all of this more consistant, but this could definitely be in another PR.

@DZakh
Copy link
Member Author

DZakh commented Mar 12, 2025

I fixed the comments, but tests are failing for some weird error. Could somebody help me with them?

@tsnobip
Copy link
Member

tsnobip commented Mar 13, 2025

I fixed the comments, but tests are failing for some weird error. Could somebody help me with them?

are you sure you ran make lib, make test and committed the changes @DZakh?

@DZakh DZakh requested a review from tsnobip March 14, 2025 15:43
Copy link
Member

@tsnobip tsnobip left a comment

Choose a reason for hiding this comment

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

we're almost there :)

Comment on lines 135 to 144
let array = []
array->Array.removeInPlace(0)
Test.run(__POS_OF__("removeInPlace - empty"), array, eq, [])
}

{
let array = ["Hello", "Hi", "Good bye"]
array->Array.removeInPlace(1)
Test.run(__POS_OF__("removeInPlace - from middle"), array, eq, ["Hello", "Good bye"])
}
Copy link
Member

Choose a reason for hiding this comment

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

should we keep these tests here given they're already in the docstring tests?

Copy link
Member Author

@DZakh DZakh Mar 14, 2025

Choose a reason for hiding this comment

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

Wow, are they automated?

Copy link
Member

Choose a reason for hiding this comment

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

@DZakh yes, that's the whole point of those docstring tests :)

@DZakh
Copy link
Member Author

DZakh commented Mar 16, 2025

@tsnobip Done 👍

Copy link
Member

@tsnobip tsnobip 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 to me now!

@tsnobip tsnobip merged commit 2470f75 into rescript-lang:master Mar 21, 2025
20 checks passed
fhammerschmidt pushed a commit that referenced this pull request Apr 4, 2025
* Add Array.removeInPlace

* Update changelog

* Add documentation

* Commit new tests output

* Fix analysis test

* Update tests/analysis_tests/tests/test.sh

Co-authored-by: Paul Tsnobiladzé <[email protected]>

* Remove duplicated tests

---------

Co-authored-by: Paul Tsnobiladzé <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants