Skip to content

Commit

Permalink
Merge pull request #4796 from TheGamingMousse/minor-fixes
Browse files Browse the repository at this point in the history
minor fixes
  • Loading branch information
SansPapyrus683 committed Sep 22, 2024
2 parents 6400b18 + aa24560 commit e2ef494
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
16 changes: 10 additions & 6 deletions content/1_General/Lambda_Funcs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ description: 'Defining anonymous function objects.'
</Resources>

## Why Use Lambdas?
Lambdas allow us to write simple and anonymous functions in place.
Lambdas allow us to write simple and anonymous functions inline.
This allows us to write smaller functions while keeping code local and
organized. In addition, lambdas can capture their surrounding variables, which
is often convenient when writing helper functions.

In addition, the C++ standard library has good support for lambdas, with functions
The C++ standard library has good support for lambdas, with functions
like `std::sort` and `std::lower_bound` allowing you to pass custom functions
to act as a comparator of objects.
to act as a comparator of objects. This is often more convenient than the
alternative of writing a dedicated function to act as a comparator.

## General Form
Lambdas have the following form:
Expand Down Expand Up @@ -93,6 +94,9 @@ Then, you can call the lambda like a normal function.
cout << square(10) << '\n'; // prints out 100
```

As mentioned in the [custom comparator module](/silver/sorting-custom?lang=cpp)
module, lambdas can also be used as comparators.

## Recursive Lambdas

Let's say we want to write a recursive GCD function. With a lambda, the most
Expand Down Expand Up @@ -169,9 +173,9 @@ int main() {
}
```

### With `auto` as a parameter:
The main issue with recursive lambdas is that a lambda typically cannot access
itself. To fix this, we pass the lambda into itself.
### With Generic Lambdas
To remedy the issue of the lambda being unable to access itself, we pass it
into itself.

```cpp
int main() {
Expand Down
4 changes: 2 additions & 2 deletions content/4_Gold/Combinatorics.problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@
}
},
{
"uniqueId": "ac-XorPyramid",
"uniqueId": "cses-2419",
"name": "Xor Pyramid",
"url": "https://cses.fi/problemset/task/2419",
"source": "AC",
"source": "CSES",
"difficulty": "Normal",
"isStarred": false,
"tags": ["Combinatorics", "Bitwise"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
id: ac-XorPyramid
id: cses-2419
source: AC
title: Xor Pyramid
author: Mihnea Brebenel
Expand Down

0 comments on commit e2ef494

Please sign in to comment.