æ代é ãã²ã¨ãFizzBuzzç¥ããä»åã¯JScript on WSHãåã åã®PowerShellããã®æµãã§ãããããã¡ã¤ã«ã®å¾ç¶ç¹ããã§WSHãé¸ãã ã¨ãè¨ããããååã®REXXå ±ã ãããããã¡ã¤ã«ã®ä»£ç¨ãã¨ããå´é¢*1ã§å ±éæ§ãããã¨ãè¨ããããã£ã¨ãWSHã¯ãã¾ãåºã¾ããã¨ãªãå»ããä¸æ¹ã§ããããä»å¾ã¯Windows PowerShellã«ç§»è¡ãã¦ããã¯ããªã®ã§ãå ã åºããªãã£ãæ´»èºã®å ´ãæ´ã«çããªãããã ã
ããã§ãå人çã«ã¯WSHã¯æå¤ã¨ãªã¹ã¹ã¡ã ã¨æããç¹ã«ããã°ã©ãåãã«JScriptãªãã¦ã©ãã ããï¼
çç±ï¼
- JScriptã使ããã¨ãããã¨ã¯ãè¦ããã«JavaScriptï¼ã®è¦ªæï¼ã§Windowsä¸ã§åãå°ãã¼ã«ãæ¸ããã¨ãããã¨ã ãJavaScriptã¯çµæ§å¼·åãªè¨èªã ãç¡åé¢æ°ã¨ãã¯ãã¼ã¸ã£ã¨ããååã«ä½¿ãã¹ãï¼
- HTAï¼HTML Applicationï¼ã¨çµã¿åããããã¨ã§ãã¯ã©ã¤ã¢ã³ããµã¤ãJavaScriptã®è¦é ã§ç°¡æGUIã¢ããªãå®è£ ã§ããã
COMã³ã³ãã¼ãã³ãå¨ããé¤ãã°ãJScriptãHTAã«ããéçºã§ã¯JavaScriptã®ç¥èã®å¤§é¨åãæµç¨ã§ãããJavaScriptã«ã¤ãã¦ã¯ãªã©ã¤ãªã¼ã®ãJavaScript 第5çãã¨ããè¯æ¸ãããããWSHã®åºæ¬çãªã³ã³ãã¼ãã³ãã«ã¤ãã¦ã¯ãããä¸ã§æ å ±åéããã°äºè¶³ãã*2ãã¾ããã®ä»ã®COMã³ã³ãã¼ãã³ãã®æ å ±ãæ¢ãã«ããã®ãè¾ãã¨ããã§ã¯ãããã
ãã¨ä»äººã«ä½¿ã£ã¦ããããã¼ã«ãä½ãå ´åã«ãç¾ç¶ã§ã¯PowerShellãããWSHã®æ¹ã使ã£ã¦ããããããã¨ããé¢ãããã®ã¯å¦å®ã§ããªãã£ãããããWindows XPãVistaã«æåããå ¥ã£ã¦ãããã¨ããé¢ã¯å¤§ããã
JScriptã¯ä¸å¿ECMAScriptã®å®è£ ã¨ãããæ¹è¨ã¨ããããããªæããªã®ã§ã極åECMAScriptç±æ¥ã®æ©è½ã«çµãè¾¼ãã§FizzBuzzãæ¸ãã¦ã¿ããåä½ç°å¢ã¯Windows XP Professional SP3ä¸ã®WSH 5.7ã
(function() { var print = function(s) { WScript.Echo(s); }; var fizzbuzz = function(n) { var fb_str = [ n%3 === 0 ? 'Fizz' : '', n%5 === 0 ? 'Buzz' : '', ].join(''); return fb_str.length === 0 ? n : fb_str; }; var i; for (i = 1; i <= 100; ++i) { print(fizzbuzz(i)); } })();
æå³ã¯ãªããã©é åã§æååãé£çµãã¦ã¿ãã大éã®æååãæåå以å¤ã®ãªãã¸ã§ã¯ããå¤ãå«ãæã¯+æ¼ç®åãããé åã§é£çµããã»ããå¹ççããããããã®ç¨åº¦ã§ã¯å¹æã¯ç¡ãã¯ãã ã
ç§ã¯æµãããããæ§æ ¼ãããã以åèªãã ãJavaScript: The Good Partsãã®å½±é¿ãåããæ¸ãæ¹ããã¦ããã¨æãã
(function() { var print = function(s) { WScript.Echo(s); }; var fizzbuzz = function(n) { var ref_tbl = [n, 'Fizz', 'Buzz', 'FizzBuzz']; var fizz = (n%3 === 0) ? 1 : 0; var buzz = (n%5 === 0) ? 2 : 0; return ref_tbl[fizz + buzz]; }; var i; for (i = 1; i <= 100; ++i) { print(fizzbuzz(i)); } })();
ECMAScriptã®æ¨æºãªãã¸ã§ã¯ãã¯ãããªãã«æ©è½ãæã£ã¦ããããä»ã®LLã«æ¯ã¹ãã¨ã³ã³ãã¯ããªå°è±¡ãåãããå人çã«ã¯ãRubyã齧ã£ã¦ããçºãArrayã¯ã©ã¹ã®æ©è½ãä¸è¶³ãã¦ããããã«æããããStringã¯ã©ã¹ãã©ããç©è¶³ããªãã
prototype.jsã使ããã¨è¯ãã®ã ããWSHã§ãã®ã¾ã¾çµã¿è¾¼ãã§ä½¿ããã¨ã¯ã§ããªãã®ãæ®å¿µã *3ã
ãã®ãããã¯Mozillaã§ãç©è¶³ããªããæãã¦ããã®ããJavaScript 1.6以éãªã©ã§å¹¾ã¤ãã¡ã½ããã追å ããã¦ããããã®ä¸ã§ãä¸çªæ¬²ããã®ãArray.forEachã¡ã½ããã ã
(function() { var print = function(s) { WScript.Echo(s); }; if (Array.prototype.forEach === undefined) { // Mozillaã®JavaScript 1.6ã®æ¡å¼µæ©è½ã§ããArray.forEachã¨åçã®ã¡ã½ããã Array.prototype.forEach = function(fn, that) { var i, len = this.length; for (i = 0; i < len; ++i) { if (i in this) { fn.call(that, this[i], i, this); } } }; } var fizzbuzz = function(n) { var ref_tbl = [n, 'Fizz', 'Buzz', 'FizzBuzz']; var fizz = (n%3 === 0) ? 1 : 0; var buzz = (n%5 === 0) ? 2 : 0; return ref_tbl[fizz + buzz]; }; var make_array = function(n, fn) { var i, ary = []; for (i = 0; i < n; ++i) { ary.push(fn(i)); } return ary; }; make_array(100, function(n) { return fizzbuzz(n+1); }).forEach(print); })();
ãã ãå®ã®ã¨ããJavaScript 1.6ã®Array.forEachã¯thisãå¼æ°ã«åããããã«ãªã£ã¦ããç¹ã§å¾®å¦ã«ã¤ã³ã¿ã¼ãã§ã¼ã¹ãã´ãã£ã´ãã£ãã¦ããæãããã¦ããã¾ã好ãã«ãªããªããããã§ãããã°é常ã«å©ããã
Array.forEachãå®ç¾©ãã¦ããã¨ãããã使ã£ã¦é åãèããé¡ã®ä»ã®ã¡ã½ãããå¹¾ã¤ãå®ç¾©ã§ãããå¥ã«Array.forEachã使ç¨ããªãã¦ãå®ç¾©ã§ããã®ã ããä½ã¨ãªãä¼¼ããããªå 容ãæ¸ããã¨ã«ãªãããªããæããæ°åã«ãªã£ã¦ãã¾ãã
(function() { var print = function(s) { WScript.Echo(s); }; if (Array.prototype.forEach === undefined) { // Mozillaã®JavaScript 1.6ã®æ¡å¼µæ©è½ã§ããArray.forEachã¨åçã®ã¡ã½ããã Array.prototype.forEach = function(fn, that) { var i, len = this.length; for (i = 0; i < len; ++i) { if (i in this) { fn.call(that, this[i], i, this); } } }; } if (Array.prototype.map === undefined) { // Mozillaã®JavaScript 1.6ã®æ¡å¼µæ©è½ã§ããArray.mapã¨åçã®ã¡ã½ããã Array.prototype.map = function(fn, that) { var ary = [], thisp = this; this.forEach(function(val, i) { ary.push(fn.call(that, val, i, thisp)); }); return ary; }; } var fizzbuzz = function(n) { var ref_tbl = [n, 'Fizz', 'Buzz', 'FizzBuzz']; var fizz = (n%3 === 0) ? 1 : 0; var buzz = (n%5 === 0) ? 2 : 0; return ref_tbl[fizz + buzz]; }; var make_array = function(n, fn) { if (fn === undefined) { fn = function(i) { return i; }; } var ary, i; if (n <= 0) { throw new RangeError(); } ary = new Array(n); for (i = 0; i < ary.length; ++i) { ary[i] = fn(i); } return ary; }; make_array(100).map(function(n) { return fizzbuzz(n+1); }).forEach(print); })();
Array.forEachã使ç¨ãã¦Array.mapãå®ç¾©ãã両æ¹ã¨ã使ã£ã¦ã¿ãã
æ¬å½ã¯ãæå¾ã®1è¡ã¯ã
new Array(100).map(function(n) { return fizzbuzz(n+1); }).forEach(print);
ã®ããã«è¨è¿°ãããã®ã ããArray.forEachãArray.mapã¯å¤ãä»£å ¥ããã¦ããã¤ã³ããã¯ã¹ã«ã®ã¿ã³ã¼ã«ããã¯é¢æ°ãé©ç¨ããä»æ§ãªã®ã§ããnew Array(100)ãã§çæããç´å¾ã®é åã«ã¯ä½¿ããªã*4ã
JScriptï¼ç¹ã«WSHã®JScriptã®ããã«åçåã®ãã®ï¼ã¯ãå人çã«ã¯ãã£ã¨æµè¡ãã¦æ¬²ãããJavaScriptã¯å¼·åãªè¨èªãªã®ã§ãWebéçºä»¥å¤ã®åéã«ãæ®åãã¦ãããã¨ããããã®ã ããJScriptã¯æ¹è¨ã¨ã¯ããWindowsä»å±ã§LLã«ä¼¼ãæãã§ãã¼ã«ãå®è£ ã§ãã訳ã§ãçµæ§æåãªåè£ã ã¨æãã®ã ããããæªã ã«ãã¤ãã¼ã ããä»å¾ã®æ®åã®æã¿ã¯èããæ®å¿µã ã
*1:IBM PC-DOSãOS/2ã«ã¯REXXããã³ãã«ããã¦ãã¦ãããããã¡ã¤ã«ã®ä»£ããã«ä½¿ããã¦ãããWSHããMicrosoftã®å½åã®äºå®ã§ã¯ããããã¡ã¤ã«ã®ä»£ããã«ãªãã¯ãã ã£ãã®ã ãâ¦â¦ã
*2:ç§ã¿ããã«ãWSHã¯ã¤ãã¯ãªãã¡ã¬ã³ã¹ 第2çããªã©ãè³¼å ¥ãã¦ãããã
*3:å ãã¿ãå¿ããããè¥å¹²æç´ãããã°ä½¿ãããããã
*4:çæããç´å¾ãé åã®ã©ã®ã¤ã³ããã¯ã¹ã«ãå¤ãä»£å ¥ããã¦ããªãçºã