Total Lines | 17 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package gotil |
||
2 | |||
3 | // Creates an array of values by running each element in the given array thru iteratee. |
||
4 | // The value to be iterated should be given as the first parameter. |
||
5 | // The second parameter will be a function that will take the parameter and return value type interface{}. |
||
6 | // result := gotil.Map([]int{10, 20}, func(v, i int) int { |
||
7 | // return v * v |
||
8 | // }) |
||
9 | // fmt.Println(result) |
||
10 | // // Output: 100, 400 |
||
11 | |||
12 | func Map[T, D any](s []T, f func(val T, i int) D) []D { |
||
13 | result := make([]D, len(s)) |
||
14 | for i, v := range s { |
||
15 | result[i] = f(v, i) |
||
16 | } |
||
17 | return result |
||
18 | } |
||
19 |