docs: high order functions
parent
50436a0e4a
commit
aef756a3fd
13
doc/docs.md
13
doc/docs.md
|
@ -689,7 +689,7 @@ fn register(u User) User {
|
||||||
user = register(user)
|
user = register(user)
|
||||||
```
|
```
|
||||||
|
|
||||||
## High order functions
|
## Anonymous & high order functions
|
||||||
|
|
||||||
```v
|
```v
|
||||||
fn sqr(n int) int {
|
fn sqr(n int) int {
|
||||||
|
@ -702,6 +702,17 @@ fn run(value int, op fn(int) int) int {
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println(run(5, sqr)) // "25"
|
println(run(5, sqr)) // "25"
|
||||||
|
|
||||||
|
// Anonymous functions can be declared inside other functions:
|
||||||
|
double_fn := fn(n int) int {
|
||||||
|
return n + n
|
||||||
|
}
|
||||||
|
println(run(5, double_fn)) // "10"
|
||||||
|
|
||||||
|
// Functions can be passed around without assigning them to variables:
|
||||||
|
res := run(5, fn(n int) int {
|
||||||
|
return n + n
|
||||||
|
})
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue