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

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

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

音樂は SoundCloud に公開中です。

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

Programming は GitHub で開發中です。

ES.next Promiseが未だ無い時にQで代用する

追記20140324
Bluebirdを使おう。

if (!global.Promise) {
  global.Promise = require('bluebird');
}

或いは

<script src="bower_components/bluebird/js/browser/bluebird.js"></script>

で済む。
cf. petkaantonov/bluebird https://github.com/petkaantonov/bluebird

Chromeには目出度くPromiseが実装され、Operaにも降りてきました。他のWeb browserは洞うでせうか。IEは未だ遠さうですね。
cf. Promiseが実装された - JS.next http://js-next.hatenablog.com/entry/2013/11/28/093230
cf. ES6 Promiseの何が美しいのか - snyk_s log http://saneyukis.hatenablog.com/entry/2013/12/02/223504
Promise (deferred, Future) にはjQuery.DeferredやQ http://documentup.com/kriskowal/q/ が好く使われてきた筈です。QにES.nextの互換APIが入ってゐるので其れを使ひます。

npm install q --save
bower install q --save

此れで、

if (! window.Promise) {
  /**
   * ES6 Promise
   */
  window.Promise = Q.Promise || Q.promise;
  (function () {
    'use strict';
    var i = 0, iz = 0, keys = Object.keys(Q),
        key;

    for (i = 0, iz = keys.length; key = keys[i], i < iz; ++ i) {
      window.Promise[key] = Q[key];
    }
  }());
}

怠け過ぎです。polyfillとは言へねえ。此れで動きます。

// Promise example.

function gen() {
  return new Promise(function (resolve, reject) {
    setTimeout(resolve, 2000, 'message');
  });
}

gen().then(function (message) { console.log(message); },
  function (error) { throw error; });

Promise.all([gen(), gen()]).
  then(function (message) { console.log(message); }).
  catch(function (error) { throw error; });

yieldやco https://github.com/visionmedia/co は待たう!
(=_ヾ)クシクシ

補遺

DartではFutureです。
cf. dart:async Library / Dart API Reference https://api.dartlang.org/docs/channels/stable/latest/dart_async.html

似たのにRx (Reactive extention) がある。遅延 (非同期) stream版のLINQだ。
cf. RxJS Reactive Extensions for JavaScript http://reactive-extensions.github.io/RxJS/

ScalaではFutureとPromiseが区別されてるが、あまり普遍的な根拠は無いんじゃないか、それでScala以外ではあまり気にしなくてもいいんじゃないか。不明だ。
cf. Future と Promise - Scala Documentation http://docs.scala-lang.org/ja/overviews/core/futures.html

洞れも (Future, LINQ, Rxも) monadじゃないかと思ふ。確かめる気力が未だ湧かない。
cf. Futures and promises - Wikipedia, the free encyclopedia http://en.wikipedia.org/wiki/Futures_and_promises
cf. モナドの定義とか - 檜山正幸のキマイラ飼育記 http://d.hatena.ne.jp/m-hiyama/20060418/1145322223
jQuerymonadだ」ではなく「jQuerymonadか?」と云ふ記事群。
cf. モナド: お前はもう知っている - TIM Labs http://labs.timedia.co.jp/2011/03/monad-you-already-know.html
cf. jQueryモナドだ - id:anatooのブログ http://d.hatena.ne.jp/anatoo/20100305/1267801847
と云ふか「命令形言語」はmonadだ。ともかく、います、そこに、モナドが。

String.prototype.startsWith

String.prototype.startsWith と String.prototype.endsWith のpolyfillも載せておく。

if (! String.prototype.startsWith) {
  Object.defineProperty(String.prototype, 'startsWith', {
    /**
     * ES6 String.prototype.startsWith
     *
     * @param {string} pattern
     * @return {boolean}
     */
    value: function (pattern) {
      'use strict';
      var i = 0,
          len = this.length, pLen = pattern.length;

      if (len < pLen) { return false; }
      for (i = 0; i < pLen; ++ i) {
        if (this[i] !== pattern[i]) { return false; }
      }
      return true;
    }
  });
}
if (! String.prototype.endsWith) {
  Object.defineProperty(String.prototype, 'endsWith', {
    /**
     * ES6 String.prototype.endsWith
     *
     * @param {string} pattern
     * @return {boolean}
     */
    value: function (pattern) {
      'use strict';
      var i = 0,
          len = this.length, pLen = pattern.length;

      if (len < pLen) { return false; }
      for (i = 1; i <= pLen; ++ i) {
        if (this[len - i] !== pattern[pLen - i]) { return false; }
      }
      return true;
    }
  });
}

template要素

requestAnimationFrame と template要素のpolyfillも載せておく。非同期的 (async) life game (JavaScript)で使った。

if (! window.requestAnimationFrame) {
  if (window.mozRequestAnimationFrame) {
    window.requestAnimationFrame = window.mozRequestAnimationFrame;
  }
}

// http://c4se.hatenablog.com/entry/2013/09/28/004018
if (! ('content' in document.createElement('template'))) {
  (function () {
    'use strict';
    var style = document.createElement('style');

    style.textContent = 'template { display: none; }';
    document.body.appendChild(style);
  }());
  Object.defineProperty(HTMLUnknownElement.prototype, 'content', {
    /**
     * @return {DocumentFragment|undefined}
     */
    get: function() {
      'use strict';
      var fragment;

      if (this.tagName !== 'TEMPLATE') { return; }
      fragment = document.createDocumentFragment();
      [].slice.call(this.childNodes).forEach(function (node) {
        fragment.appendChild(node.cloneNode(true));
      });
      return fragment;
    }
  });
}