Skip to content

Add DifferenceBy to compute differences by a derived key AND align Difference naming#867

Open
chg1f wants to merge 2 commits into
samber:masterfrom
chg1f:master
Open

Add DifferenceBy to compute differences by a derived key AND align Difference naming#867
chg1f wants to merge 2 commits into
samber:masterfrom
chg1f:master

Conversation

@chg1f
Copy link
Copy Markdown
Contributor

@chg1f chg1f commented Apr 22, 2026

Describe your changes

This PR adds DifferenceBy, a new helper that computes the asymmetric difference between two collections using a custom key selector. It fills the gap next to Difference and IntersectBy: callers can now compare slices of non-comparable values, or compare values by a derived identity, without having to manually project and diff intermediate slices.

DifferenceBy keeps the same directional semantics as Difference. The first returned slice contains elements from left whose keys are absent from right, and the second returned slice contains elements from right whose keys are absent from left. The implementation preserves the input order of each side and keeps duplicates when their extracted keys are not present on the other side, which matches the current behavior and mental model of Difference.

This PR also includes a follow-up naming cleanup for Difference. The behavior of Difference is unchanged, but its inputs and outputs now use the same directional terminology as DifferenceBy: left, right, notInRight, and notInLeft. I included this cleanup in the same PR not only to keep the base helper and its new By variant consistent across implementation, tests, examples, and documentation, but also because the renamed return values make it more concise and explicit what each returned slice contains.

In addition to the core implementation, this PR updates the related tests, adds an example to lo_example_test.go, updates the README entries for both helpers, and adds the docs/data page for DifferenceBy while aligning the Difference page with the new terminology. This keeps the public API surface, examples, and generated helper metadata in sync.

Validation:

  • go test ./...
  • node docs/scripts/check-similar-keys-exist-in-directory.js
  • node docs/scripts/check-cross-references.js
  • golangci-lint fmt --diff -E gofumpt intersect.go

Checklist before requesting a review

  • 👓 I have performed a self-review of my code
  • 👶 This helper does not already exist
  • 🧪 This helper is tested
  • 🏎️ My code limits memory allocation and is fast
  • 🧞‍♂️ This helper is immutable and my tests prove it
  • ✍️ I implemented the parallel, iterator and mutable variants
  • 🔬 An example has been added to lo_example_test.go
  • ⛹️ An example has been created on https://go.dev/play and added in comments
  • 📖 My helper has been added to documentation
    • in README.md
    • in docs/data/*.md
    • in docs/static/llms.txt

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.06%. Comparing base (6075142) to head (677954f).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #867      +/-   ##
==========================================
+ Coverage   92.03%   92.06%   +0.03%     
==========================================
  Files          32       32              
  Lines        6177     6203      +26     
==========================================
+ Hits         5685     5711      +26     
  Misses        389      389              
  Partials      103      103              
Flag Coverage Δ
unittests 92.06% <100.00%> (+0.03%) ⬆️

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.

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.

1 participant