Created
June 15, 2015 20:51
-
-
Save jeremiahbiard/eef39c98d0b4cfefd2bd to your computer and use it in GitHub Desktop.
Solution to the symmetric difference bonfire. I love functional programming.
This file contains 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
function complement(A, B) { | |
return A.filter(function(elem) { return B.indexOf(elem) == -1; }); | |
} | |
function unique(arr) { | |
return arr.filter(function(elem, pos) { | |
return arr.indexOf(elem) == pos; | |
}); | |
} | |
function sym(args) { | |
var arg = [].slice.call(arguments); | |
return arg.map(unique).reduce(function(previous, current) { | |
return complement(previous, current).concat(complement(current, previous)); }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment