Pot.reduce

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

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

引数 object の (左から右へ) 2 つの値に対して
同時に関数 callback を適用し、単一の値にして返します。

処理は、Array.prototype.reduce と同じように実行されます。
引数 object には、対象の配列またはオブジェクトを指定します。
引数 callback は、実行する関数を指定します。
引数 initial を指定すると、callback の最初の呼び出しのときに、最初の実引数として用いるための値として使われます。
引数 context が指定されると、callback 関数の this が context にとって代わります。

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

function (leftValue, rightValue, key, object) { this === context }

    leftValue  : {*}              object の各要素のうち 左 (前) の値
    rightValue : {*}              object の各要素のうち 右 (今) の値
    key        : {String|Number|*}  object の各キー
    object     : {Object|Array}     object 自身

initial を指定すると、最初のイテレートの時の leftValue として使用されます。
省略すると、object の最初の値が使用されます。

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

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

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

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

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

object の (左から右へ) 2 つの値に対して 同時に関数 callback を適用した結果の 単一の値を返します。

Pot.reduce で同期処理の例:

// 配列に対しての例

var arr = [1, 2, 3, 4, 5];

var total = Pot.reduce(arr, function(leftValue, rightValue, i, array) {
    return leftValue + rightValue;
});
debug(total); // 15
// initial を指定した例

var arr = [0, 1, 2, 3, 4];

var result = Pot.reduce(arr, function(leftValue, rightValue, i, array) {
    return leftValue + rightValue;
}, 10);
debug(result); // 20
// オブジェクトに対しての例

var obj = {a: 1, b: 2, c: 3};

var total = Pot.reduce(obj, function(leftValue, rightValue, key, object) {
    return leftValue + rightValue;
});
debug(total); // 6

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

// 配列に対しての例

var arr = [1, 2, 3, 4, 5];

Pot.Deferred.reduce(arr, function(leftValue, rightValue, i, array) {
    return leftValue + rightValue;
}).then(function(result) {
    debug(result); // 15
});
// オブジェクトに対しての例

var obj = {a: 1, b: 2, c: 3};

Pot.Deferred.reduce(obj, function(leftValue, rightValue, key, object) {
    return leftValue + rightValue;
}).then(function(result) {
    debug(result); // 6
});

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

// 配列に対しての例

var arr = [1, 2, 3, 4, 5];

var d = new Pot.Deferred();

d.reduce(function(leftValue, rightValue, i, array) {
    return leftValue + rightValue;
}).then(function(result) {
    debug(result); // 15
}).begin(arr);
// オブジェクトに対しての例

var obj = {a: 1, b: 2, c: 3};

var d = new Pot.Deferred();

d.then(function() {
    return obj;
}).reduce(function(leftValue, rightValue, key, object) {
    return leftValue + rightValue;
}).then(function(result) {
    debug(result); // 6
}).begin();
// オブジェクトに対しての例 (2)

var obj = {a: 1, b: 2, c: 3};

Pot.Deferred.begin(function() {
    return obj;
}).reduce(function(leftValue, rightValue, key, object) {
    return leftValue + rightValue;
}).then(function(result) {
    debug(result); // 6
});