lo icon indicating copy to clipboard operation
lo copied to clipboard

feat: add AssignBy

Open yoshidan opened this issue 3 years ago • 3 comments

I need the function to customize map merging.

yoshidan avatar Jun 22 '22 08:06 yoshidan

Codecov Report

Merging #164 (3e6bf66) into master (05d69d7) will increase coverage by 0.08%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #164      +/-   ##
==========================================
+ Coverage   89.89%   89.97%   +0.08%     
==========================================
  Files          12       12              
  Lines        1326     1337      +11     
==========================================
+ Hits         1192     1203      +11     
  Misses        132      132              
  Partials        2        2              
Flag Coverage Δ
unittests 89.97% <100.00%> (+0.08%) :arrow_up:

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

Impacted Files Coverage Δ
map.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 05d69d7...3e6bf66. Read the comment docs.

codecov-commenter avatar Jul 03 '22 19:07 codecov-commenter

Hi @yoshidan and thanks for this first contribution.

I wonder if it would be better to use a variadic callback such as iteratee(args ...T) T, instead of a simple iteratee(a T, b T) T.

I suppose in most cases, it is easier to build a callback with a fixed number of arguments, but sometimes, having all intermediary values would be very useful.

WDYT?

samber avatar Jul 03 '22 20:07 samber

Hi @samber and thanks for your review.

I implemented it with reference to mergeWith. Since customzer needs oldValue and srcValue, I thought it would be better to limit the argument of iteratee to two. If it should be general purpose, it would be better to include the key and map itself in the callback like the internal implementation of mergeWith.

iteratee func(V, V, K, map[K]V, map[K]V)
result1 := AssignBy(func(a int, b int, _ string, _ map[string]int, _ map[string]int) int {
     return a + b
}, map[string]int{"a": 1, "b": 2}, map[string]int{"b": 3, "c": 4})

I couldn't think of a use case that I wanted to have variadic arguments, so could you please tell me when it's needed?

yoshidan avatar Jul 04 '22 00:07 yoshidan