Javascript 对称差分的递推函数

Javascript 对称差分的递推函数,javascript,Javascript,我制作了一个函数来计算作为参数传递的数组之间的非对称差。我对两个阵列进行了测试,效果良好。现在的问题是我想把函数扩展到n个变量。我认为如果函数的arguments.length等于2,我应该计算symm差,否则我应该调用递归函数来计算其他元素和前两个元素之间的symm差?我不知道,我很困惑 function sym(args) { var arr=[].slice.call(arguments); var cnts={}; var result=[]; if(argument

我制作了一个函数来计算作为参数传递的数组之间的非对称差。我对两个阵列进行了测试,效果良好。现在的问题是我想把函数扩展到n个变量。我认为如果函数的arguments.length等于2,我应该计算symm差,否则我应该调用递归函数来计算其他元素和前两个元素之间的symm差?我不知道,我很困惑

function sym(args) {

  var arr=[].slice.call(arguments);
  var cnts={};
  var result=[];

  if(arguments.length==2){

    arr=arguments[0].concat(arguments[1]);
    console.log(arr);
    for(var number in arr){

      if(cnts.hasOwnProperty(arr[number])){

         ++cnts[arr[number]].cnt;

       }

      else   cnts[arr[number]]={cnt:1,val:arr[number]};

     }

     for(var counts in cnts){

        if(cnts[counts].cnt===1) result.push(cnts[counts].val);

      }

    }

    else{  

      var first=arguments[0];
      var nextDiff=function(next){

        return ...........?????????;

      }; 

     }  

  return result;
}

sym([1, 2, 5], [2, 3, 5], [3, 4, 5]);

这里有两个关键的见解。首先是我们有

sym_diff(A1, A2, ..., An) === sym_diff(sym_diff(A1, A2), A3, ..., An)
这是因为对称差分是关联的,允许我们重复出现

第二个是

sym_diff(A, B) === diff(A, B) ++ diff(B, A)
这里的
++
表示联合,而
diff
是通常的相对差异

因此:

function sym_diff() {
    // Convert the passed arguments to an array for convenience
    let args = Array.prototype.slice.call(arguments);

    // This is an example of an immediately-invoked function expression 
    // (IIFE). Basically, we define a function and then immediately call it (see * below)
    // in one go and return the result

    return (function sym_diff(a, b) {
        // a: the first argument
        // b: an array containing the rest of the arguments

        if (!b.length) {
            // If only a is given, return a if is an array, undefined otherwise
            return Array.isArray(a) ? a : undefined;
        }
        else if (b.length === 1) {
            // Define a function that takes two arrays s and t, and returns
            // those elements of s that are not in t. This is an 
            // example of arrow notation`
            let diff = (s, t) => s.filter(i => t.indexOf(i) === -1);

            // Use the second insight to compute the sym_diff of a and
            // b[0]
            return diff(a, b[0]).concat(diff(b[0], a));
        }
        else {
            // Use the first insight to recursively compute the sym_diff
            // We pass [b[0]] because sym_diff expects an array of arrays as the second argument
            // b.slice(1) gives all of b except the first element
            return sym_diff(sym_diff(a, [b[0]]), b.slice(1));
        }
    })(args[0], args.slice(1)); //* Here is where we pass the arguments to the IIFE
}

这里有两个关键的见解。首先是我们有

sym_diff(A1, A2, ..., An) === sym_diff(sym_diff(A1, A2), A3, ..., An)
这是因为对称差分是关联的,允许我们重复出现

第二个是

sym_diff(A, B) === diff(A, B) ++ diff(B, A)
这里的
++
表示联合,而
diff
是通常的相对差异

因此:

function sym_diff() {
    // Convert the passed arguments to an array for convenience
    let args = Array.prototype.slice.call(arguments);

    // This is an example of an immediately-invoked function expression 
    // (IIFE). Basically, we define a function and then immediately call it (see * below)
    // in one go and return the result

    return (function sym_diff(a, b) {
        // a: the first argument
        // b: an array containing the rest of the arguments

        if (!b.length) {
            // If only a is given, return a if is an array, undefined otherwise
            return Array.isArray(a) ? a : undefined;
        }
        else if (b.length === 1) {
            // Define a function that takes two arrays s and t, and returns
            // those elements of s that are not in t. This is an 
            // example of arrow notation`
            let diff = (s, t) => s.filter(i => t.indexOf(i) === -1);

            // Use the second insight to compute the sym_diff of a and
            // b[0]
            return diff(a, b[0]).concat(diff(b[0], a));
        }
        else {
            // Use the first insight to recursively compute the sym_diff
            // We pass [b[0]] because sym_diff expects an array of arrays as the second argument
            // b.slice(1) gives all of b except the first element
            return sym_diff(sym_diff(a, [b[0]]), b.slice(1));
        }
    })(args[0], args.slice(1)); //* Here is where we pass the arguments to the IIFE
}

可能的副本可能的副本谢谢!你能给我解释一下这个代码吗?我开始了,我不太明白。例如符号差异呼叫中的b是什么?它接受除第一个参数之外的所有参数?我在代码中添加了一些注释。希望能有帮助。谢谢!你能给我解释一下这个代码吗?我开始了,我不太明白。例如符号差异呼叫中的b是什么?它接受除第一个参数之外的所有参数?我在代码中添加了一些注释。希望有帮助。