#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
template < typename Container, typename F>
ContainerOut Map( const F& f, const ContainerIn& xs )
{
ContainerOut ys;
auto it = std:: inserter ( ys, end( ys ) ) ;
std:: transform ( begin( xs ) , end( xs ) , it, f ) ;
return ys;
}
template < typename Ret, typename Arg1, typename ... Args >
auto Curry( Ret ( * f) ( Arg1, Args...) , Arg1 arg ) - > std:: function < Ret( Args...) >
{
return [ = ] ( Args... args ) { return f( arg, args... ) ; } ;
}
int main( )
{
auto square = [ ] ( int x) { return x* x; } ;
typedef std:: vector < int > Row;
Row row = { 1 , 2 , 3 } ;
Row squaredRow = Map( square, row) ;
typedef std:: vector < Row> Mat;
Mat mat = { { 1 ,2 } ,{ 2 ,3 ,4 } } ;
auto squareElements = Curry( Map< Row, decltype( square) > , square) ;
Mat squaredMat = Map( squareElements, mat) ;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KI2luY2x1ZGUgPGFsZ29yaXRobT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdGVtcGxhdGUgPHR5cGVuYW1lIENvbnRhaW5lciwgdHlwZW5hbWUgRj4KQ29udGFpbmVyT3V0IE1hcCggY29uc3QgRiYgZiwgY29uc3QgQ29udGFpbmVySW4mIHhzICkKewogICAgQ29udGFpbmVyT3V0IHlzOwogICAgYXV0byBpdCA9IHN0ZDo6aW5zZXJ0ZXIoIHlzLCBlbmQoIHlzICkgKTsKICAgIHN0ZDo6dHJhbnNmb3JtKCBiZWdpbiggeHMgKSwgZW5kKCB4cyApLCBpdCwgZiApOwogICAgcmV0dXJuIHlzOwp9CnRlbXBsYXRlIDx0eXBlbmFtZSBSZXQsIHR5cGVuYW1lIEFyZzEsIHR5cGVuYW1lLi4uIEFyZ3M+CiAgYXV0byBDdXJyeShSZXQgKCpmKShBcmcxLCBBcmdzLi4uKSwgIEFyZzEgYXJnICkgLT4gc3RkOjpmdW5jdGlvbjxSZXQoQXJncy4uLik+CnsKICAgIHJldHVybiBbPV0oIEFyZ3MuLi4gYXJncyApIHsgcmV0dXJuIGYoIGFyZywgYXJncy4uLiApOyB9Owp9CmludCBtYWluKCkgCnsKCWF1dG8gc3F1YXJlID0gW10oaW50IHgpeyByZXR1cm4geCp4OyB9OwoJCiAgdHlwZWRlZiBzdGQ6OnZlY3RvcjxpbnQ+IFJvdzsKICBSb3cgcm93ID0gezEsIDIgLCAzfTsKICBSb3cgc3F1YXJlZFJvdyA9IE1hcChzcXVhcmUsIHJvdyk7CgogIHR5cGVkZWYgc3RkOjp2ZWN0b3I8Um93PiBNYXQ7CiAgTWF0IG1hdCA9IHt7MSwyfSx7MiwzLDR9fTsKICBhdXRvIHNxdWFyZUVsZW1lbnRzID0gQ3VycnkoTWFwPFJvdywgZGVjbHR5cGUoc3F1YXJlKT4sIHNxdWFyZSk7CiAgTWF0IHNxdWFyZWRNYXQgPSBNYXAoc3F1YXJlRWxlbWVudHMsIG1hdCk7CgkKCXJldHVybiAwOwp9
compilation info
prog.cpp:7:1: error: 'ContainerOut' does not name a type
ContainerOut Map( const F& f, const ContainerIn& xs )
^
prog.cpp: In function 'int main()':
prog.cpp:25:35: error: 'Map' was not declared in this scope
Row squaredRow = Map(square, row);
^
prog.cpp:29:38: error: expected primary-expression before ',' token
auto squareElements = Curry(Map<Row, decltype(square)>, square);
^
prog.cpp:29:40: error: expected primary-expression before 'decltype'
auto squareElements = Curry(Map<Row, decltype(square)>, square);
^
stdout