Created
April 12, 2012 03:17
-
-
Save ethertank/2364426 to your computer and use it in GitHub Desktop.
Array .min() / .max()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.min || (Array.prototype.min = function() { | |
return this.length ? Math.min.apply("",this) : undefined; | |
}); | |
/* check result *//* | |
[1, 2, 3, 0].min() ====> 0 | |
typeof[1, 2, 3, 0].min() ====> number | |
[1, 2, 3, +100].min() ====> 1 | |
typeof[1, 2, 3, +100].min() ====> number | |
["1", "2", "3", "+100"].min() ====> 1 | |
typeof["1", "2", "3", "+1"].min() ====> number | |
[].min() + ", " + typeof[].min() ====> undefined | |
typeof[].min() ====> undefined | |
[[1, 2, 3], [4, 5, 6]].min() ====> NaN | |
割と自然な結果。多次元配列はNG(対象外としても良いか?) | |
*/ | |
/* | |
Array.prototype.min || (Array.prototype.min = function() { | |
return this.sort(function(a,b){return a<b;}).pop(); | |
}); | |
*/ | |
/* check result *//* | |
[1, 2, 3, 0].min() ====> 0 | |
typeof[1, 2, 3, 0].min() ====> number | |
[1, 2, 3, +100].min() ====> 1 | |
typeof[1, 2, 3, +100].min() ====> number | |
["1", "2", "3", "+100"].min() ====> +100 | |
typeof["1", "2", "3", "+1"].min() ====> string | |
[].min() + ", " + typeof[].min() ====> undefined | |
typeof[].min() ====> undefined | |
[[1, 2, 3], [4, 5, 6]].min() ====> 1,2,3 | |
「+100」 の 「+」記号をsortの評価対象としてる故、他の数値よりこれが小さいとなる。多次元配列も同様にNG | |
*/ | |
/* | |
Array.prototype.min || (Array.prototype.min = function() { | |
if(!this.length) return; | |
var res = this[0], | |
i = 0, | |
l = this.length; | |
for (; i < l; ++i) { | |
if (this[i] < res) res = this[i]; | |
} | |
return res; | |
}); | |
*/ | |
/* check result *//* | |
[1, 2, 3, 0].min() ====> 0 | |
typeof[1, 2, 3, 0].min() ====> number | |
[1, 2, 3, +100].min() ====> 1 | |
typeof[1, 2, 3, +100].min() ====> number | |
["1", "2", "3", "+100"].min() ====> +100 | |
typeof["1", "2", "3", "+1"].min() ====> string | |
[].min() + ", " + typeof[].min() ====> undefined | |
typeof[].min() ====> undefined | |
[[1, 2, 3], [4, 5, 6]].min() ====> 1,2,3 | |
NG。但しfor文を使用すれば内部で記号付き数文字列、-Infinity 等のチェックが可能 | |
*/ | |
/* | |
Array.prototype.min || (Array.prototype.min = function() { | |
//どの様に在るべきか? | |
}); | |
*/ | |
// checker | |
var arrayEcho = function(a) { | |
for (var i = 0, l = a.length; i < l; ++i) { | |
document.write(a[i] + "<br />"); | |
} | |
}, s = " ====> "; | |
arrayEcho([ | |
"[1, 2, 3, 0].min()".bold() + s + [1, 2, 3, 0].min(), | |
"typeof[1, 2, 3, 0].min()".bold() + s + typeof[1, 2, 3, 0].min(), | |
"[1, 2, 3, +100].min()".bold() + s + [1, 2, 3, +100].min(), | |
"typeof[1, 2, 3, +100].min()".bold() + s + typeof[1, 2, 3, +100].min(), | |
'["1", "2", "3", "+100"].min()'.bold() + s + ["1", "2", "3", "+100"].min(), | |
'typeof["1", "2", "3", "+1"].min()'.bold() + s + typeof["1", "2", "3", "+1"].min(), | |
'[].min() + ", " + typeof[].min()'.bold() + s + [].min(), | |
"typeof[].min()".bold() + s + typeof[].min(), | |
"[[1, 2, 3], [4, 5, 6]].min()".bold() + s + [[1, 2, 3], [4, 5, 6]].min() | |
/*, {a:1, b:2, c:3}.min()*/ | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
「配列が一次元で、且つ数値のみから構成されるとは限らない」 | |
・文字列 | |
・プラス記号を含む数文字列(例 : "+100") | |
・多次元配列 | |
・空の配列 | |
・etc. | |
「返り値の型はどう有るべきか」 | |
・最小値が複数存在するケースを考慮して配列で返す(Suger.js) | |
・配列ではなく要素を返す | |
「レアケースに対する対応」 | |
Suger.js のように最小値の個数を尊重するので無ければ、Negative Infinity が出現した時点で走査を停止し、それを返すことも出来る。 | |
大きな配列で且つ -Infinity が配列内の前方に有る場合は極めて高速になるが、それ以外の場合低速になる。 | |
レアケースに対するケアをどこまでするべきか。 | |
…今の所、min / max はスニペットならともかく、ライブラリ的なものには入れるべきではないという結論に至った。 | |
速度を犠牲にして多種の入力に応える原理の簡単なメソッドを作ったとしても、無駄ではないかと。 | |
あるいは入力を制限したとすると、個々の型に対する説明が必要になり、かえって使い手に無駄な思考が発生するのではないかと。 | |
また今度この辺考える。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment