4 ms·
For JavaScript using Stream.js https://github.com/winterbe/streamjs https://github.com/winterbe/streamjs var total = Stream .iterate(0, function (n)
by winterbe 12y ago
For JavaScript using Stream.js https://github.com/winterbe/streamjs https://github.com/winterbe/streamjs
var total = Stream
.iterate(0, function (n) {
return n + 1;
})
.map(function (x) {
return x * x;
})
.limit(10)
.sum();
- rikkus 12y agoLazy.js, with ES6 arrow functions: var Lazy = require('lazy.js'); var total = Lazy .generate(i => i + 1) .map(i => i * i) .take(10) .sum(); C#, with LINQ: IEnumerable<int> NaturalNumbers() { var i = 1; while (true) yield return i++; } var total = NaturalNumbers().Select(n => n * n).Take(10).Sum(); As, of course, int has a maximum, it would be more sensible to use: Enumerable.Range(1, int.MaxValue).Select(n => n * n).Take(10).Sum(); And if you already know that you want 10, as in the above, you'd likely do: Enumerable.Range(1, 10).Select(n => n * n).Sum();