doc: add anonymous function exemples on filter/map (#7089)

pull/7094/head
Andréas Livet 2020-12-02 14:41:31 +01:00 committed by GitHub
parent 1356ac63b4
commit 93fe2ce22d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 0 deletions

View File

@ -602,10 +602,20 @@ Arrays can be efficiently filtered and mapped with the `.filter()` and
nums := [1, 2, 3, 4, 5, 6]
even := nums.filter(it % 2 == 0)
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']
upper := words.map(it.to_upper())
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.