Open adamhamlin opened 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.
@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
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 isundefined
, that element is filtered out:collectFirst
-- same as above, but the first defined result is returned immediately:They are essentially more generalized versions of
filter
andfind
, 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 usingreduce
just feels like too much.@rayepps would you be interested in a PR for these additions?