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

feat: implement std.remove and std.removeAt #1071

Merged
merged 2 commits into from
May 25, 2023

Conversation

rohitjangid
Copy link
Member

Implement std.remove and std.removeAt

go-jsonnet PR: google/go-jsonnet#689

availableSince: 'upcoming',
description: html.paragraphs([
|||
Remove given <code>elem</code> from <code>arr</code>.
Copy link
Contributor

Choose a reason for hiding this comment

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

Specifically the first one found

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh it actually removes all of them! Say that then :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually it seems the go version only removes the 1st one and that's the semantics i'd expect from the name of the function so I guess that's what you wanted.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it is for first occurrence. Made appropriate changes.


removeAt(arr, at):: [
arr[i],
for i in std.range(0, std.length(arr) - 1)
Copy link
Contributor

@sparkprime sparkprime May 3, 2023

Choose a reason for hiding this comment

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

Edit: Never mind, this is fine

@@ -1710,4 +1710,16 @@ limitations under the License.
round(x):: std.floor(x + 0.5),

isEmpty(str):: std.length(str) == 0,

remove(arr, elem):: [
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can express this as something like:

local indexes = std.find(arr, at);
if std.length(indexes) == 0 then
  return arr
else
  return std.removeAt(arr,indexes[0])

So it does not remove all of them

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

@sparkprime sparkprime merged commit 9f9cf3a into google:master May 25, 2023
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.

2 participants