feat: add AssignBy
I need the function to customize map merging.
Codecov Report
Merging #164 (3e6bf66) into master (05d69d7) will increase coverage by
0.08%. The diff coverage is100.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 dataPowered by Codecov. Last update 05d69d7...3e6bf66. Read the comment docs.
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?
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?