lo icon indicating copy to clipboard operation
lo copied to clipboard

feat(func): add `FuncWithIndex` and `FuncWithIndexNoReturn`

Open quantumsheep opened this issue 1 year ago • 8 comments

Hey! I regularly write some functions that I pass to lo.Map and it's a hassle to have to pass the index every time.

Currently:

lo.Map(slice, func (v string, _ int) string {
	return doThing(v)
})

With the PR:

lo.Map(slice, lo.FuncWithIndex(doThing))

I'm not sure about the naming, it could simply be WithIndex.

I hope you like the idea.

quantumsheep avatar Nov 28 '24 18:11 quantumsheep

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 94.35%. Comparing base (efef0ff) to head (7b493bc).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #565      +/-   ##
==========================================
+ Coverage   94.33%   94.35%   +0.01%     
==========================================
  Files          17       17              
  Lines        2790     2798       +8     
==========================================
+ Hits         2632     2640       +8     
  Misses        154      154              
  Partials        4        4              
Flag Coverage Δ
unittests 94.35% <100.00%> (+0.01%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Nov 28 '24 18:11 codecov[bot]

Wow. also faced that issue, and made a quite similar solution:

  • https://github.com/samber/lo/issues/330#issuecomment-2507770542

itroot avatar Nov 29 '24 13:11 itroot

Wow. also faced that issue, and made a quite similar solution:

* [Curried map, reduce and filter for partial application  #330 (comment)](https://github.com/samber/lo/issues/330#issuecomment-2507770542)

Glad to see I'm not the only one wanting a feature like this :)

quantumsheep avatar Nov 29 '24 15:11 quantumsheep

I agree.

But I would vote for duplicating lo.Map helper:

  • lo.Map
  • lo.MapI

samber avatar Jan 24 '25 23:01 samber

@samber lo.MapI would be the version with or without index?

If it's the one with index, I wonder if that's a good idea as it will break many projects. Or it will require a major update.

What do you think?

quantumsheep avatar Jan 25 '25 13:01 quantumsheep

Yes 😅

I would break a large ecosystem. We would need to group all that changes into a single v2 major update.

I'm just sharing some ideas. Not telling we are going to do it right now 😇

samber avatar Jan 25 '25 14:01 samber

Then I really prefer this option! I'm not sure how people use lo.Map right now but I guess the index argument is not that much used.

Is there a place where contributors can discuss the changes for v2? I would like to help 😄

quantumsheep avatar Jan 25 '25 14:01 quantumsheep

@quantumsheep see #579

samber avatar Jan 25 '25 23:01 samber