feat: add UnionBy and UnionByErr#878
Merged
Merged
Conversation
Adds UnionBy and UnionByErr helpers to compute the union of multiple collections using a key iteratee, mirroring the pattern of UniqBy.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #878 +/- ##
==========================================
+ Coverage 92.57% 92.60% +0.02%
==========================================
Files 32 32
Lines 5067 5095 +28
==========================================
+ Hits 4691 4718 +27
- Misses 273 274 +1
Partials 103 103
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds UnionBy and UnionByErr helpers to the core intersect utilities, enabling union-of-slices behavior based on a computed key (with an error-capable variant), and wires them into the public documentation.
Changes:
- Added
UnionByandUnionByErrimplementations inintersect.go. - Added unit tests for the new helpers in
intersect_test.go. - Updated README and added new docs pages for the helpers.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| README.md | Adds README index entries + usage sections for UnionBy / UnionByErr. |
| intersect.go | Implements UnionBy and UnionByErr alongside existing intersection helpers. |
| intersect_test.go | Adds tests covering basic functionality and error behavior. |
| docs/data/core-unionby.md | Introduces docs front matter + examples for UnionBy. |
| docs/data/core-unionbyerr.md | Introduces docs front matter + examples for UnionByErr. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+257
to
+260
| // UnionBy is like Union except that it accepts iteratee which is invoked for each element of each collections | ||
| // to generate the criterion by which uniqueness is computed. | ||
| // Result values are chosen from the first collections in which the value occurs. | ||
| // Play: TODO. |
Comment on lines
+284
to
+287
| // UnionByErr is like UnionBy except that it accepts iteratee which can return an error. | ||
| // It returns the first error returned by the iteratee. | ||
| // Play: TODO. | ||
| func UnionByErr[T any, V comparable, Slice ~[]T](iteratee func(item T) (V, error), lists ...Slice) (Slice, error) { |
|
|
||
| ### UnionBy | ||
|
|
||
| Returns all distinct elements from predicate returns. Result will not change the order of elements relatively. |
|
|
||
| ### UnionByErr | ||
|
|
||
| Returns all distinct elements from predicate returns. Result will not change the order of elements relatively. It returns the first error returned by the iteratee. |
| - core#intersect#intersect | ||
| - core#intersect#intersectby | ||
| - core#slice#uniq | ||
| - core#slice#uniqby |
Comment on lines
+7
to
+13
| playUrl: | ||
| variantHelpers: | ||
| - core#intersect#unionby | ||
| - core#intersect#unionbyerr | ||
| similarHelpers: | ||
| - core#intersect#unionby | ||
| - core#intersect#union |
Comment on lines
+330
to
+340
| result11 := UnionBy(testFunc, []int{0, 1, 2, 3, 4, 5}, []int{0, 2, 10}, []int{0, 1, 11}) | ||
| result12 := UnionBy(testFunc, []int{0, 1, 2, 3, 4, 5}, []int{6, 7}, []int{8, 9}) | ||
| result13 := UnionBy(testFunc, []int{0, 1, 2, 3, 4, 5}, []int{}, []int{}) | ||
| result14 := UnionBy(testFunc, []int{0, 1, 2}, []int{0, 1, 2}, []int{0, 1, 2}) | ||
| result15 := UnionBy(testFunc, []int{}, []int{}, []int{}) | ||
| is.Equal([]int{0, 2, 4, 10}, result11) | ||
| is.Equal([]int{0, 2, 4, 6, 8}, result12) | ||
| is.Equal([]int{0, 2, 4}, result13) | ||
| is.Equal([]int{0, 2}, result14) | ||
| is.Equal([]int{}, result15) | ||
| } |
Comment on lines
+383
to
+394
| errFunc := func(i int) (int, error) { | ||
| if i == 2 { | ||
| return 0, assert.AnError | ||
| } | ||
| return i / 2, nil | ||
| } | ||
|
|
||
| _, err6 := UnionByErr(errFunc, []int{0, 1, 2, 3, 4, 5}, []int{0, 2, 10}) | ||
| is.Error(err6) | ||
|
|
||
| _, err7 := UnionByErr(errFunc, []int{0, 1, 3, 4, 5}, []int{2, 10}) | ||
| is.Error(err7) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adding UnionBy helper function