Skip to content

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

Closed
eikes wants to merge 2 commits into
jashkenas:masterfrom
eikes:symmetric_difference
Closed

Add a function for the symmetric difference of two arrays#309
eikes wants to merge 2 commits into
jashkenas:masterfrom
eikes:symmetric_difference

Conversation

@eikes

@eikes eikes commented Sep 22, 2011

Copy link
Copy Markdown

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

#277

@jashkenas

Copy link
Copy Markdown
Owner

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

@jashkenas

Copy link
Copy Markdown
Owner

... apparently not.

@jashkenas jashkenas closed this Oct 18, 2011
@Aaronius

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Collaborator
_.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
…g _.difference accept any number of arguments.
@jashkenas

Copy link
Copy Markdown
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.

@michaelficarra

Copy link
Copy Markdown
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.

@eikes

eikes commented Nov 23, 2011

Copy link
Copy Markdown
Author

Neat!

@Aaronius

Copy link
Copy Markdown

Woohoo! Thanks!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants