-
-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from Azer0s/feature/drop
Add Drop, DropRight, DropWhile and DropRightWhile
- Loading branch information
Showing
3 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
})) | ||
} |