Skip to content

Instantly share code, notes, and snippets.

@takayoshiotake
Created November 5, 2018 01:44
Show Gist options
  • Select an option

  • Save takayoshiotake/04b3362f9b48c5b95489d988244a5c3f to your computer and use it in GitHub Desktop.

Select an option

Save takayoshiotake/04b3362f9b48c5b95489d988244a5c3f to your computer and use it in GitHub Desktop.
#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