4 ms·
Here's Rust: let total = (1..).map(|x| x*x).take(10).fold(0, |acc, x| acc+x); You could replace the fold with ".sum()" if you use the unstable std::iter::
by CUViper 12y ago
Here's Rust:
let total = (1..).map(|x| x*x).take(10).fold(0, |acc, x| acc+x);
You could replace the fold with ".sum()" if you use the unstable std::iter::AdditiveIterator. Or you could incorporate the squaring map into the fold, but then we're no longer comparing the same thing.
- jonreem 12y agoYou can also replace the closure in the fold with Add::add, where Add is std::ops::Add: let total = (1..).map(|x| x * x).take(10).fold(0, Add::add); This is an application of UFCS, which allows you to call methods as regular functions.