Skip to content

Commit

Permalink
move drop functions
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Apr 18, 2022
1 parent 6c19890 commit ee50be0
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 129 deletions.
65 changes: 0 additions & 65 deletions drop.go

This file was deleted.

64 changes: 0 additions & 64 deletions drop_test.go

This file was deleted.

64 changes: 64 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,70 @@ func KeyBy[K comparable, V any](collection []V, iteratee func(V) K) map[K]V {
return result
}

// Drop drops n elements from the beginning of a slice or array.
func Drop[T any](collection []T, n int) []T {
if len(collection) <= n {
return make([]T, 0)
}

result := make([]T, len(collection)-n)
for i := n; i < len(collection); i++ {
result[i-n] = collection[i]
}

return result
}

// DropWhile drops elements from the beginning of a slice or array while the predicate returns true.
func DropWhile[T any](collection []T, predicate func(T) bool) []T {
i := 0
for ; i < len(collection); i++ {
if !predicate(collection[i]) {
break
}
}

result := make([]T, len(collection)-i)

for j := 0; i < len(collection); i, j = i+1, j+1 {
result[j] = collection[i]
}

return result
}

// DropRight drops n elements from the end of a slice or array.
func DropRight[T any](collection []T, n int) []T {
if len(collection) <= n {
return make([]T, 0)
}

result := make([]T, len(collection)-n)
for i := len(collection) - 1 - n; i >= 0; i-- {
result[i] = collection[i]
}

return result
}

// DropRightWhile drops elements from the end of a slice or array while the predicate returns true.
func DropRightWhile[T any](collection []T, predicate func(T) bool) []T {
i := len(collection) - 1
for ; i >= 0; i-- {
if !predicate(collection[i]) {
break
}
}

result := make([]T, i+1)

for ; i >= 0; i-- {
result[i] = collection[i]
}

return result
}

// Reject is the opposite of Filter, this method returns the elements of collection that predicate does not return truthy for.
func Reject[V any](collection []V, predicate func(V, int) bool) []V {
result := []V{}
Expand Down
58 changes: 58 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,64 @@ func TestKeyBy(t *testing.T) {
is.Equal(result1, map[int]string{1: "a", 2: "aa", 3: "aaa"})
}

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

is.Equal([]int{1, 2, 3, 4}, Drop([]int{0, 1, 2, 3, 4}, 1))
is.Equal([]int{2, 3, 4}, Drop([]int{0, 1, 2, 3, 4}, 2))
is.Equal([]int{3, 4}, Drop([]int{0, 1, 2, 3, 4}, 3))
is.Equal([]int{4}, Drop([]int{0, 1, 2, 3, 4}, 4))
is.Equal([]int{}, Drop([]int{0, 1, 2, 3, 4}, 5))
is.Equal([]int{}, Drop([]int{0, 1, 2, 3, 4}, 6))
}

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

is.Equal([]int{0, 1, 2, 3}, DropRight([]int{0, 1, 2, 3, 4}, 1))
is.Equal([]int{0, 1, 2}, DropRight([]int{0, 1, 2, 3, 4}, 2))
is.Equal([]int{0, 1}, DropRight([]int{0, 1, 2, 3, 4}, 3))
is.Equal([]int{0}, DropRight([]int{0, 1, 2, 3, 4}, 4))
is.Equal([]int{}, DropRight([]int{0, 1, 2, 3, 4}, 5))
is.Equal([]int{}, DropRight([]int{0, 1, 2, 3, 4}, 6))
}

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

is.Equal([]int{4, 5, 6}, DropWhile([]int{0, 1, 2, 3, 4, 5, 6}, func(t int) bool {
return t != 4
}))

is.Equal([]int{}, DropWhile([]int{0, 1, 2, 3, 4, 5, 6}, func(t int) bool {
return true
}))

is.Equal([]int{0, 1, 2, 3, 4, 5, 6}, DropWhile([]int{0, 1, 2, 3, 4, 5, 6}, func(t int) bool {
return t == 10
}))
}

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

is.Equal([]int{0, 1, 2, 3}, DropRightWhile([]int{0, 1, 2, 3, 4, 5, 6}, func(t int) bool {
return t != 3
}))

is.Equal([]int{0, 1}, DropRightWhile([]int{0, 1, 2, 3, 4, 5, 6}, func(t int) bool {
return t != 1
}))

is.Equal([]int{0, 1, 2, 3, 4, 5, 6}, DropRightWhile([]int{0, 1, 2, 3, 4, 5, 6}, func(t int) bool {
return t == 10
}))

is.Equal([]int{}, DropRightWhile([]int{0, 1, 2, 3, 4, 5, 6}, func(t int) bool {
return t != 10
}))
}

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

Expand Down

0 comments on commit ee50be0

Please sign in to comment.