Skip to content

Commit

Permalink
Apply it to the GroceryList
Browse files Browse the repository at this point in the history
  • Loading branch information
xtofl committed Feb 10, 2019
1 parent ff01021 commit 4b85733
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
17 changes: 10 additions & 7 deletions monoid/groceries_monoid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
#include "groceries.h"
#include "monoid.hpp"

const auto intmapmonoid =
lean::fmonoid<std::map<std::string, int>>(
lean::monoid(0, std::plus<int>{})
);

const auto grocery_monoid = lean::monoid(
GroceryList{},
[](auto a, auto b){
for (const auto &ib: b.items) {
a.items[ib.first] += ib.second;
}
return a;
});
GroceryList{intmapmonoid.mempty},
[](auto a, auto b) -> GroceryList{
return {intmapmonoid.mappend(a.items, b.items)};
}
);

template<>
GroceryList overloading::mempty<GroceryList>() { return {}; }
Expand Down
19 changes: 19 additions & 0 deletions monoid/monoid.md
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,25 @@ EXPECT_EQ(
)
);
```
--

## After

```C++
const auto intmapmonoid =
lean::fmonoid<std::map<std::string, int>>(
lean::monoid(0, std::plus<int>{})
);

const auto grocery_monoid = lean::monoid(
GroceryList{intmapmonoid.mempty},
[](auto a, auto b) -> GroceryList {
return {intmapmonoid.mappend(a.items, b.items)};
}
);
```

(We could dig deeper and also extract the `{ ... .items}`)

--

Expand Down

0 comments on commit 4b85733

Please sign in to comment.