-
-
Notifications
You must be signed in to change notification settings - Fork 847
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
Add lo.MapByPtr #425
Comments
I can create a PR if proposal will be approved |
But as you said, it's expensive and some elements aren't copyable - so if (especially the latter one) thats the case, why are you not using pointers in the first case? By having an slice of pointers to your struct, you avoid ever copying your elements, and you can use If you really have a use case where it's necessary to use a non-pointer slice of something, than sure, that'd be a valid use case for such a MapByPtr func — it's just that I almost never use an slice of some struct, but almost always a pointer. And with that, the following works just fine. type Account struct { Id int }
slice := []*Account{{Id: 1}, {Id: 2}, {Id: 3}}
accountIds := lo.Map(slice, func(item *Account, index int) int {
return item.Id
}) |
I had a real use case. I needed to iterate over slice of structs which was created in generated code from protobuf. Those structs have embedded mutex. Linters went crazy with warnings. It was somewhat painful to copypaste code with small changes to my own package just to avoid those warnings. |
When we use
lo.Map
elements are passed by value. That's not OK in some cases:I propose to add new function
lo.MapByPtr
(or some other name) with implementation pretty much similar to lo.Map:The text was updated successfully, but these errors were encountered: