Skip to content

Instantly share code, notes, and snippets.

@moleike moleike/list.cc

Last active Sep 15, 2015
Embed
What would you like to do?
Linked list using polymorphic lambdas
// http://lists.boost.org/Archives/boost/2014/06/214213.php
// http://stackoverflow.com/questions/25338795/is-there-a-name-for-this-tuple-creation-idiom
#include <iostream>
#include <string>
#include <functional>
auto list = [](auto ...xs) {
return [=](auto access) { return access(xs...); };
};
auto head = [](auto xs) {
return xs([](auto first, auto ...rest) { return first; });
};
auto tail = [](auto xs) {
return xs([](auto first, auto ...rest) { return list(rest...); });
};
auto length = [](auto xs) {
return xs([](auto ...z) { return sizeof...(z); });
};
int main()
{
auto l = list(1, '2', "3");
auto three = length(l);
std::cout << three;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.