doc: add anonymous function exemples on filter/map (#7089)
parent
1356ac63b4
commit
93fe2ce22d
10
doc/docs.md
10
doc/docs.md
|
@ -602,10 +602,20 @@ Arrays can be efficiently filtered and mapped with the `.filter()` and
|
||||||
nums := [1, 2, 3, 4, 5, 6]
|
nums := [1, 2, 3, 4, 5, 6]
|
||||||
even := nums.filter(it % 2 == 0)
|
even := nums.filter(it % 2 == 0)
|
||||||
println(even) // [2, 4, 6]
|
println(even) // [2, 4, 6]
|
||||||
|
// filter can accept anonymous functions
|
||||||
|
even_fn := nums.filter(fn (x int) bool {
|
||||||
|
return x % 2 == 0
|
||||||
|
})
|
||||||
|
println(even_fn)
|
||||||
|
|
||||||
words := ['hello', 'world']
|
words := ['hello', 'world']
|
||||||
upper := words.map(it.to_upper())
|
upper := words.map(it.to_upper())
|
||||||
println(upper) // ['HELLO', 'WORLD']
|
println(upper) // ['HELLO', 'WORLD']
|
||||||
|
// map can also accept anonymous functions
|
||||||
|
upper_fn := words.map(fn (w string) string {
|
||||||
|
return w.to_upper()
|
||||||
|
})
|
||||||
|
println(upper_fn) // ['HELLO', 'WORLD']
|
||||||
```
|
```
|
||||||
|
|
||||||
`it` is a builtin variable which refers to element currently being processed in filter/map methods.
|
`it` is a builtin variable which refers to element currently being processed in filter/map methods.
|
||||||
|
|
Loading…
Reference in New Issue