From c00d9af59f36523303c9a0349169a1ebaf74662f Mon Sep 17 00:00:00 2001 From: Ruidy Date: Mon, 21 Mar 2022 13:04:38 -0400 Subject: [PATCH] drop (#18) * last function * drop function * add documentation * fix CI Co-authored-by: Ruidy --- drop.go | 12 ++++++++++++ drop_test.go | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 drop.go create mode 100644 drop_test.go diff --git a/drop.go b/drop.go new file mode 100644 index 0000000..e572c74 --- /dev/null +++ b/drop.go @@ -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 +} diff --git a/drop_test.go b/drop_test.go new file mode 100644 index 0000000..97a187d --- /dev/null +++ b/drop_test.go @@ -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)) +}