Skip to content

Commit 8ce82af

Browse files
Update Lambda Functions Topic
1 parent f57c61a commit 8ce82af

9 files changed

Lines changed: 178 additions & 11 deletions

File tree

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
```C
2-
// TODO
2+
// No Native Support.
3+
// Use named functions and function pointers.
4+
5+
int add(int first, int second)
6+
{
7+
return first + second;
8+
}
9+
10+
int (*operation)(int, int) = add;
11+
int result = operation(10, 20);
312
```
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
11
```Cpp
2-
// TODO
2+
// Since C++11
3+
4+
// ------------------------------------
5+
// Definition
6+
// ------------------------------------
7+
auto functionName = []([parameters]) {
8+
// ...
9+
};
10+
11+
// ------------------------------------
12+
// Declaration Example
13+
// ------------------------------------
14+
auto add = [](int first, int second) {
15+
return first + second;
16+
};
17+
18+
int result = add(10, 20);
19+
20+
// Capture by value
21+
int factor {2};
22+
auto multiply = [factor](int value) {
23+
return value * factor;
24+
};
25+
26+
// Capture by reference
27+
auto increment = [&factor]() {
28+
factor++;
29+
};
330
```
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
11
```Cs
2-
// TODO
2+
// ------------------------------------
3+
// Definition
4+
// ------------------------------------
5+
Func<int, int, int> functionName = ([parameters]) => {
6+
// ...
7+
};
8+
9+
// ------------------------------------
10+
// Declaration Example
11+
// ------------------------------------
12+
Func<int, int, int> add = (first, second) => first + second;
13+
Func<int, int, int> addBlock = (first, second) =>
14+
{
15+
return first + second;
16+
};
17+
18+
Action<string> print = message => Console.WriteLine(message);
19+
20+
int result = add(10, 20);
321
```
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
```Go
2-
// TODO
2+
// Go has function literals.
3+
4+
// ------------------------------------
5+
// Definition
6+
// ------------------------------------
7+
functionName := func([parameters]) [returnType] {
8+
// ...
9+
}
10+
11+
// ------------------------------------
12+
// Declaration Example
13+
// ------------------------------------
14+
add := func(first int, second int) int {
15+
return first + second
16+
}
17+
18+
result := add(10, 20)
19+
20+
// Function literals can capture values from their environment.
21+
factor := 2
22+
multiply := func(value int) int {
23+
return value * factor
24+
}
325
```
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
11
```Java
2-
// TODO
2+
// Since Java 8
3+
4+
// ------------------------------------
5+
// Definition
6+
// ------------------------------------
7+
([parameters]) -> {
8+
// ...
9+
}
10+
11+
// ------------------------------------
12+
// Declaration Example
13+
// ------------------------------------
14+
BinaryOperator<Integer> add = (first, second) -> first + second;
15+
Consumer<String> print = message -> System.out.println(message);
16+
Supplier<Integer> getNumber = () -> 10;
17+
18+
int result = add.apply(10, 20);
19+
20+
List<String> names = List.of("Ada", "Grace");
21+
names.forEach(name -> System.out.println(name));
322
```

docs/programming-languages/lambda-functions/_javascript.mdx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,35 @@ const functionName = () => { /*...*/ } // no parameter
2525
// Named Parameters (Keyword Arguments)
2626
// ------------------------------------
2727

28-
// TODO
28+
// No Native Support.
29+
// Use object destructuring.
30+
const printUser = ({ name, age }) => {
31+
console.log(`${name}: ${age}`);
32+
}
33+
34+
printUser({ name: "Ada", age: 36 });
2935

3036

3137
// ------------------------------------
3238
// Optional Parameters
3339
// ------------------------------------
3440

35-
// TODO
41+
// No Native Support as syntax.
42+
// Check for undefined or use a default value.
43+
const printUser = (name, age) => {
44+
if (age === undefined) {
45+
// ...
46+
}
47+
}
3648

3749

3850
// ------------------------------------
3951
// Default Argument for Parameters
4052
// ------------------------------------
4153

42-
// TODO
54+
const connect = (host, port = 443) => {
55+
// ...
56+
}
4357

4458

4559
// ------------------------------------
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
```Python
2-
# TODO
2+
# ------------------------------------
3+
# Definition
4+
# ------------------------------------
5+
function_name = lambda [parameters]: expression
6+
7+
# ------------------------------------
8+
# Declaration Example
9+
# ------------------------------------
10+
add = lambda first, second: first + second
11+
result = add(10, 20)
12+
13+
numbers = [1, 2, 3]
14+
doubled = list(map(lambda value: value * 2, numbers))
15+
16+
# Prefer def for complex logic.
17+
def add(first, second):
18+
return first + second
319
```
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
11
```Rust
2-
// TODO
2+
// Also Known as Closures
3+
4+
// ------------------------------------
5+
// Definition
6+
// ------------------------------------
7+
let function_name = |[parameters]| {
8+
// ...
9+
};
10+
11+
// ------------------------------------
12+
// Declaration Example
13+
// ------------------------------------
14+
let add = |first: i32, second: i32| first + second;
15+
let add_block = |first: i32, second: i32| {
16+
return first + second;
17+
};
18+
19+
let result = add(10, 20);
20+
21+
// Closures can capture values from their environment.
22+
let factor = 2;
23+
let multiply = |value: i32| value * factor;
324
```
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
11
```Typescript
2-
// TODO
2+
// Also Known as Arrow Functions
3+
4+
// ------------------------------------
5+
// Definition
6+
// ------------------------------------
7+
const functionName = ([parameters]): returnType => {
8+
// ...
9+
};
10+
11+
// ------------------------------------
12+
// Declaration Example
13+
// ------------------------------------
14+
const add = (first: number, second: number): number => first + second;
15+
16+
const addBlock = (first: number, second: number): number => {
17+
return first + second;
18+
};
19+
20+
const print = (message: string): void => console.log(message);
21+
const getNumber = (): number => 10;
22+
23+
const result = add(10, 20);
324
```

0 commit comments

Comments
 (0)