-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: bzsuni <[email protected]>
- Loading branch information
Showing
8 changed files
with
235 additions
and
27 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
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,21 @@ | ||
// Copyright 2022 Authors of spidernet-io | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package slice | ||
|
||
func RemoveElement[T int | string](slice []T, element T) []T { | ||
i := indexOf(slice, element) | ||
if i == -1 { | ||
return slice | ||
} | ||
return append(slice[:i], slice[i+1:]...) | ||
} | ||
|
||
func indexOf[T int | string](slice []T, element T) int { | ||
for i, v := range slice { | ||
if v == element { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} |
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,31 @@ | ||
// Copyright 2022 Authors of spidernet-io | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package slice | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestRemoveElement(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
in []string | ||
el string | ||
expect []string | ||
}{ | ||
{"when included", []string{"a", "b", "c"}, "a", []string{"b", "c"}}, | ||
{"when not included", []string{"a", "b", "c"}, "d", []string{"a", "b", "c"}}, | ||
{"when empty slice", []string{}, "a", []string{}}, | ||
} | ||
|
||
for _, v := range cases { | ||
t.Run(v.name, func(t *testing.T) { | ||
out := RemoveElement[string](v.in, v.el) | ||
if !reflect.DeepEqual(out, v.expect) { | ||
t.Errorf("RemoveElement() got = %v, want = %v", out, v.expect) | ||
} | ||
}) | ||
} | ||
} |
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
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