Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pull methods from lodash #531

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
62 changes: 62 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lo

import (
"slices"
"sort"

"github.com/samber/lo/internal/constraints"
Expand Down Expand Up @@ -693,3 +694,64 @@ func Splice[T any, Slice ~[]T](collection Slice, i int, elements ...T) Slice {

return append(append(append(output, collection[:i]...), elements...), collection[i:]...)
}

// Pull removes all given values from slice using SameValueZero for equality comparisons.
SicParv1sMagna marked this conversation as resolved.
Show resolved Hide resolved
func Pull[T comparable, Slice ~[]T](collection Slice, elements ...T) Slice {
output := make(Slice, 0, len(collection))

for _, item := range collection {
if !slices.Contains(elements, item) {
output = append(output, item)
}
}

return output
}

// PullAllBy is like [Pull] except that it accepts iteratee which is invoked
// for each element of slice and values to generate the criterion by which they're compared
func PullAllBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(T) U, elements ...U) Slice {
elementCriteria := make(map[U]struct{}, len(elements))
for _, element := range elements {
elementCriteria[element] = struct{}{}
}

var output Slice
for _, item := range collection {
if _, shouldRemove := elementCriteria[iteratee(item)]; !shouldRemove {
output = append(output, item)
}
}

return output
}

// PullAt removes elements from slice corresponding to indexes and returns
// an array of removed elements
func PullAt[T any](slice []T, indexes ...int) ([]T, []T) {
if len(slice) == 0 || len(indexes) == 0 {
return nil, slice
}
SicParv1sMagna marked this conversation as resolved.
Show resolved Hide resolved

sort.Sort(sort.Reverse(sort.IntSlice(indexes)))

indexMap := make(map[int]struct{}, len(indexes))
SicParv1sMagna marked this conversation as resolved.
Show resolved Hide resolved
for _, index := range indexes {
if index >= 0 && index < len(slice) {
indexMap[index] = struct{}{}
}
}

var removed []T
output := make([]T, 0, len(slice)-len(indexMap))

for i, v := range slice {
if _, ok := indexMap[i]; ok {
removed = append(removed, v)
} else {
output = append(output, v)
}
}

return removed, output
}
93 changes: 93 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,3 +1029,96 @@ func TestSplice(t *testing.T) {
nonempty := Splice(allStrings, 1, "1", "2")
is.IsType(nonempty, allStrings, "type preserved")
}

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

result1 := Pull([]int{0, 1, 1, 2, 2, 2, 3, 3, 3}, 1)
result2 := Pull([]int{0, 1, 1, 2, 2, 2, 3, 3, 3}, 5)
result3 := Pull([]int{}, 1)
result4 := Pull([]int{0}, 0)
result5 := Pull([]string{"a", "b", "c", "a", "b", "c"}, "a", "c")
result6 := Pull([]string{"h", "e", "l", "l", "o"}, "a", "c")

is.Equal(result1, []int{0, 2, 2, 2, 3, 3, 3})
is.Equal(result2, []int{0, 1, 1, 2, 2, 2, 3, 3, 3})
is.Equal(result3, []int{})
is.Equal(result4, []int{})
is.Equal(result5, []string{"b", "b"})
is.Equal(result6, []string{"h", "e", "l", "l", "o"})
}

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

result1 := PullAllBy([]int{1, 2, 3, 4, 5}, func(n int) int {
return n
}, 2, 4)
is.Equal(result1, []int{1, 3, 5})

result2 := PullAllBy([]string{"apple", "banana", "pear", "kiwi"}, func(s string) int {
return len(s)
}, 5, 4)
is.Equal(result2, []string{"banana"})

result3 := PullAllBy([]int{1, 2, 3, 4, 5}, func(n int) int {
return n * n
}, 1, 9, 25)
is.Equal(result3, []int{2, 4})

result4 := PullAllBy([]string{"hello", "hi", "world", "welcome"}, func(s string) byte {
return s[0]
}, 'h')
is.Equal(result4, []string{"world", "welcome"})
}

func TestPullAt(t *testing.T) {
assert := assert.New(t)

tests := []struct {
name string
slice []int
indexes []int
expectedRemoved []int
expectedRemaining []int
}{
{
name: "Remove multiple elements",
slice: []int{1, 2, 3, 4, 5, 6},
indexes: []int{1, 3, 5},
expectedRemoved: []int{2, 4, 6},
expectedRemaining: []int{1, 3, 5},
},
{
name: "Remove elements out of range",
slice: []int{1, 2, 3, 4, 5},
indexes: []int{10, 15},
expectedRemoved: []int{},
expectedRemaining: []int{1, 2, 3, 4, 5},
},
{
name: "Empty slice",
slice: []int{},
indexes: []int{0},
expectedRemoved: []int{},
expectedRemaining: []int{},
},
{
name: "Remove all elements",
slice: []int{1, 2, 3},
indexes: []int{0, 1, 2},
expectedRemoved: []int{1, 2, 3},
expectedRemaining: []int{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
removed, remaining := PullAt(tt.slice, tt.indexes...)
assert.ElementsMatch(tt.expectedRemoved, removed, "Removed elements do not match")
assert.ElementsMatch(tt.expectedRemaining, remaining, "Remaining elements do not match")
})
}
}