-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
559127a
commit 1c2890c
Showing
3 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package slicesx | ||
|
||
func Map[S ~[]E, E, T any](s S, f func(E) T) []T { | ||
t := make([]T, len(s)) | ||
for i, e := range s { | ||
t[i] = f(e) | ||
} | ||
return t | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package slicesx | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMap(t *testing.T) { | ||
type nobelPrize struct { | ||
Category string | ||
Laureate string | ||
} | ||
nobelPrizes := []nobelPrize{ | ||
{Category: "Chemistry", Laureate: "Luis Leloir"}, | ||
{Category: "Economics", Laureate: "Paul Samuelson"}, | ||
{Category: "Literature", Laureate: "Aleksandr Solzhenitsyn"}, | ||
{Category: "Peace", Laureate: "Norman Borlaug"}, | ||
{Category: "Physics", Laureate: "Hannes Alfvén"}, | ||
{Category: "Physics", Laureate: "Louis Néel"}, | ||
{Category: "Medicine", Laureate: "Sir Bernard Katz"}, | ||
{Category: "Medicine", Laureate: "Ulf von Euler"}, | ||
{Category: "Medicine", Laureate: "Julius Axelrod"}, | ||
} | ||
|
||
lowered := Map(nobelPrizes, func(n nobelPrize) string { | ||
return strings.ToLower(n.Laureate) | ||
}) | ||
|
||
assert.EqualValues(t, []string{ | ||
"luis leloir", | ||
"paul samuelson", | ||
"aleksandr solzhenitsyn", | ||
"norman borlaug", | ||
"hannes alfvén", | ||
"louis néel", | ||
"sir bernard katz", | ||
"ulf von euler", | ||
"julius axelrod", | ||
}, lowered) | ||
} |