feat: each

This commit is contained in:
Ruidy 2021-12-29 10:09:51 -04:00
parent fa9989c181
commit b4e8b7acc8
2 changed files with 29 additions and 0 deletions

7
each.go Normal file
View file

@ -0,0 +1,7 @@
package underscore
func Each[T any](values []T, predicate func(T)) {
for _, v := range values {
predicate(v)
}
}

22
each_test.go Normal file
View file

@ -0,0 +1,22 @@
package underscore_test
import (
"fmt"
"testing"
"github.com/rjNemo/underscore"
"github.com/stretchr/testify/assert"
)
func TestEach(t *testing.T) {
names := []string{"Alice", "Bob", "Charles"}
want := []string{"Hi Alice", "Hi Bob", "Hi Charles"}
res := make([]string, 0)
underscore.Each(names, func(n string) {
res = append(res, fmt.Sprintf("Hi %s", n))
})
assert.Equal(t, want, res)
}