4 ms·
First example doesn't look bad in C++23: auto get_ids(std::span<const Widget> data) { return data | filter(&Widget::alive)
by 0xf00ff00f 1y ago
First example doesn't look bad in C++23:
auto get_ids(std::span<const Widget> data)
{
return data
| filter(&Widget::alive)
| transform(&Widget::id)
| to<std::vector>();
}
- inetknght 1y agoThis is not functionally different from operator<< which std::cout has taught us is a neat trick but generally a bad idea.
- senderista 1y agoUnlike the iostreams shift operators, the ranges pipe operator isn't stateful.
- jjmarr 1y agoThere's state when you try to use the final result, though. It's not threadsafe due to caching. https://www.youtube.com/watch?v=c1gfbbE2zts https://www.youtube.com/watch?v=c1gfbbE2zts
- Shorel 1y agoThis looks awesome! I'm really want to start playing with some C++23 in the future.
- 0xf00ff00f 1y agoI cheated a bit, I omitted the namespaces. Here's a working version: https://godbolt.org/z/1rE9o3Y95 https://godbolt.org/z/1rE9o3Y95
- uzerfcwn 1y agoTo me, the cool (and uncommon in other languages' standard libraries) part about C++ ranges is that they reify pipelines so that you can cut and paste them into variables, like so: auto get_ids(std::span<const Widget> data) { auto pipeline = filter(&Widget::alive) | transform(&Widget::id); auto sink = to<std::vector>(); return data | pipeline | sink; }