Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dl/difference-by: Added differenceBy #426

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

lydamian
Copy link

@lydamian lydamian commented Feb 5, 2024

Issue

Added difference-by

Summary of changes

// DifferenceBy returns the difference between two collections: the elements from list1 that are not present in list2,
// and the elements from list2 that are not present in list1, based on the provided predicate function.
// The predicate function is used to compare elements from both lists.
// It takes two arguments of type T and returns a boolean value indicating whether the elements are considered equal.
// The returned slices maintain the order of the elements in the original lists.
func DifferenceBy[T any](
	list1 []T,
	list2 []T,
	predicate func(T, T) bool,
) ([]T, []T)

I will call out though that this predicate signature/implementation is different than the lodashjs implementation found here https://lodash.com/docs/4.17.15#differenceBy but is consistent with the lodashjs version of differenceWith https://lodash.com/docs/4.17.15#differenceWith

Given that the sander/lo package doesn't have a pattern of using With(...) and that the function name and predicate shape is consistent with the naming convention of other similarly named functions in this Repo... (e.g NoneBy(...) ContainsBy(...)) I think this is naming convention is the best path forward for consistency and backwards compatibility sake.

Related issues

#411
#289 - I think this variant of difference would be nice to have as well but it should be named something different (maybe DifferenceTo or DifferenceWith) since it is inconsistent with other similarly named functions (e.g. NoneBy(...) ContainsBy(...)) in this repo

@lydamian lydamian changed the title dll/difference-by: Added difference-by dl/difference-by: Added difference-by Feb 5, 2024
@lydamian lydamian changed the title dl/difference-by: Added difference-by dl/difference-by: Added differenceBy Feb 5, 2024
@lydamian
Copy link
Author

lydamian commented Feb 8, 2024

Ready for review @samber

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.

None yet

1 participant