Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
rwdougla committed Dec 5, 2023
1 parent 22e29ab commit 5cd7154
Showing 1 changed file with 67 additions and 11 deletions.
78 changes: 67 additions & 11 deletions sources/modules/functions/lambdas.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ _Max 5 items._

A student should be able to:

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



#### Caveats

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

A student should be able to:

1. Utilize std::function as a means to hold and transfer lambdas
1. Utilize `std::function` as a means to hold and transfer lambdas
2. Define a function-template taking a lambda as a template parameter
3. Use captures to change
3. specify, per parameter, whether to capture by reference or value
4. specify the default capture type for a lambda
5. use a lambda in a class, capturing and utilizing class data via `this`

#### Caveats

#### Points to cover

motivation:
1. how to write library code to call user code? --- callbacks
2. root finding using lambdas
3. "customizing" generic algorithms

### Advanced
Explain why they can't write out the type of a lambda?
_These are important topics that are not expected to be covered but provide
guidance where one can continue to investigate this topic in more depth._


#### Background/Required Knowledge

* All of the above.

#### Student outcomes

A student should be able to:

1. Use a lambda to introduce a new identifier for use within the body of the lambda
2. explain the relationships between generic lambdas and templates
3. construct an object with choice between 2 identifiers, using immediately-dispathced lambda
4. utilize an immediately dispatched lambda to encode multiple statements where the language requires an expression
5. use the `mutable` keyword to allow changing a captured-by-value or lambda-local value within the lambda body
6. explicitly specify the return type for a lambda
7. explain under what conditions an explicit return type is necessary

#### Caveats

#### Points to cover

Construct an object with choice between 2 identifiers:
```
class X;
bool b;
X x = [c = b](){ if (c) return X(5, 10) else X("a", "b"); }();
```

multiple statements in a single expression
```
assert([](){
std::vector v{1, 2, 3, 4, 5};
for (auto x: v) std::cout << v << std::endl;
}());
```

Capture can introduce new names
```
char const* const s = "5";
Expand All @@ -96,6 +146,9 @@ char const* const s = "5";
```
std::ranges::for_each(
std::vector{1,2,3,4,5},
// introducing new identifier
// generic lambda
// mutable allows changing "init"
[init = true] (auto&& x) mutable {
if (init)
{ std::cout << x; init = false};
Expand All @@ -104,7 +157,10 @@ std::ranges::for_each(
});
```

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

0 comments on commit 5cd7154

Please sign in to comment.