Skip to content

Commit 5cd7154

Browse files
committed
WIP
1 parent 22e29ab commit 5cd7154

File tree

1 file changed

+67
-11
lines changed

1 file changed

+67
-11
lines changed

sources/modules/functions/lambdas.md

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ _Max 5 items._
5454

5555
A student should be able to:
5656

57-
1. Use a lambda taking a concrete type as a parameter and return a value in response
58-
2.
59-
3.
60-
4.
61-
5.
57+
1. Use a lambda taking a concrete type as a parameter and return a value
58+
2. transform a function definition into a lambda and use it
59+
3. enumerate trade-offs of code-locality using lambdas vs code reuse with free-functions
60+
4. assign a lambda to a variable for multiple calls
61+
5. named lambdas to increase code readability
62+
63+
6264

6365
#### Caveats
6466

@@ -79,14 +81,62 @@ _This section lists important details for each point._
7981

8082
A student should be able to:
8183

82-
1. Utilize std::function as a means to hold and transfer lambdas
84+
1. Utilize `std::function` as a means to hold and transfer lambdas
8385
2. Define a function-template taking a lambda as a template parameter
84-
3. Use captures to change
86+
3. specify, per parameter, whether to capture by reference or value
87+
4. specify the default capture type for a lambda
88+
5. use a lambda in a class, capturing and utilizing class data via `this`
89+
90+
#### Caveats
91+
92+
#### Points to cover
93+
94+
motivation:
95+
1. how to write library code to call user code? --- callbacks
96+
2. root finding using lambdas
97+
3. "customizing" generic algorithms
98+
99+
### Advanced
100+
Explain why they can't write out the type of a lambda?
101+
_These are important topics that are not expected to be covered but provide
102+
guidance where one can continue to investigate this topic in more depth._
103+
104+
105+
#### Background/Required Knowledge
106+
107+
* All of the above.
85108

109+
#### Student outcomes
110+
111+
A student should be able to:
112+
113+
1. Use a lambda to introduce a new identifier for use within the body of the lambda
114+
2. explain the relationships between generic lambdas and templates
115+
3. construct an object with choice between 2 identifiers, using immediately-dispathced lambda
116+
4. utilize an immediately dispatched lambda to encode multiple statements where the language requires an expression
117+
5. use the `mutable` keyword to allow changing a captured-by-value or lambda-local value within the lambda body
118+
6. explicitly specify the return type for a lambda
119+
7. explain under what conditions an explicit return type is necessary
120+
86121
#### Caveats
87122

88123
#### Points to cover
89124

125+
Construct an object with choice between 2 identifiers:
126+
```
127+
class X;
128+
bool b;
129+
X x = [c = b](){ if (c) return X(5, 10) else X("a", "b"); }();
130+
```
131+
132+
multiple statements in a single expression
133+
```
134+
assert([](){
135+
std::vector v{1, 2, 3, 4, 5};
136+
for (auto x: v) std::cout << v << std::endl;
137+
}());
138+
```
139+
90140
Capture can introduce new names
91141
```
92142
char const* const s = "5";
@@ -96,6 +146,9 @@ char const* const s = "5";
96146
```
97147
std::ranges::for_each(
98148
std::vector{1,2,3,4,5},
149+
// introducing new identifier
150+
// generic lambda
151+
// mutable allows changing "init"
99152
[init = true] (auto&& x) mutable {
100153
if (init)
101154
{ std::cout << x; init = false};
@@ -104,7 +157,10 @@ std::ranges::for_each(
104157
});
105158
```
106159

107-
### Advanced
108-
Explain why they can't write out the type of a lambda?
109-
_These are important topics that are not expected to be covered but provide
110-
guidance where one can continue to investigate this topic in more depth._
160+
When to use lambdas or not:
161+
forcing code into a lambda can prevent some features
162+
```
163+
std::for_each(std::par_sec, v, [](){/* can't utilize OpenMP here */}};
164+
for(auto i : v){ /* OpenMP can be utilized here */ }
165+
```
166+

0 commit comments

Comments
 (0)