Tuesday, May 6, 2014

The New C++14 Lambdas

This post isn't directly Haskell related, but should be of interest to C++ programmers who like Haskell.
So I've had a look at the new C++14 Lambdas. I'm still absorbing them, but they definitely are a big improvement over the C++11 version. See this blog post by Sumant Tambe for a first taste.
A clear win here is that std::bind, that gawdawful mess, is a thing of the past. You can assign lambdas to auto variables, you can return them directly -- they are darn near really and truly first class. There's a way to define 'id' and some ways to make it useful. You can compose functions, too. Though you have to define the composition operator yourself, and I suspect that you will get template error hell if you mismatch your types. We will have to see what Clang can do about that.
It doesn't look like the standard itself adds any support for function composition, maps/folds, and such. But it does look like those are all attainable, so I fully expect to see some supporting libraries coming out from the likes of Boost. With luck, they will even be in the Standard Library for C++17 or C++20.
Next up, typeclasses!
Sadly joking about that last one. Concepts Lite seems to have been put off until at least C++17.

No comments:

Post a Comment