sodiray / radash

Functional utility library - modern, simple, typed, powerful
https://radash-docs.vercel.app
MIT License
4.31k stars 173 forks source link

Any interest in `collect`/`collectFirst`? #269

Open adamhamlin opened 1 year ago

adamhamlin commented 1 year ago

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.

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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

Opened https://github.com/rayepps/radash/pull/327 👍