4 ms·
While we are at it Python: >>> sum(x*x for x in xrange(1,11)) 385 You could use a map in Python as well, but really, the generator expression is much
by timtadh 12y ago
While we are at it Python:
>>> sum(x*x for x in xrange(1,11))
385
You could use a map in Python as well, but really, the generator expression is much cleaner:
>>> sum(map(lambda x: x*x, xrange(1,11)))
385
- deleted 12y ago[deleted]
- timtadh 12y agoOops, my bad. I didn't read the original closely enough.
- jlarocco 12y agoThose compute the same value, but I think the point of the C++ stream version is to compute the value using a lazily evaluated infinite list. The C++ equivalent of your Python would be something like: typedef boost::counting_iterator<int> ci; auto sum = std::accumulate(ci(0), ci(10), 0, [](auto x, auto y) { return x + (y*y); }); No special streaming library required.
- father_of_two 12y agoA solution in the spirit of the post using itertools: >>> from itertools import islice,count >>> sum(x*x for x in islice(count(1),10)) 385