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

Extensions for sequence comparing #737

Closed
dmitrynogin opened this issue Dec 13, 2019 · 3 comments
Closed

Extensions for sequence comparing #737

dmitrynogin opened this issue Dec 13, 2019 · 3 comments

Comments

@dmitrynogin
Copy link

dmitrynogin commented Dec 13, 2019

What about adding extensions allowing producing "diffgrams" between sequences of objects with value and id identity?

The desired behavior would be:

        [TestMethod]
        public void Compare_Value_Identity()
        {
            var source = new[] { 1, 2 };
            var modified = new[] { 2, 3 };
            
            var (inserted, deleted) = source.Compare(modified);

            CollectionAssert.AreEqual(new[] { 3 }, inserted.ToArray());
            CollectionAssert.AreEqual(new[] { 1 }, deleted.ToArray());
        }

And:

        [TestMethod]
        public void Compare_Id_Identity()
        {
            var source = new[] { (1, "A"), (2, "B"), (3, "C") };
            var modified = new[] { (2, "B"), (3, "D"), (4, "E") };

            var (inserted, updated, deleted) = source.Compare(modified, x => x.Item1);

            CollectionAssert.AreEqual(new[] { (4, "E") }, inserted.ToArray());
            CollectionAssert.AreEqual(new[] { (3, "D") }, updated.ToArray());
            CollectionAssert.AreEqual(new[] { (1, "A") }, deleted.ToArray());
        }

Library code is:

    public static class SequenceChange
    {
        public static (IEnumerable<T> Inserted, IEnumerable<T> Deleted) Compare<T>(
            this IEnumerable<T> source, IEnumerable<T> modified) =>
            source.Compare(modified, EqualityComparer<T>.Default);

        public static (IEnumerable<T> Inserted, IEnumerable<T> Deleted) Compare<T>(
            this IEnumerable<T> source, IEnumerable<T> modified, IEqualityComparer<T> comparer) =>
            (modified.Except(source, comparer), source.Except(modified, comparer));

        public static (IEnumerable<T> Inserted, IEnumerable<T> Updated, IEnumerable<T> Deleted) Compare<T, TKey>(
            this IEnumerable<T> source, IEnumerable<T> modified, Func<T, TKey> keySelector) =>
            source.Compare(modified, keySelector, EqualityComparer<T>.Default);

        public static (IEnumerable<T> Inserted, IEnumerable<T> Updated, IEnumerable<T> Deleted) Compare<T, TKey>(
            this IEnumerable<T> source, IEnumerable<T> modified, Func<T, TKey> keySelector, IEqualityComparer<T> comparer) =>
            (modified.ExceptBy(source, keySelector),
            from s in source
            join m in modified on keySelector(s) equals keySelector(m)
            where !comparer.Equals(s, m)
            select m,
            source.ExceptBy(modified, keySelector));
    }  
@leandromoh
Copy link
Collaborator

you can get this result using FullGroupJoin operator.

@atifaziz
Copy link
Member

atifaziz commented May 4, 2020

@dmitrynogin Thanks for your proposal but I think ListDiff by @praeclarum should be quite sufficient for this so I don't see much point/motivation in maintaining duplicate functionality separately as part of this library.

@atifaziz
Copy link
Member

Closing based on previous comment.


See also praeclarum/ListDiff#12 that renders ListDiff even more generic if the PR ever gets merged.

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

No branches or pull requests

3 participants