Skip to content

Instantly share code, notes, and snippets.

@thepabloaguilar
Created April 27, 2021 02:23
Show Gist options
  • Save thepabloaguilar/9762d5b1f62f73fff83b17dc2125ec16 to your computer and use it in GitHub Desktop.
Save thepabloaguilar/9762d5b1f62f73fff83b17dc2125ec16 to your computer and use it in GitHub Desktop.

Revisions

  1. thepabloaguilar created this gist Apr 27, 2021.
    21 changes: 21 additions & 0 deletions map.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    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]
    }