Concatenate lambdas with recursion
You can stack lambdas so that the output of one is the input of the next, using a simple recursive function. This creates a simple way to build one function upon another.
How to do it…
This is a short and simple recipe that uses one recursive function to do most of the work:
- We'll start by defining the concatenation function
concat()
:template <typename T, typename ...Ts> auto concat(T t, Ts ...ts) { if constexpr (sizeof...(ts) > 0) { return [&](auto ...parameters) { return t(concat(ts...)(parameters...)); }; } else { return t; } }
This function returns an anonymous lambda, which in turn calls the function again...