Created
April 13, 2010 16:15
-
-
Save hryk/364780 to your computer and use it in GitHub Desktop.
順列 javascript
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
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); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
出力する文字列の長さを配列で指定できる