Skip to content
Closed
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
17 changes: 17 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ func FilterMap[T any, R any](collection []T, callback func(item T, index int) (R
return result
}

// FilterInPlace is a generic function that modifies the input slice in-place to contain only the elements
// that satisfy the provided predicate function. The predicate function takes an element of the slice and its index,
// and should return true for elements that should be kept and false for elements that should be removed.
// The function returns the modified slice, which may be shorter than the original if some elements were removed.
// Note that the order of elements in the original slice is preserved in the output.
func FilterInPlace[V any](collection []V, predicate func(item V, index int) bool) []V {
j := 0
for i, item := range collection {
if predicate(item, i) {
collection[j] = item
j++
}
}
return collection[:j]
}

// FlatMap manipulates a slice and transforms and flattens it to a slice of another type.
// The transform function can either return a slice or a `nil`, and in the `nil` case
// no value is added to the final slice.
Expand Down Expand Up @@ -592,3 +608,4 @@ func IsSortedByKey[T any, K constraints.Ordered](collection []T, iteratee func(i

return true
}

17 changes: 17 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ func TestFilter(t *testing.T) {
is.Equal(r2, []string{"foo", "bar"})
}

func TestFilterInPlace(t *testing.T) {
t.Parallel()
is := assert.New(t)

r1 := FilterInPlace([]int{1, 2, 3, 4}, func(x int, _ int) bool {
return x%2 == 0
})

is.Equal(r1, []int{2, 4})

r2 := FilterInPlace([]string{"", "foo", "", "bar", ""}, func(x string, _ int) bool {
return len(x) > 0
})

is.Equal(r2, []string{"foo", "bar"})
}

func TestMap(t *testing.T) {
t.Parallel()
is := assert.New(t)
Expand Down