Skip to content

Instantly share code, notes, and snippets.

@IFeelBloated
Created April 7, 2022 03:19
Show Gist options
  • Save IFeelBloated/0a4afe2136ffd1c437bf6e0ceb78ba01 to your computer and use it in GitHub Desktop.
Save IFeelBloated/0a4afe2136ffd1c437bf6e0ceb78ba01 to your computer and use it in GitHub Desktop.
auto x = 42;
auto y = 2.71;
auto z = "hello";
auto f(auto&& x) {
return x + x;
}
auto x = f(21); // x == 42
auto y = f("hello"s); // y == "hellohello"
for (auto c = std::array{ 1, 2, 3, 4 }; auto x : c)
std::cout << x;
auto f(auto&& x, auto&& y) {
return std::tuple{ x + x, y + y };
}
auto [x, y] = f(21, "hello"s); // x == 42, y == "hellohello"
auto curry2(auto f) {
return [=](auto x) { return [=](auto y) { return f(x, y); }; };
}
auto v = curry2([](auto x, auto y){ return x + y; })(11)(22); // v == 33
// type level magic
struct A {
auto f(this auto&& self) {}
};
struct B {};
auto g(auto&& x) {
if constexpr (requires { x.f(); })
return 42;
else
return "hello"s;
}
auto x = g(A{}); // std::same_as<decltype(x), int> == true
auto y = g(B{}); // std::same_as<decltype(y), std::string> == true
x = 42
y = 2.71
z = 'hello'
def f(x):
return x + x
x = f(21) # x == 42
y = f('hello') # y == 'hellohello'
c = [1, 2, 3, 4]
for x in c:
print(x)
def f(x, y):
return x + x, y + y
x, y = f(21, 'hello') # x == 42, y == 'hellohello'
def curry2(f):
return lambda x: lambda y: f(x, y)
v = curry2(lambda x, y: x + y)(11)(22) # v == 33
# type level magic
class A:
def f(self):
pass
class B:
pass
def g(x):
if hasattr(x, 'f'):
return 42
else:
return 'hello'
x = g(A()) # type(x) == int
y = g(B()) # type(y) == str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment