Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added exercises/area
Binary file not shown.
16 changes: 11 additions & 5 deletions exercises/area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
#include <memory>

// Change function `areaLessThan20` into lambda.
Expand Down Expand Up @@ -31,9 +32,7 @@ class Circle {
using CirclePtr = shared_ptr<Circle>;
using Collection = vector<CirclePtr>;

bool areaLessThan20(CirclePtr s) {
return (s && s->getArea() < 20);
}


void printCollection(const Collection& collection) {
for (const auto & it : collection) {
Expand All @@ -53,7 +52,8 @@ void printAreas(const Collection& collection) {

void findFirstShapeMatchingPredicate(const Collection& collection,
std::string info,
bool (*predicate)(CirclePtr s)) {
std::function<bool(CirclePtr)>predicate){
// 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;
Expand Down Expand Up @@ -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;
}
Binary file added exercises/dangling
Binary file not shown.
5 changes: 3 additions & 2 deletions exercises/dangling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

auto getIndexGenerator() {
int value = 0;
auto lambda = [&value] {
auto lambda = [value] () mutable {
return value++;
};
return lambda;
Expand All @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions exercises/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

#include <iostream>
#include <functional>

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);
}