Skip to content

Instantly share code, notes, and snippets.

@hryk
Created April 13, 2010 16:15
Show Gist options
  • Save hryk/364780 to your computer and use it in GitHub Desktop.
Save hryk/364780 to your computer and use it in GitHub Desktop.
順列 javascript
var Permutate = function(list, length){
this.set = list;
if (typeof(length) == "number") {
this.len = length - 1;
}
else if (typeof(length) == "object") {
this.len = length; /* in case of Array */
}
}
Permutate.prototype = {
"nest_for" : function(ary, sub, bind){
if (typeof(bind) == 'undefined') {
bind = "";
}
for (var j=0;j<ary.length;j++) {
sub(bind+ary[j]);
}
},
"wrap" : function(s) {
var bind = this;
return function(s2){ bind.nest_for(bind["set"], s, s2);};
},
"run" : function(func){
var bind = this;
if (typeof(this.len) == "number") {
for (var i=0;i<this.len;i++) {
func = this.wrap(func);
}
this.call(func, "");
}
else {
var func_orig = func;
for each(var l in this.len) {
for (var i=0;i<(l-1);i++) {
func = this.wrap(func);
}
this.call(func, "");
func = func_orig;
}
}
},
"call": function(func, src_arg){
this.nest_for(this["set"], func, src_arg);
}
};
p = new Permutate([1,2,3,4], [2,3]);
p.run(function(sequence){ print(sequence); });
@hryk
Copy link
Author

hryk commented Apr 13, 2010

出力する文字列の長さを配列で指定できる

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