Get the latest tech news

`std::flip`


std::flip is a little-known utility from the C++ standard library header <functional>: it is a higher-order function that accepts a Callable and returns an equivalent Callable with the order of its parameters reversed (or “flipped”).

Interestingly enough, most of these implementations only flip the first two parameters of whichever function they are passed, though it seems to be because most of them are based on the Haskell prelude, and handling arbitrary arity can be tricky in that language. Perhaps the simplest example of what std::flip brings to the table is the classic problem of sorting a collection in descending order according to a predicate: The following snippet of code uses boost::algorithm::longest_increasing_subsequence to compute the longest increasing subsequence, a feature of Boost.Algorithm that only exists in a seemingly abandoned pull request, then builds other flavours of the algorithm on top of it:

Get the Android app

Or read this on Hacker News

Read more on:

Photo of std::flip

std::flip