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

deleteWith function & friends #242

Open
flip111 opened this issue Sep 1, 2023 · 2 comments
Open

deleteWith function & friends #242

flip111 opened this issue Sep 1, 2023 · 2 comments

Comments

@flip111
Copy link

flip111 commented Sep 1, 2023

A lot of functions like delete require an index. A lot of times that index was not stored (which would also get updated as the array would change) but has to be refound with findIndex. When you use findIndex and immediately after use delete you know you are guaranteed an index that is not out of bounds. Could "with" functions be added that do this?

deleteWith :: forall a. (a -> Boolean) -> Array a -> Maybe (Array a)
deleteWith pred array = map (\idx -> unsafeDeleteAt idx array) (findIndex pred array)

It would make sense to have the unsafe implementations for them available, at least internally in the array library #167

One could argue that if you do this you are better off using a Map. But i think people will want to use an array plenty of times anyway.

@flip111 flip111 changed the title delete deleteWith function & friends Sep 1, 2023
@garyb
Copy link
Member

garyb commented Sep 1, 2023

At first I was going to suggest that this seems like a worse filter, but the difference is it's intended to only delete the first instance of the value matching the predicate? And return Nothing if nothing matched?

@flip111
Copy link
Author

flip111 commented Sep 2, 2023

yes

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

No branches or pull requests

2 participants