From 7d422c59d3c81842f443fab6ce7d697924439447 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Fri, 28 Jan 2022 12:51:42 -0400 Subject: [PATCH] each (#10) * feat: maps map * :sparkles:: return initial slice Co-authored-by: Ruidy --- each.go | 4 +++- each_test.go | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) 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)) + })) +}