diff --git a/exercises/area b/exercises/area new file mode 100755 index 0000000..254837e Binary files /dev/null and b/exercises/area differ diff --git a/exercises/area.cpp b/exercises/area.cpp index b0a90d0..6b6129b 100644 --- a/exercises/area.cpp +++ b/exercises/area.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include // Change function `areaLessThan20` into lambda. @@ -31,9 +32,7 @@ class Circle { using CirclePtr = shared_ptr; using Collection = vector; -bool areaLessThan20(CirclePtr s) { - return (s && s->getArea() < 20); -} + void printCollection(const Collection& collection) { for (const auto & it : collection) { @@ -53,7 +52,8 @@ void printAreas(const Collection& collection) { void findFirstShapeMatchingPredicate(const Collection& collection, std::string info, - bool (*predicate)(CirclePtr s)) { + std::functionpredicate){ + // bool (*predicate)(CirclePtr s)) {{} auto it = std::find_if(collection.begin(), collection.end(), predicate); if(it != collection.end()) { cout << "First shape matching predicate: " << info << endl; @@ -84,9 +84,15 @@ int main() { cout << "Areas after sort: " << std::endl; printAreas(circles); + +auto areaLessThanX = [x=20](CirclePtr s)->bool{ +return (s && s->getArea() < x); +}; + + findFirstShapeMatchingPredicate(circles, "area less than 20", - areaLessThan20); + areaLessThanX); return 0; } diff --git a/exercises/dangling b/exercises/dangling new file mode 100755 index 0000000..c8bcca9 Binary files /dev/null and b/exercises/dangling differ diff --git a/exercises/dangling.cpp b/exercises/dangling.cpp index b701bb7..2969442 100644 --- a/exercises/dangling.cpp +++ b/exercises/dangling.cpp @@ -2,7 +2,7 @@ auto getIndexGenerator() { int value = 0; - auto lambda = [&value] { + auto lambda = [value] () mutable { return value++; }; return lambda; @@ -20,8 +20,9 @@ int getFive() { int main() { auto generator = getIndexGenerator(); [[maybe_unused]] int value = getFive(); + for (int i = 0; i < 10; ++i) { - std::cout << generator(); + generator(); } std::cout << '\n'; return 0; diff --git a/exercises/main.cpp b/exercises/main.cpp new file mode 100644 index 0000000..9374ccc --- /dev/null +++ b/exercises/main.cpp @@ -0,0 +1,15 @@ + +#include +#include + + constexpr auto add = [](int n, int m) { + auto L = [=] { return n; }; + auto R = [=] { return m; }; + return [=] { return L() + R(); }; +}; + + +int main() { + +static_assert(add(3, 4)() == 7); +} \ No newline at end of file