3 ms·
Lazy.js, with ES6 arrow functions: var Lazy = require('lazy.js'); var total = Lazy .generate(i => i + 1) .map(i => i * i) .take(10) .sum()
by rikkus 12y ago
Lazy.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();