Skip to content

Commit

Permalink
Merge pull request #28 from Azer0s/feature/drop
Browse files Browse the repository at this point in the history
Add Drop, DropRight, DropWhile and DropRightWhile
  • Loading branch information
samber authored Mar 10, 2022
2 parents 2a41c2c + 766a67a commit 40e92cd
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 0 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ Supported helpers for slices:
- Fill
- Repeat
- ToMap
- Drop
- DropRight
- DropWhile
- DropRightWhile

Supported helpers for maps:

Expand Down Expand Up @@ -411,6 +415,46 @@ m := lo.ToMap[int, string]([]string{"a", "aa", "aaa"}, func(str string) int {
// map[int]string{1: "a", 2: "aa", 3: "aaa"}
```

### Drop

Drops n elements from the beginning of a slice or array.

```go
l := lo.Drop[int]([]int{0, 1, 2, 3, 4, 5}, 2)
// []int{2, 3, 4, 5}
```

### DropRight

Drops n elements from the end of a slice or array.

```go
l := lo.DropRight[int]([]int{0, 1, 2, 3, 4, 5}, 2)
// []int{0, 1, 2, 3}
```

### DropWhile

Drop elements from the beginning of a slice or array while the predicate returns true.

```go
l := lo.DropWhile[string]([]string{"a", "aa", "aaa", "aa", "aa"}, func(val string) bool {
return len(val) <= 2
})
// []string{"aaa", "aa", "a"}
```

### DropRightWhile

Drop elements from the end of a slice or array while the predicate returns true.

```go
l := lo.DropRightWhile[string]([]string{"a", "aa", "aaa", "aa", "aa"}, func(val string) bool {
return len(val) <= 2
})
// []string{"a", "aa", "aaa"}
```

### Keys

Creates an array of the map keys.
Expand Down
65 changes: 65 additions & 0 deletions drop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package lo

//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
}
64 changes: 64 additions & 0 deletions drop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package lo

import (
"github.com/stretchr/testify/assert"
"testing"
)

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
}))
}

0 comments on commit 40e92cd

Please sign in to comment.