3 ms·
Beautiful Folding (2008)
- rikkus 12y agoC# purely functional: Enumerable.Range(1, 10000000).Aggregate( new { Sum = 0L, Length = 0L }, (agg, n) => new { Sum = agg.Sum + n, Length = agg.Length + 1 } ) C# side-effecting (executes in about 1/3 of the time): class Agg { public long Sum; public long Count; } Enumerable.Range(1, 10000000).Aggregate( new Agg(), (a, n) => { a.Sum += n; ++a.Count; return a; } );