Skip to content

Commit fea00f8

Browse files
committed
difference between 'traonsform' and 'and_then' in monadic interface
1 parent 48d7357 commit fea00f8

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

cpp.fp/expected/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ auto parse_number(string_view str) -> expected<double, parse_error>
3535
auto print_error(parse_error e) -> expected<double, string>
3636
{
3737
if (e == parse_error::invalid_input)
38+
{
39+
logger.()
3840
return unexpected("error : invalid input");
41+
}
3942

4043
if (e == parse_error::overflow)
4144
return unexpected("error : overflow");

cpp.fp/optional/optional.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ int main(int argc, char* argv[])
2323
{
2424
vector_opt<string> v = { "1234", "15 foo", "bar", "42", "5000", " 5" };
2525

26+
// NOTICE
27+
// an operation which changes stored variable -> transform
28+
// an operation which could itself fail -> and_then
2629
auto filter = [](auto&& o) {
27-
return o.and_then(to_int) // flatmap from str to int
30+
return o.and_then(to_int) // flatmap from str to int
2831
.transform([](int n) { return n + 1; })
2932
.transform([](int n) { return to_string(n); })
3033
.or_else ([] { return optional("null"s); });

0 commit comments

Comments
 (0)