File tree Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Expand file tree Collapse file tree 2 files changed +14
-3
lines changed Original file line number Diff line number Diff line change 1
1
package slice
2
2
3
3
// Drop
4
- // Creates a slice of array with n elements dropped from the beginning.
4
+ // Creates a slice of array with n elements dropped from the beginning while n >=0 and the endding while n < 0 .
5
5
func Drop [T any ](array []T , n int ) (result []T ) {
6
6
length := len (array )
7
+ if n < 0 {
8
+ n = length + n
9
+ }
7
10
if length <= n || n < 0 {
8
11
return array
9
12
}
Original file line number Diff line number Diff line change @@ -24,12 +24,20 @@ func TestDrop(t *testing.T) {
24
24
if result4 [len (result4 )- 1 ] != "bar" {
25
25
t .Error ("drop end error result: " , result4 )
26
26
}
27
+
28
+ result5 := Drop (arr3 , - 1 )
29
+ if result5 [len (result5 )- 1 ] != "bar" {
30
+ t .Error ("drop -1 error result: " , result5 )
31
+ }
27
32
}
28
33
29
34
func ExampleDrop () {
30
35
array := []int {1 , 3 , 4 , 5 }
31
-
32
36
fmt .Println (Drop (array , 1 ))
37
+
38
+ array2 := []string {"start" , "foo" , "bar" , "end" }
39
+ fmt .Println (Drop (array2 , - 1 ))
33
40
// Output:
34
- //[1 4 5]
41
+ // [1 4 5]
42
+ // [start foo bar]
35
43
}
You can’t perform that action at this time.
0 commit comments