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

Any interest in collect/collectFirst? #269

Open
adamhamlin opened this issue Mar 2, 2023 · 3 comments · May be fixed by #327
Open

Any interest in collect/collectFirst? #269

adamhamlin opened this issue Mar 2, 2023 · 3 comments · May be fixed by #327

Comments

@adamhamlin
Copy link

Two of my favorite utility functions from the Scala programming language are collect and collectFirst. I think these would make a great addition to the library.

  • collect -- a map+filter combo where, if the result of the mapping function is undefined, that element is filtered out:
radash.collect([{a: 1}, {b: 2}, {a: 3}], (el) => el.a) // result: [1, 3]
  • collectFirst -- same as above, but the first defined result is returned immediately:
radash.collectFirst([{a: 1}, {b: 2}, {a: 3}], (el) => el.a) // result: 1

They are essentially more generalized versions of filter and find, where you get the opportunity to transform the value(s) at the same time. collect ends up being a very nice alternative when you don't want to iterate twice but using reduce just feels like too much.

@rayepps would you be interested in a PR for these additions?

@sodiray
Copy link
Owner

sodiray commented Jun 26, 2023

Sorry for the long delay @adamhamlin 🙃 These are interesting functions, we do haveselect which is a little similar to collect but I do see the difference.

If you're still interested in making a PR for these I'd be happy to take a look and bring them into radash.

@adamhamlin
Copy link
Author

@rayepps I never lost hope, no worries! And yep, I saw select but I think the semantics of collect are a little cleaner/easier to reason about, esp. since it only requires 1 function arg instead of 2. I also expect select would feel a little duplicative if you need to target the same nested property for both mapping and filtering.

But great, I'll go ahead and open a PR soon, thanks

@adamhamlin adamhamlin linked a pull request Jul 8, 2023 that will close this issue
5 tasks
@adamhamlin
Copy link
Author

Opened #327 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants