You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: assign6/README.md
+61-5Lines changed: 61 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -94,17 +94,73 @@ Should produce:
94
94
Course not found.
95
95
```
96
96
97
-
### Hints:
98
-
There are three monadic operations `and_then`, `transform`, and `or_else`. Read the description of each of them in the lecture slides, and take a look at [here](https://en.cppreference.com/w/cpp/utility/optional). Namely, you only need 2 of the mondadic operations.
97
+
### Monadic Operations
98
+
99
+
There are three monadic operations: [`and_then`](https://en.cppreference.com/w/cpp/utility/optional/and_then), [`transform`](https://en.cppreference.com/w/cpp/utility/optional/transform), and [`or_else`](https://en.cppreference.com/w/cpp/utility/optional/or_else). Read the description of each of them in the lecture slides, and take a look at [the standard library documentation](https://en.cppreference.com/w/cpp/utility/optional). You will only need to use 2 of the mondadic operations.
99
100
100
101
Your code should end up looking something like this:
Notice the lambda functionbeing passed into the first monadic function. This should be a hint!**Think about what type output is** and what you're monadic opertaions therefore need to return/produce.
110
+
It can help to **think about what the type of `output` is and work backwards from there**. Pay attention to what each of the monadic functions does, as described in the hint below.
111
+
112
+
> [!NOTE]
113
+
> Recall what the role is of each of the monadic functions. The official C++ library doesn't do a good job explaining this, so we have included a short reference here. Suppose `T` and `U` are arbitrary types.
114
+
>
115
+
> ```cpp
116
+
> /**
117
+
> * tl;dr;
118
+
> * Calls a function to produce a new optional if there is a value; otherwise, returns nothing.
119
+
> *
120
+
> * The function passed to `and_then` takes a non-optional instance of type `T` and returns a `std::optional<U>`.
121
+
> * If the optional has a value, `and_then` applies the function to its value and returns the result.
122
+
> * If the optional doesn't have a value (i.e. it is `std::nullopt`), it returns `std::nullopt`.
><sup>Note that the `->` notation in the lambda functionis a way of explicitly writing out the returntype of the function!</sup>
161
+
>
162
+
> Notice that since each method returns an `std::optional`, you can chain them together. If you are certain that the optional will have a value at the end of the chain, you could call [`.value()`](https://en.cppreference.com/w/cpp/utility/optional/value) to get the value. Otherwise, you could call [`.value_or(fallback)`](https://en.cppreference.com/w/cpp/utility/optional/value_or) to get the result or some other `fallback` value if the optional doesn't have a value.
0 commit comments