
hi all, since c++11 has lambda functions, writing functors is easier than never before ... so, i wonder, what do people think about a small utility function, that calls a functor N times? ruby: 10.times { puts 'hello world' } supercollider: 10.do { "hello world".postln } c++11/boost proposal: boost::loop(10, [] { printf("hello world\n"); }); boost::loop(10, [](int index) { printf("hello %d\n, index); }); personally, i find this coding style more elegant than for loops, as it avoids the visual noise of explicitly counting a loop variable ... not sure, if it is worth a separate library, maybe it could just be added to boost/utility ... thoughts? prototype available at github: https://github.com/timblechmann/boost_loop tim

On 01/31/2013 10:31 AM, Tim Blechmann wrote:
hi all,
since c++11 has lambda functions, writing functors is easier than never before ... so, i wonder, what do people think about a small utility function, that calls a functor N times?
ruby: 10.times { puts 'hello world' }
supercollider: 10.do { "hello world".postln }
c++11/boost proposal: boost::loop(10, [] { printf("hello world\n"); }); boost::loop(10, [](int index) { printf("hello %d\n, index); });
personally, i find this coding style more elegant than for loops, as it avoids the visual noise of explicitly counting a loop variable ... not sure, if it is worth a separate library, maybe it could just be added to boost/utility ...
thoughts? This can already be done using the boost range library:
========8<========8<========8<========8<========8<========8<======== #include <boost/range/irange.hpp> #include <boost/range/algorithm/for_each.hpp> int main() { boost::for_each(boost::irange(0, 10), [] (int) { std::cout << "hello world\n";}); boost::for_each(boost::irange(0, 10), [] (int index) { std::cout << "hello world from " << index << "\n";}); } ========8<========8<========8<========8<========8<========8<========
prototype available at github: https://github.com/timblechmann/boost_loop
tim
_______________________________________________ Unsubscribe& other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

On Thu, Jan 31, 2013 at 1:58 PM, Thomas Heller <thom.heller@gmail.com> wrote:
On 01/31/2013 10:31 AM, Tim Blechmann wrote:
hi all,
since c++11 has lambda functions, writing functors is easier than never before ... so, i wonder, what do people think about a small utility function, that calls a functor N times?
ruby: 10.times { puts 'hello world' }
supercollider: 10.do { "hello world".postln }
c++11/boost proposal: boost::loop(10, [] { printf("hello world\n"); }); boost::loop(10, [](int index) { printf("hello %d\n, index); });
personally, i find this coding style more elegant than for loops, as it avoids the visual noise of explicitly counting a loop variable ... not sure, if it is worth a separate library, maybe it could just be added to boost/utility ...
thoughts?
This can already be done using the boost range library:
========8<========8<========8<========8<========8<========8<======== #include <boost/range/irange.hpp> #include <boost/range/algorithm/for_each.hpp>
int main() { boost::for_each(boost::irange(0, 10), [] (int) { std::cout << "hello world\n";}); boost::for_each(boost::irange(0, 10), [] (int index) { std::cout << "hello world from " << index << "\n";}); } ========8<========8<========8<========8<========8<========8<========
IMHO, a regular loop looks simpler than either of these variants: for (int i = 0; i < 10; ++i) std::cout << "hello world from " << i << "\n"; also, I guess this should also work: for (auto i: boost::irange(0, 10)) std::cout << "hello world from " << i << "\n"; We could add a new range generator to simplify this code even further and remove the lower bound: for (auto i: boost::times(10)) std::cout << "hello world from " << i << "\n"; where times(n) is equivalent to irange(0, n).

hi thomas & andrey,
int main() { boost::for_each(boost::irange(0, 10), [] (int) { std::cout << "hello world\n";}); boost::for_each(boost::irange(0, 10), [] (int index) { std::cout << "hello world from " << index << "\n";}); } ========8<========8<========8<========8<========8<========8<======== IMHO, a regular loop looks simpler than either of these variants:
for (int i = 0; i < 10; ++i) std::cout << "hello world from " << i << "\n";
also, I guess this should also work:
for (auto i: boost::irange(0, 10)) std::cout << "hello world from " << i << "\n";
of course, these variants all work, though they have the visual noise, that i was referring to: a: boost::for_each(boost::irange(0, X), b: for (int i = 0; i < X; ++i) c: for (auto i: boost::irange(0, X)) d: for (auto i: boost::times(X)) e: boost::loop (X, or if one uses namespace boost: a: for_each(irange(0, X), b: for (int i = 0; i < X; ++i) c: for (auto i: irange(0, X)) d: for (auto i: times(X)) e: loop (X, in both cases my proposal is the most compact one, though still not as compact as "X.do". using a lot of higher-level languages these days, writing loops in c++ just feels too verbose for this century ... range-based for loops avoid the manual handling of iterators ... iac, if we want to do something N times, we just want to loop without thinking about integer ranges or incrementing integers ;) cheers, tim

On 01/31/2013 12:16 PM, Tim Blechmann wrote:
hi thomas& andrey,
int main() { boost::for_each(boost::irange(0, 10), [] (int) { std::cout<< "hello world\n";}); boost::for_each(boost::irange(0, 10), [] (int index) { std::cout<< "hello world from "<< index<< "\n";}); } ========8<========8<========8<========8<========8<========8<======== IMHO, a regular loop looks simpler than either of these variants:
for (int i = 0; i< 10; ++i) std::cout<< "hello world from "<< i<< "\n";
also, I guess this should also work:
for (auto i: boost::irange(0, 10)) std::cout<< "hello world from "<< i<< "\n"; of course, these variants all work, though they have the visual noise, that i was referring to:
a: boost::for_each(boost::irange(0, X), b: for (int i = 0; i< X; ++i) c: for (auto i: boost::irange(0, X)) d: for (auto i: boost::times(X)) e: boost::loop (X,
or if one uses namespace boost:
a: for_each(irange(0, X), b: for (int i = 0; i< X; ++i) c: for (auto i: irange(0, X)) d: for (auto i: times(X)) e: loop (X,
in both cases my proposal is the most compact one, though still not as compact as "X.do". using a lot of higher-level languages these days, writing loops in c++ just feels too verbose for this century ... range-based for loops avoid the manual handling of iterators ...
iac, if we want to do something N times, we just want to loop without thinking about integer ranges or incrementing integers ;) And this is exactly what Boost.Range does ;) Honestly, I don't see the added advantage the proposed boost::loop gives since for_each is a more or less standardized name for a function iterating over some range, where boost.range gives you the facilities to loop over a range of integers. Also my visual preferences don't see the extra few characters you need to type as visual noise. True it adds verbosity, however, the added verbosity highlights the intend (this is also true for the range based for loops in C++11 as Andrey proposed). Additionally, the boost range solution gives you the flexibility to use any range (from adapting the start of the range to specifying the step).
cheers, tim
_______________________________________________ Unsubscribe& other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

hi thomas,
iac, if we want to do something N times, we just want to loop without thinking about integer ranges or incrementing integers ;) And this is exactly what Boost.Range does ;) Honestly, I don't see the added advantage the proposed boost::loop gives since for_each is a more or less standardized name for a function iterating over some range, where boost.range gives you the facilities to loop over a range of integers. Also my visual preferences don't see the extra few characters you need to type as visual noise. True it adds verbosity, however, the added verbosity highlights the intend (this is also true for the range based for loops in C++11 as Andrey proposed). Additionally, the boost range solution gives you the flexibility to use any range (from adapting the start of the range to specifying the step).
i think there are different intentions. the question is: what do you want to express? * iterate over a range of integers * do something N times boost.range is wonderful to express the first case! my proposal tries to add expressive power for the second ... again comparing this to ruby: (0..9).each {|i| print i, "\n"} 10.times {|i| print i, "\n"} or supercollider: (0..9).do (_.postln) 10.do (_.postln) one does not need to know these languages to see that the developer wants to express different things ;) cheers, tim

2013/1/31 Tim Blechmann <tim@klingt.org>
hi all,
since c++11 has lambda functions, writing functors is easier than never before ... so, i wonder, what do people think about a small utility function, that calls a functor N times?
ruby: 10.times { puts 'hello world' }
supercollider: 10.do { "hello world".postln }
c++11/boost proposal: boost::loop(10, [] { printf("hello world\n"); }); boost::loop(10, [](int index) { printf("hello %d\n, index); });
personally, i find this coding style more elegant than for loops, as it avoids the visual noise of explicitly counting a loop variable ... not sure, if it is worth a separate library, maybe it could just be added to boost/utility ...
thoughts?
Then how about some class like "looper" that takes a function to run. e.g. looper x10(10); x10([] { printf("hello world\n"); }); x10([](int index) { printf("hello %d\n, index); }); Or with user-defined literals: 10_times([] { printf("hello world\n"); });
participants (4)
-
Andrey Semashev
-
Thomas Heller
-
Tim Blechmann
-
TONGARI