GithubHelp home page GithubHelp logo

Add lo.MapByPtr about lo HOT 3 OPEN

afadeevz avatar afadeevz commented on July 20, 2024 2
Add lo.MapByPtr

from lo.

Comments (3)

afadeevz avatar afadeevz commented on July 20, 2024

I can create a PR if proposal will be approved

from lo.

d0x7 avatar d0x7 commented on July 20, 2024

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.

afadeevz avatar afadeevz commented on July 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.