Created
May 13, 2016 18:12
-
-
Save olange/525de34f236a06ea1ad4d9f918f977cd to your computer and use it in GitHub Desktop.
Symmetric difference of two Sets in CoffeeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# Given two sets A and B, returns a triple with the set of elements | |
# in A only, in both and in B only. | |
# | |
symmetricDiff = (setA, setB) -> | |
[inAOnly, inBoth, inBOnly] = [new Set(), new Set(), new Set()] | |
setA.forEach (eid) -> | |
(if setB.has( eid) then inBoth else inAOnly).add eid | |
setB.forEach (eid) -> | |
inBOnly.add( eid) unless inBoth.has( eid) | |
[inAOnly, inBoth, inBOnly] | |
a = new Set [55, 44, 33, 22, 11] | |
b = new Set [55, 1, 2, 3] | |
[A, both, B] = symmetricDiff a, b | |
console.info A, both, B | |
# Console output: | |
# -> Set {44, 33, 22, 11} Set {55} Set {1, 2, 3} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment