From Jason Turner

[optional.monadic]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpnu1c7wic/{from.md → to.md} +128 -0
tmp/tmpnu1c7wic/{from.md → to.md} RENAMED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #### Monadic operations <a id="optional.monadic">[[optional.monadic]]</a>
2
+
3
+ ``` cpp
4
+ template<class F> constexpr auto and_then(F&& f) &;
5
+ template<class F> constexpr auto and_then(F&& f) const &;
6
+ ```
7
+
8
+ Let `U` be `invoke_result_t<F, decltype(value())>`.
9
+
10
+ *Mandates:* `remove_cvref_t<U>` is a specialization of `optional`.
11
+
12
+ *Effects:* Equivalent to:
13
+
14
+ ``` cpp
15
+ if (*this) {
16
+ return invoke(std::forward<F>(f), value());
17
+ } else {
18
+ return remove_cvref_t<U>();
19
+ }
20
+ ```
21
+
22
+ ``` cpp
23
+ template<class F> constexpr auto and_then(F&& f) &&;
24
+ template<class F> constexpr auto and_then(F&& f) const &&;
25
+ ```
26
+
27
+ Let `U` be `invoke_result_t<F, decltype(std::move(value()))>`.
28
+
29
+ *Mandates:* `remove_cvref_t<U>` is a specialization of `optional`.
30
+
31
+ *Effects:* Equivalent to:
32
+
33
+ ``` cpp
34
+ if (*this) {
35
+ return invoke(std::forward<F>(f), std::move(value()));
36
+ } else {
37
+ return remove_cvref_t<U>();
38
+ }
39
+ ```
40
+
41
+ ``` cpp
42
+ template<class F> constexpr auto transform(F&& f) &;
43
+ template<class F> constexpr auto transform(F&& f) const &;
44
+ ```
45
+
46
+ Let `U` be `remove_cv_t<invoke_result_t<F, decltype(value())>>`.
47
+
48
+ *Mandates:* `U` is a non-array object type other than `in_place_t` or
49
+ `nullopt_t`. The declaration
50
+
51
+ ``` cpp
52
+ U u(invoke(std::forward<F>(f), value()));
53
+ ```
54
+
55
+ is well-formed for some invented variable `u`.
56
+
57
+ [*Note 1*: There is no requirement that `U` is
58
+ movable [[dcl.init.general]]. — *end note*]
59
+
60
+ *Returns:* If `*this` contains a value, an `optional<U>` object whose
61
+ contained value is direct-non-list-initialized with
62
+ `invoke(std::forward<F>(f), value())`; otherwise, `optional<U>()`.
63
+
64
+ ``` cpp
65
+ template<class F> constexpr auto transform(F&& f) &&;
66
+ template<class F> constexpr auto transform(F&& f) const &&;
67
+ ```
68
+
69
+ Let `U` be
70
+ `remove_cv_t<invoke_result_t<F, decltype(std::move(value()))>>`.
71
+
72
+ *Mandates:* `U` is a non-array object type other than `in_place_t` or
73
+ `nullopt_t`. The declaration
74
+
75
+ ``` cpp
76
+ U u(invoke(std::forward<F>(f), std::move(value())));
77
+ ```
78
+
79
+ is well-formed for some invented variable `u`.
80
+
81
+ [*Note 2*: There is no requirement that `U` is
82
+ movable [[dcl.init.general]]. — *end note*]
83
+
84
+ *Returns:* If `*this` contains a value, an `optional<U>` object whose
85
+ contained value is direct-non-list-initialized with
86
+ `invoke(std::forward<F>(f), std::move(value()))`; otherwise,
87
+ `optional<U>()`.
88
+
89
+ ``` cpp
90
+ template<class F> constexpr optional or_else(F&& f) const &;
91
+ ```
92
+
93
+ *Constraints:* `F` models `invocable<>` and `T` models
94
+ `copy_constructible`.
95
+
96
+ *Mandates:* `is_same_v<remove_cvref_t<invoke_result_t<F>>, optional>` is
97
+ `true`.
98
+
99
+ *Effects:* Equivalent to:
100
+
101
+ ``` cpp
102
+ if (*this) {
103
+ return *this;
104
+ } else {
105
+ return std::forward<F>(f)();
106
+ }
107
+ ```
108
+
109
+ ``` cpp
110
+ template<class F> constexpr optional or_else(F&& f) &&;
111
+ ```
112
+
113
+ *Constraints:* `F` models `invocable<>` and `T` models
114
+ `move_constructible`.
115
+
116
+ *Mandates:* `is_same_v<remove_cvref_t<invoke_result_t<F>>, optional>` is
117
+ `true`.
118
+
119
+ *Effects:* Equivalent to:
120
+
121
+ ``` cpp
122
+ if (*this) {
123
+ return std::move(*this);
124
+ } else {
125
+ return std::forward<F>(f)();
126
+ }
127
+ ```
128
+