List Comprehensions in C++ 20 Aug, 2018 List Comprehensions in C++

From Bartosz Milewski's What Does Haskell Have to Do with C++?: Haskell style list comprehensions of the form

one x = 1
count lst = sum [one x | x <- lst]

can be written in modern C++ like so:

template<class T> struct
one {
    static const int value = 1;

template<class... lst> struct
count {
    static const int value = sum<one<lst>::value...>::value;

The author calls out the comparison between one<lst>::value... and [one x | x <- lst]. I've used packs like this before, but looking at this juxtaposition was a revelation.

Tags  ·   Functional Programming  ·   C++  ·   TMP  ·   Template Meta Programming  ·   Show Comments ▾

Original design for Tumblr crafted by Prashanth Kamalakanthan.
Adapted for Tumblr & Jekyll by Sai Charan. Customized theme available on Github.

Sai Charan's blog by Sai Charan is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Creative Commons License