Skip to content

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.

1 participant