| Version 10 (modified by cho45, 5 years ago) |
|---|
JSEnumerator
A light-weight library for list or enumeration.
Download
- http://svn.coderepos.org/share/lang/javascript/jsenumerator/trunk/jsenumerator.js
- No comments: http://svn.coderepos.org/share/lang/javascript/jsenumerator/trunk/jsenumerator.nodoc.js
- Compressed: http://svn.coderepos.org/share/lang/javascript/jsenumerator/trunk/jsenumerator.mini.js
svn co http://svn.coderepos.org/share/lang/javascript/jsenumerator/trunk jsenumerator
Documentation
See source.
Test
http://svn.coderepos.org/share/lang/javascript/jsenumerator/trunk/test.html
Benchmark
http://svn.coderepos.org/share/lang/javascript/jsenumerator/trunk/benchmark/benchmark.html
- If you are nervous for speed, you should always use for loop :)
License
MIT. See header of http://svn.coderepos.org/share/lang/javascript/jsenumerator/trunk/jsenumerator.js
Concept
- Do not taint global like jQuery.
- Infinite list functions like MochiKit.
- Enumeration method chain like Ruby 1.9.
Using method chain for generating sequence has advantages over nesting function call for writing as you think.
Sample
// normal
fizzbuzz = E(1).countup().imap(function (i) {
return (i % 3 == 0) ? (i % 5 == 0) ? "FizzBuzz" : "Fizz" :
(i % 5 == 0) ? "Buzz" : i ;
}).take(20);
// take some values from pre-defined sequence
fizzbuzz = E(1)
.countup()
.izip(
E(["", "", "Fizz"]).cycle(),
E(["", "", "", "", "Buzz"]).cycle())
.imap(function (num, fizz, buzz) {
return fizz + buzz || num;
})
.take(20);
with JSDeferred
function fib () {
var p = 0, i = 1;
return E([0, 1]).chain(E(function () {
var ret = p + i;
p = i;
i = ret;
return ret;
}));
}
Enumerator.prototype.dloop = function (fun, n) {
var enum = this;
if (!n) n = 1;
return next(function () {
for (var i = 0; i < n; i++) {
fun.call(enum, enum.next());
}
return call(arguments.callee);
}).error(function (e) {
if (e != Enumerator.StopIteration) throw e;
});
};
fib().itake(100).dloop(function (i) {
log(i);
});
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)