diff --git a/each.go b/each.go index 809098c..ea04159 100644 --- a/each.go +++ b/each.go @@ -1,8 +1,10 @@ package underscore // 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 { action(v) } + return values } diff --git a/each_test.go b/each_test.go index 1f4da32..9082869 100644 --- a/each_test.go +++ b/each_test.go @@ -20,3 +20,14 @@ func TestEach(t *testing.T) { 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)) + })) +}