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
Proposal: Add functors that iterate values only #263
Comments
How about adding a function that ignores second argument? It's more generic, and don't have to make aliases of many functions. keys := lo.Map(channels, lo.Ignore1(makeKey))
func Ignore1[T0 any, T1 any, R any](f func(T0) R) func(T0,T1) R {
return func(v0 T0,_ T1) R {
return f(v0)
}
} |
I think it's a good compromise. I thought about the func NoIndex[T, R any](f func(T) R) func(T, int) R {
return func(t T, _ int) R {
return f(t)
}
} The "Ignore" functor is better for sure. Let's consider While I think the alias way is complicated for library developers, and the "Ignore" way is complicated for library users. |
I agree with you. lo.Ignore is way too complicated for what it does. (Also users should specify all generic types. |
@samber What's your opinion on this proposal? Glad to hear that. |
It's really too tedious to constantly wrap any function passed to I think adding |
We often have a |
#259 as example |
Having just written the same |
Yes please, the extra argument makes the usage often no more readable or easy to use than the loop variant of the same functionality. |
Functors like "Map" is handy, but can we make it better?
Say we have
func Map[T any, R any](collection []T, iteratee func(T, int) R) []R
I guess we would write code like this often.
keys := lo.Map(channels, func(channel typed.Channel, _ int) string { return makeKey(channel) })
It could be even better if we add an alias to Map functor, that iterate values only.
func MapV[T, R any](collection []T, iteratee func(T)R) []R
So we can turn the previous code into this.
keys := lo.MapV(channels, makeKey)
It would make the code much clear.
And it can be implemented easily like this.
And other functors like Reduce, Filter should benefit from these alias too.
Would this contribution be appreciated @samber ?
The text was updated successfully, but these errors were encountered: