From ee50be0e9339641450fc16584605a76196f58b81 Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Tue, 19 Apr 2022 00:33:50 +0200 Subject: [PATCH] move drop functions --- drop.go | 65 --------------------------------------------------- drop_test.go | 64 -------------------------------------------------- slice.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ slice_test.go | 58 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 129 deletions(-) delete mode 100644 drop.go delete mode 100644 drop_test.go diff --git a/drop.go b/drop.go deleted file mode 100644 index eb725f55..00000000 --- a/drop.go +++ /dev/null @@ -1,65 +0,0 @@ -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 -} diff --git a/drop_test.go b/drop_test.go deleted file mode 100644 index a67f3f81..00000000 --- a/drop_test.go +++ /dev/null @@ -1,64 +0,0 @@ -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 - })) -} diff --git a/slice.go b/slice.go index f21ce9d5..4717fb87 100644 --- a/slice.go +++ b/slice.go @@ -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{} diff --git a/slice_test.go b/slice_test.go index f4fc446c..a07ffc53 100644 --- a/slice_test.go +++ b/slice_test.go @@ -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)