c4se記:さっちゃんですよ☆

.。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆)

.。oO(此のblogは、主に音樂考察Programming に分類されますよ。ヾ(〃l _ l)ノ゙♬♪♡)

音樂は SoundCloud に公開中です。

考察は現在は主に Scrapbox で公表中です。

Programming は GitHub で開發中です。

CoffeeScriptで平方根Newton法 #js

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);