Years ago I wrote c++ library for stream compostion. Something like C++20 ranges.
It turns out that as long as you compose everything with lambdas, compiled code is same as it would be with naive loops. Everything gets optimised.
For example, you can write sum of numbers less than n as:
For example, you can write sum of numbers less than n as:
Clang converted this into n*(n-1)/2.