Pot.map

{Object|Array|*} Pot.map ({Object|Array|*} object, {Function} callback [, {Object|*} context])
{Pot.Deferred} Pot.Deferred.map ({Object|Array|*} object, {Function} callback [, {Object|*} context])
{Pot.Deferred} Pot.Deferred.prototype.map ({Function} callback [, {Object|*} context])

Pot.js と PotLite.js で利用可能。

与えられた関数 callback を object のすべての要素に対して実行し、
その結果からなる新しいオブジェクト/配列を生成して返します。

処理は、Array.prototype.map と同じように実行されます。
引数 object には、対象の配列またはオブジェクトを指定します。
引数 callback は、実行する関数を指定します。
引数 context が指定されると、callback 関数の this が context にとって代わります。

コールバック関数の引数は以下のようになります。

function (value, key, object) { this === context }

    value  : {*}              object の各値
    key    : {String|Number|*}  object の各キー
    object : {Object|Array}     object 自身

処理は 同期処理、非同期処理、それぞれ異なります。

  • Pot.map は同期処理になります。
    Pot.globalize() 適応済みの場合は map() で実行できます。
  • Pot.Deferred.map は非同期処理になり、Pot.Deferred のインスタンスを返します。
    Pot.globalize() 適応済みの場合は Deferred.map() で実行できます。
  • Pot.Deferred.prototype.map は、Pot.Deferred.prototype.async() によって 同期/非同期 が変化します。
    Pot.Deferred チェイン上で実行でき、Pot.Deferred のインスタンスを返します。

callback 関数の中で StopIteration (Pot.StopIteration) を throw することでループを止められます。

Pot.Deferred.map.slow() のようにして速度の指定ができます。
非同期では、ほとんどの環境で速度指定が可能ですが、 同期での速度指定は一部の環境 (XUL 上など) のみになります。
指定できる名前は以下の表を参照ください。
デフォルトの速度は normal です。

値 / メソッド名 速度
limp 最も遅い
doze 遅い
slow 遅め
normal 通常
fast 速め
rapid 速い
ninja 最も速い

与えられた関数 callback を object のすべての要素に対して実行した結果からなる 新しいオブジェクト/配列 を返します。

Pot.map で同期処理の例:

// 配列の例

var words = ['foot', 'goose', 'moose'];

var result = Pot.map(words, function(value, i, array) {
  return value.replace(/o/g, 'e');
});
debug(result); // ['feet', 'geese', 'meese']
// オブジェクトの例

var obj = {foo: 'foo1', bar: 'bar2', baz: 'baz3'};

var result = Pot.map(obj, function(value, key, object) {
  return value + '00';
});
debug(result); // {foo: 'foo100', bar: 'bar200', baz: 'baz300'}

Pot.Deferred.map で非同期処理の例:

// 配列の例

var words = ['foot', 'goose', 'moose'];

Pot.Deferred.map(words, function(value, i, array) {
    return value.replace(/o/g, 'e');
}).then(function(result) {
    debug(result); // ['feet', 'geese', 'meese']
});
// オブジェクトの例

var obj = {foo: 'foo1', bar: 'bar2', baz: 'baz3'};

Pot.Deferred.map(obj, function(value, key, object) {
    return value + '00';
}).then(function(result) {
    debug(result); // {foo: 'foo100', bar: 'bar200', baz: 'baz300'}
});

Pot.Deferred.prototype.map で非同期処理の例:

// 配列の例

var words = ['foot', 'goose', 'moose'];

var d = new Pot.Deferred();

d.then(function() {
    return words;
}).map(function(value, i, array) {
    return value.replace(/o/g, 'e');
}).then(function(result) {
    debug(result); // ['feet', 'geese', 'meese']
}).begin();
// 配列の例 (2)

var words = ['foot', 'goose', 'moose'];

var d = new Pot.Deferred();

d.map(function(value, i, array) {
    return value.replace(/o/g, 'e');
}).then(function(result) {
    debug(result); // ['feet', 'geese', 'meese']
}).begin(words);
// オブジェクトの例

var obj = {foo: 'foo1', bar: 'bar2', baz: 'baz3'};

Pot.Deferred.begin(function() {
    return obj;
}).map(function(value, key, object) {
    return value + '00';
}).then(function(result) {
    debug(result); // {foo: 'foo100', bar: 'bar200', baz: 'baz300'}
});