Skip to content

Commit 1573d4c

Browse files
authored
docs: add a section for Lambda function expressions f(|x,y|x+y) (fix #23223) (#24789)
1 parent 8f17139 commit 1573d4c

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

‎doc/docs.md‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ by using any of the following commands in a terminal:
120120
* [Mutable arguments](#mutable-arguments)
121121
* [Variable number of arguments](#variable-number-of-arguments)
122122
* [Anonymous & higher-order functions](#anonymous--higher-order-functions)
123+
* [Lambda expressions](#lambda-expressions)
123124
* [Closures](#closures)
124125
* [Parameter evaluation order](#parameter-evaluation-order)
125126
* [References](#references)
@@ -3021,6 +3022,28 @@ fn main() {
30213022
}
30223023
```
30233024

3025+
### Lambda expressions
3026+
3027+
Lambda expressions in V are small anonymous functions, defined using
3028+
the `|variables| expression` syntax. Note: this syntax is valid only inside calls to higher
3029+
order functions.
3030+
3031+
Here are some examples:
3032+
```v
3033+
mut a := [1, 2, 3]
3034+
a.sort(|x, y| x > y) // sorts the array, defining the comparator with a lambda expression
3035+
println(a.map(|x| x * 10)) // prints [30, 20, 10]
3036+
```
3037+
3038+
```v
3039+
// Lambda function can be used as callback
3040+
fn f(cb fn (a int) int) int {
3041+
return cb(10)
3042+
}
3043+
3044+
println(f(|x| x + 4)) // prints 14
3045+
```
3046+
30243047
### Closures
30253048

30263049
V supports closures too.
@@ -8331,4 +8354,4 @@ This is the place to be, to discuss the V language, learn about latest
83318354
developments, quickly get help with issues, witness/participate in
83328355
~~epic flame wars~~ constructive criticism exchanges and design decisions.
83338356
Join it, and learn more about languages, games, editors, people, Klingons,
8334-
Conway's law and the universe.
8357+
Conway's law and the universe.

0 commit comments

Comments
 (0)