Comments (3)
I can create a PR if proposal will be approved
from lo.
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 lo.Map
without any issues.
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
})
from lo.
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.
from lo.
Related Issues (20)
- Why Async doesn't close a chan? HOT 1
- propose: Add First() on slice, empty to return nil HOT 2
- Chunk has memory leak
- Slight performance boost on PickByKeys / OmitByKeys
- Add .Get() to Tuple family as alias for .Unpack() to match samber/mo
- Keys: accept multiple maps (vaargs) HOT 5
- Cancelable concurrent tasks
- Proposal: considering a new type of Group: a key mapper to a bunch of values, for extend map structure
- Proposal: Use generics for underling types
- propose: Add distinctByKey() on slice,return slince
- Suggestion: Add a sorting method by a certain type to slice and return the slice
- Error: could not resolve parametric type of accumulator: could not resolve interface type
- Proposal: Helpers for combine function and tuple HOT 1
- TYPO: Maybe a typo in Readme.md HOT 4
- proposal: Another PickByKeys that ouputs a slice
- How to lazy evaluate for the first return value of FilterMap HOT 1
- ForEachWhile method does not exist HOT 1
- Chunk: memory safety
- breaking changes to Last() in 1.42.0 HOT 1
- Add Nillable function
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lo.