Lispの本読んでる筈なんだけどなー。練習。
sqrt = (x) -> prev = x next = x / 2 while prev isnt next prev = next next = predictive_next_sqrt x, prev next predictive_next_sqrt = (x, value) -> (value + x / value) / 2 module.exports.sqrt = sqrt
coffee -cで生成されたJavaScript。
(function() { var predictive_next_sqrt, sqrt; sqrt = function(x) { var next, prev; prev = x; next = x / 2; while (prev !== next) { prev = next; next = predictive_next_sqrt(x, prev); } return next; }; predictive_next_sqrt = function(x, value) { return (value + x / value) / 2; }; module.exports.sqrt = sqrt; }).call(this);
uglifyjsでコンパイル。
((function(){var a,b;b=function(b){var c,d;d=b,c=b/2;while(d!==c)d=c,c=a(b,d);return c},a=function(a,b){return(b+a/b)/2},module.exports.sqrt=b})).call(this);