其实生成 笛卡尔积的方法原本很简单,for循环就可以了,
function discarts() { //笛卡尔积 var twodDscartes = function (a, b) { var ret = []; for (var i = 0; i < a.length; i++) { for (var j = 0; j < b.length; j++) { ret.push(ft(a[i], b[j])); } } return ret; } var ft = function (a, b) { if (!(a instanceof Array)) a = [a]; var ret = a.slice(0); ret.push(b); return ret; } //多个一起做笛卡尔积 return (function (data) { var len = data.length; if (len == 0) return []; else if (len == 1) return data[0]; else { var r = data[0]; for (var i = 1; i < len; i++) { r = twodDscartes(r, data[i]); } return r; } })(arguments.length > 1 ? arguments : arguments[0]); }
调用方式:
var a = discarts([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); var a = discarts([['a', 'b', 'c'], [1, 2, 3, 4], ['A', 'B'], ['#', '@', '+'], ['Mary', 'Terry', 'KYO']]);
缺点:
.在有些时候我们需要返回的是一个迭代器,比如要生成10000号码,discarts已经循环了10000次,如果业务需要对着10000个号码需要过滤,那么还需要循环10000次,这样可是不行的哦。
那么修改后的code如下:
this.combins = function () { if (arguments.length < 2) return arguments[0] || []; var args = Array.prototype.slice.call(arguments); var that = { index: 0, nth: function (n) { var result = [], d = 0; for (; d < this.dim; d++) { var l = this[d].length; var i = n % l; result.push(this[d][i]); n -= i; n /= l; } return result; }, next: function () { if (this.index >= size) return; var result = this.nth(this.index); this.index++; return result; } }; var size = 1; for (var i = 0; i < args.length; i++) { size = size * args[i].length; that[i] = args[i]; } that.size = size; that.dim = args.length; return that; }
调用code:
var a = combins([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); var index = 0; while (c = a.next()) { index++; console.log(index+1+":"+c.join('')); }
由于时间关系,比较粗糙,有不当的地方还请大家指正。