Skip to content

Instantly share code, notes, and snippets.

@moleike
Last active September 15, 2015 07:13
Show Gist options
  • Save moleike/aebea45ff3ef1facbe3e to your computer and use it in GitHub Desktop.
Save moleike/aebea45ff3ef1facbe3e to your computer and use it in GitHub Desktop.
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