Created
November 5, 2018 01:44
-
-
Save takayoshiotake/04b3362f9b48c5b95489d988244a5c3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <functional> | |
| namespace scopefunction { | |
| template <typename T> T apply(T&& value, std::function<void(T&)> block) { | |
| block(value); | |
| return value; | |
| } | |
| template <typename T, typename R> R let(T&& value, std::function<R(T&)> block) { | |
| return block(value); | |
| } | |
| } | |
| #if 0 // e.g. | |
| #include <string> | |
| #include <iostream> | |
| using namespace scopefunction; | |
| int main(void) { | |
| auto test1 = apply<int>(20, [](int& it) { | |
| it *= 100; | |
| }); | |
| std::cout << test1 << std::endl; | |
| auto test2 = let<std::string, int>(std::string("test"), [](std::string& it) -> int { | |
| return it.length(); | |
| }); | |
| std::cout << test2 << std::endl; | |
| return 0; | |
| } | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment