Pot.some

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

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

引数 object のすべての要素について
関数 callback によって実行した結果が 真 (true) となる要素があるかどうかを調べます。
あれば true を返します。

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

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

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

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

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

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

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

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

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

object のすべての要素について、関数 callback によって実行した結果が 真 (true) となる要素があれば true を返します。
それ以外は false を返します。

Pot.some で同期処理の例:

// 配列に対しての例

function isBigEnough(value, index, array) {
    return (value >= 10);
}

var result1 = Pot.some([2, 5, 8, 1, 4], isBigEnough);
debug(result1); // false

var result2 = Pot.some([12, 5, 8, 1, 4], isBigEnough);
debug(result2); // true
// オブジェクトに対しての例

function isBigEnough(value, key, object) {
    return (value >= 10);
}

var result1 = Pot.some({a: 5, b: 1, c: 6}, isBigEnough);
debug(result1); // false

var result2 = Pot.some({a: 102, b: 51, c: 15}, isBigEnough);
debug(result2); // true

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

// 配列に対しての例

function isBigEnough(value, index, array) {
    return (value >= 10);
}

Pot.Deferred.some([2, 5, 8, 1, 4], isBigEnough).then(function(result) {
    debug(result); // false
});

Pot.Deferred.some([12, 5, 8, 1, 4], isBigEnough).then(function(result) {
    debug(result); // true
});
// オブジェクトに対しての例

function isBigEnough(value, key, object) {
    return (value >= 10);
}

Pot.Deferred.some({a: 5, b: 1, c: 6}, isBigEnough).then(function(result) {
    debug(result); // false
});

Pot.Deferred.some({a: 102, b: 51, c: 15}, isBigEnough).then(function(result) {
    debug(result); // true
});

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

// 配列に対しての例

function isBigEnough(value, index, array) {
    return (value >= 10);
}

var d = new Pot.Deferred();

d.then(function() {
    return [2, 5, 8, 1, 4];
}).some(isBigEnough).then(function(result) {
    debug(result); // false
}).then(function() {
    return [12, 5, 8, 1, 4];
}).some(isBigEnough).then(function(result) {
    debug(result); // true
}).begin();
// オブジェクトに対しての例

function isBigEnough(value, key, object) {
    return (value >= 10);
}

Pot.Deferred.begin(function() {
    return {a: 5, b: 1, c: 6};
}).some(isBigEnough).then(function(result) {
    debug(result); // false
}).then(function() {
    return {a: 102, b: 51, c: 15};
}).some(isBigEnough).then(function(result) {
    debug(result); // true
});