* feat: maps map

* : return initial slice

Co-authored-by: Ruidy <rnemausat@newstore.com>
This commit is contained in:
Ruidy 2022-01-28 12:51:42 -04:00 committed by GitHub
parent f73905ddb0
commit 7d422c59d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View file

@ -1,8 +1,10 @@
package underscore package underscore
// Each iterates over a slice of elements, yielding each in turn to an action function. // Each iterates over a slice of elements, yielding each in turn to an action function.
func Each[T any](values []T, action func(T)) { // Returns the slice for chaining.
func Each[T any](values []T, action func(T)) []T {
for _, v := range values { for _, v := range values {
action(v) action(v)
} }
return values
} }

View file

@ -20,3 +20,14 @@ func TestEach(t *testing.T) {
assert.Equal(t, want, res) assert.Equal(t, want, res)
} }
func TestEachReturnsInitialSlice(t *testing.T) {
names := []string{"Alice", "Bob", "Charles"}
want := []string{"Alice", "Bob", "Charles"}
res := make([]string, 0)
assert.Equal(t, want, underscore.Each(names, func(n string) {
res = append(res, fmt.Sprintf("Hi %s", n))
}))
}