Skip to content

feat: add UnionBy and UnionByErr#878

Merged
samber merged 1 commit into
masterfrom
feat-unionby
May 23, 2026
Merged

feat: add UnionBy and UnionByErr#878
samber merged 1 commit into
masterfrom
feat-unionby

Conversation

@samber
Copy link
Copy Markdown
Owner

@samber samber commented May 4, 2026

Adding UnionBy helper function

Adds UnionBy and UnionByErr helpers to compute the union of multiple
collections using a key iteratee, mirroring the pattern of UniqBy.
Copilot AI review requested due to automatic review settings May 4, 2026 16:23
@codecov
Copy link
Copy Markdown

codecov Bot commented May 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.60%. Comparing base (6f2b2bc) to head (55cbd19).

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              
Flag Coverage Δ
unittests 92.60% <100.00%> (+0.02%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 UnionBy and UnionByErr implementations in intersect.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 thread intersect.go
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 thread intersect.go
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) {
Comment thread README.md

### UnionBy

Returns all distinct elements from predicate returns. Result will not change the order of elements relatively.
Comment thread README.md

### 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.
Comment thread docs/data/core-unionby.md
- 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 thread intersect_test.go
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 thread intersect_test.go
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)
@samber samber merged commit 8c82fb8 into master May 23, 2026
20 of 23 checks passed
@samber samber deleted the feat-unionby branch May 23, 2026 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants