mirror of
https://github.com/rjNemo/underscore
synced 2026-06-12 13:36:40 +00:00
drop (#18)
* last function * drop function * add documentation * fix CI Co-authored-by: Ruidy <rnemausat@newstore.com>
This commit is contained in:
parent
fb517f3b04
commit
c00d9af59f
2 changed files with 29 additions and 0 deletions
12
drop.go
Normal file
12
drop.go
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
package underscore
|
||||||
|
|
||||||
|
// Drop returns the rest of the elements in a slice.
|
||||||
|
// Pass an index to return the values of the slice from that index onward.
|
||||||
|
func Drop[T any](values []T, index int) (rest []T) {
|
||||||
|
for i, value := range values {
|
||||||
|
if i != index {
|
||||||
|
rest = append(rest, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return rest
|
||||||
|
}
|
||||||
17
drop_test.go
Normal file
17
drop_test.go
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
package underscore_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
u "github.com/rjNemo/underscore"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDrop(t *testing.T) {
|
||||||
|
|
||||||
|
nums := []int{1, 9, 2, 8, 3, 7, 4, 6, 5}
|
||||||
|
want := []int{1, 9, 2, 3, 7, 4, 6, 5}
|
||||||
|
|
||||||
|
assert.Equal(t, want, u.Drop(nums, 3))
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue