x/exp/slices: add Map/Extract function

261 Aufrufe
Direkt zur ersten ungelesenen Nachricht

cpu...@gmail.com

ungelesen,
20.03.2022, 13:14:1520.03.22
an golang-nuts
Playing with 1.18 I've noticed that I can immediately replace pretty much all uses of https://github.com/thoas/go-funk with the new slices package. 

One common use case that I happen to have in the codebase quite a lot is extracting data from a slice of objects:

funk.Map(chargers, func(c easee.Charger) string { return c.ID })

I feel it would be nice to add something like that as part of slices:

func Map[From, To any](s []From, f func(From) To) []To {
res := make([]To, len(s))
for i, v := range s {
res[i] = f(v)
}
return res
}

Has this already been discussed?

Thanks,
Andi

Carla Pfaff

ungelesen,
20.03.2022, 13:29:0420.03.22
an golang-nuts
On Sunday, 20 March 2022 at 18:14:15 UTC+1 cpu...@gmail.com wrote:
Has this already been discussed?

Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten