mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 01:21:38 +08:00
update book code
This commit is contained in:
32
eBook/exercises/chapter_7/map_function.go
Executable file
32
eBook/exercises/chapter_7/map_function.go
Executable file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
list := []int{0, 1, 2, 3, 4, 5, 6, 7}
|
||||
mf := func(i int) int {
|
||||
return i * 10
|
||||
}
|
||||
/*
|
||||
result := mapFunc(mf, list)
|
||||
for _, v := range result {
|
||||
fmt.Println(v)
|
||||
}
|
||||
*/
|
||||
println()
|
||||
// shorter:
|
||||
fmt.Printf("%v", mapFunc(mf, list) )
|
||||
}
|
||||
|
||||
func mapFunc(mf func(int) int, list []int) ([]int) {
|
||||
result := make([]int, len(list))
|
||||
for ix, v := range list {
|
||||
result[ix] = mf(v)
|
||||
}
|
||||
/*
|
||||
for ix := 0; ix<len(list); ix++ {
|
||||
result[ix] = mf(list[ix])
|
||||
}
|
||||
*/
|
||||
return result
|
||||
}
|
Reference in New Issue
Block a user