Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions slices/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import "context"
// MapWithContextError transforms a slice of a given type to a new slice of a
// different type using a context and can return an error.
func MapWithContextError[T, K any](ctx context.Context, ss []T, fn func(context.Context, T) (K, error)) ([]K, error) {
if ss == nil {
return nil, nil
}

var res = make([]K, len(ss))

for i, v := range ss {
Expand Down
6 changes: 6 additions & 0 deletions slices/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ func TestMapWithContextError(t *testing.T) {
},
want: []int64{1, 22},
},
{
name: "nil",
fn: func(context.Context, string) (int64, error) {
return 0, nil
},
},
} {
t.Run(tt.name, func(t *testing.T) {
got, err := MapWithContextError(ctx, tt.in, tt.fn)
Expand Down
Loading