Add a function for the symmetric difference of two arrays #309

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
4 participants
@eikes

eikes commented Sep 22, 2011

There was a previous pull request here, which explained why difference is not symmetric. The functionality hasn't changed though...

#277

@jashkenas

This comment has been minimized.

Show comment
Hide comment
@jashkenas

jashkenas Oct 4, 2011

Owner

Out of curiosity, do you have any real-world code that makes use of this function?

Owner

jashkenas commented Oct 4, 2011

Out of curiosity, do you have any real-world code that makes use of this function?

@jashkenas

This comment has been minimized.

Show comment
Hide comment
@jashkenas

jashkenas Oct 18, 2011

Owner

... apparently not.

Owner

jashkenas commented Oct 18, 2011

... apparently not.

@jashkenas jashkenas closed this Oct 18, 2011

@Aaronius

This comment has been minimized.

Show comment
Hide comment
@Aaronius

Aaronius Nov 19, 2011

Can we re-open this? Real-world example (names changed to keep it simple though):

I have an array of objects that contain metadata of widget A. I have an array of objects that contain metadata of widget B. I want to know which metadata objects are different between the two.

Can we re-open this? Real-world example (names changed to keep it simple though):

I have an array of objects that contain metadata of widget A. I have an array of objects that contain metadata of widget B. I want to know which metadata objects are different between the two.

@Aaronius

This comment has been minimized.

Show comment
Hide comment
@Aaronius

Aaronius Nov 19, 2011

I should add that the commit from eikes only works with two arrays. I'd like it to work with an arbitrary number of arrays.

I should add that the commit from eikes only works with two arrays. I'd like it to work with an arbitrary number of arrays.

@jashkenas jashkenas reopened this Nov 19, 2011

@michaelficarra

This comment has been minimized.

Show comment
Hide comment
@michaelficarra

michaelficarra Nov 20, 2011

Collaborator
_.symmetricDifference = function() {
  return _.reduce(arguments, function(first, second) {
    return _.union(_.difference(first, second), _.difference(second, first));
  });
}
Collaborator

michaelficarra commented Nov 20, 2011

_.symmetricDifference = function() {
  return _.reduce(arguments, function(first, second) {
    return _.union(_.difference(first, second), _.difference(second, first));
  });
}

jashkenas added a commit that referenced this pull request Nov 23, 2011

Issue #309, adding symmetric difference as _.symDifference, and makin…
…g _.difference accept any number of arguments.
@jashkenas

This comment has been minimized.

Show comment
Hide comment
@jashkenas

jashkenas Nov 23, 2011

Owner

Fixed in the above commit. While I think Michael's implementation is an exceptionally beautiful bit of code, if anyone feels like throwing together a faster implementation that's equally small, I'd be willing to take that patch.

Owner

jashkenas commented Nov 23, 2011

Fixed in the above commit. While I think Michael's implementation is an exceptionally beautiful bit of code, if anyone feels like throwing together a faster implementation that's equally small, I'd be willing to take that patch.

@michaelficarra

This comment has been minimized.

Show comment
Hide comment
@michaelficarra

michaelficarra Nov 23, 2011

Collaborator

Yeah, I definitely don't like my implementation, but I figured I would adhere to the current convention of using other underscore functions in the definition of underscore functions. It'd probably be much more efficient if we removed all those function calls.

Collaborator

michaelficarra commented Nov 23, 2011

Yeah, I definitely don't like my implementation, but I figured I would adhere to the current convention of using other underscore functions in the definition of underscore functions. It'd probably be much more efficient if we removed all those function calls.

@eikes

This comment has been minimized.

Show comment
Hide comment

eikes commented Nov 23, 2011

Neat!

@Aaronius

This comment has been minimized.

Show comment
Hide comment
@Aaronius

Aaronius Nov 23, 2011

Woohoo! Thanks!

Woohoo! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment