Created
April 27, 2021 02:23
-
-
Save thepabloaguilar/9762d5b1f62f73fff83b17dc2125ec16 to your computer and use it in GitHub Desktop.
A `map` implementation for go Slices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strconv" | |
) | |
func Map[T1, T2 any](s []T1, f func(T1) T2) []T2 { | |
newSlice := make([]T2, 0) | |
for _, item := range s { | |
newSlice = append(newSlice, f(item)) | |
} | |
return newSlice | |
} | |
func main() { | |
s := []int {1, 2, 3, 4} | |
newS := Map(s, strconv.Itoa) | |
fmt.Printf("%T -> %s", newS, newS) | |
// []string -> [1 2 3 4] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment