.。oO(お引越ししましたヾ(〃l _ l)ノ゙)
2009-08-19 (Wed)
Enumerable.js
みょんなものを書いたので、とりあえず。
ところどころ動作がおかしいけど、たぶん直します。
/*============================================================ * Enumerable 20090819 * c4se project - http://c4se.sakura.ne.jp * * project name: c4seJS * author: ne_Schirou - utakata.c4se@gmail.com * <c4seFCL 0.2> ============================================================*/ (function(){ /* * == usage * * Klass = function(){ * // codes * }; * Object.include(Klass.prototype, Enumerable.prototype); * Klass.prototype = { * forEach: function(f, o){ * // codes * } * }; * * * == cf. * * Array.prototype.forEach = function(f, o){ * var i = 0, * l = this.length; * for(; i<l; ++i){ * if(this[i] === undefined) continue; * f.call(o, this[i], i, this); * } * return this; * }; */ var _compare = function(l, r){ // default comparison function (by String charctor code) var i = 0, len = l.length; l = l.toString().split(''); r = l.toString().split(''); for(; i<len; ++i){ if(l[i].charCodeAt(0) < r[i].charCodeAt(0)) return -1; if(l[i].charCodeAt(0) > r[i].charCodeAt(0)) return 1; } return 0; }; Enumerable = function(){return this;}; Enumerable.prototype = { //_className: 'Enumerable', //forEach: forEach, every: function(f, o){ var tf = true; this.forEach(function(elm, i, _this){ if(elm === undefined) return null; if(!f.call(o, elm, i, _this)) tf = false; }, this); return tf; }, filter: function(f, o){ var a = new Array(); this.forEach(function(elm, i, _this){ if(elm === undefined) return null; if(f.call(o, elm, i, _this)) a.push(elm); }, this); return a; }, indexOf: function(v, n){ return this.toArray().indexOf(v, n); }, lastIndexOf: function(v, n){ return this.toArray().lastIndexOf(v, n); }, map: function(f, o){ a = new Array(); this.forEach(function(elm, i, _this){ if(elm === undefined) return null; a[i] = f.call(o, elm, i, _this); }, this); return a; }, reduce: function(f, v){ return this.toArray().reduce(f, v); }, reduceRight: function(f, v){ return this.toArray().reduceRight(f, v); }, some: function(f, o){ var tf = false; this.forEach(function(elm, i, _this){ if(f.call(o, elm, i, _this)) tf = true; }, this); return tf; }, sort: function(f){ f = f || _compare; return this.toArray().sort(f); }, toArray: function(){ var a = new Array(); this.forEach(function(elm){ a.push(elm); }, this); return a; }, // Extention like Ruby allQ: every, anyQ: some, car: function(){return this.toArray()[0];}, cdr: function(){return this.toArray().slice(1);}, collect: map, detect: function(f, o){ return this.filter(f, o)[0]; }, each: forEach, each_with_index: forEach, entries: toArray, find: detect, find_all: filter, grep: function(pattern, f, o){ //f = f || function(){return arguments[0];}; var a = new Array(); this.forEach(function(elm){ if(elm === undefined) return null; if(pattern === elm){ if(!f) a.push(elm); else a.push(f(elm)); } }); return a; }, includeQ: function(v){ var tf = false; this.forEach(function(elm){ if(elm === undefined) return null; if(elm == v) tf = true; }, this); return tf; }, inject: reduce, max: function(f){ f = f || _compare; var v, i = 0; this.forEach(function(elm){ if(i == 0){ v = elm; ++i; } else{ if(f(v, elm) < 0) v = elm; } }); return v; }, memberQ: includeQ, min: function(f){ f = f || _compare; var v, i = 0; this.forEach(function(elm){ if(i == 0){ v = elm; ++i; } else{ if(f(v, elm) > 0) v = elm; } }); return v; }, partition: function(f){ var at = new Array(), af = new Array(); this.forEach(function(elm, i, _this){ if(elm === undefined) return null; if(f.call(o, elm, i, _this)) at.push(elm); else af.push(elm); }, this); return [at, af]; }, reject: function(f){ var a = new Array(); this.forEach(function(elm, i, _this){ if(elm === undefined) return null; if(!f.call(o, elm, i, _this)) a.push(elm); }, this); return a; }, select: filter, sort_by: function(f){ return this.toArray().sort(function(l, r){ return f(l) - f(r); }); /*var a = this.map(f); return a.sort(function(l, r){return l - r;});*/ }, zip: function(/**/){ var f, arglen = arguments.length; if(arguments[arglen]._className == 'Function'){ f = arguments[arglen]; --arglen; } var a = new Array(); this.forEach(function(elm, i){ if(elm === undefined) return null; var b = [elm], j = 0; for(; j<arglen; ++j) b[j+1] = arguments[j][i]; a.push(b); }); if(!f) return a; var i = 0, v; while(v = a[i]) f(a[i++]); return null; } }; })();
トラックバック - http://d.hatena.ne.jp/Kureduki_Maari/20090819/1250671227
リンク元
- 4 http://www.google.co.jp/search?sourceid=navclient&hl=ja&ie=UTF-8&rlz=1T4ADBF_ja___JP327&q=alipro+mp3
- 4 http://www.pythagoras.bz/index.php/default/detail?url=http://d.hatena.ne.jp/Kureduki_Maari/
- 2 http://blog.search.goo.ne.jp/search_goo/result/?MT=,&mod=&dc=25&st=time&from=&ts=all
- 2 http://www.bing.com/search?srch=105&FORM=IE7RE&q=dilettante+mp3+ALI+PROJECT
- 2 http://www.google.co.jp/search?hl=ja&client=firefox-a&rls=org.mozilla:ja:official&q=浮動小数点+javascript&btnG=検索&lr=lang_ja
- 2 http://www.google.co.jp/search?hl=ja&lr=&safe=off&q=duvet+歌詞&revid=503659512&ei=Hv-MSs30A8-ZkQXcweWcDA&sa=X&oi=revisions_inline&resnum=0&ct=top-revision&cd=1
- 2 http://www.google.co.jp/search?hl=ja&lr=lang_ja&client=firefox-a&rls=org.mozilla:ja:official&hs=qt4&q=duvet+歌詞&revid=1369928356&ei=TR-NSsmhNs-NkQWc5tyuDA&sa=X&oi=revisions_inline&resnum=0&ct=top-revision&cd=1
- 2 http://www.google.co.jp/search?hl=ja&q=ali+project+mp3&lr=&aq=4&oq=ALI+PROJECT
- 2 http://www.google.com/search?hl=ja&lr=&rls=com.microsoft:ja:IE-SearchBox&rlz=1I7ADBR&q=ALI+PROJECT+mp3+無料&revid=644491318&ei=E62MSuvpGNKfkQW9rNgt&sa=X&oi=revisions_inline&resnum=0&ct=top-revision&cd=2
- 1 http://b.hatena.ne.jp/entry/d.hatena.ne.jp/Kureduki_Maari/20090114/1231916164