Moved stuff, added Contains to sliceutils

This commit is contained in:
mStar aka a person 2024-01-17 09:47:33 +01:00
parent 94e1712122
commit ae2da5efa5
10 changed files with 21 additions and 43 deletions

8
.idea/.gitignore vendored
View file

@ -1,8 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View file

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View file

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/goutils.iml" filepath="$PROJECT_DIR$/.idea/goutils.iml" />
</modules>
</component>
</project>

View file

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View file

@ -1,8 +1,8 @@
package sliceutils
// MapSlice applies a given function to every element of a slice.
// MapS applies a given function to every element of a slice.
// The return type may be different from the initial type of the slice.
func MapSlice[T any, M any](arr []T, apply func(T) M) []M {
func Map[T any, M any](arr []T, apply func(T) M) []M {
n := make([]M, len(arr))
for i, e := range arr {
n[i] = apply(e)
@ -10,9 +10,9 @@ func MapSlice[T any, M any](arr []T, apply func(T) M) []M {
return n
}
// FilterSlice filters a slice using a given function.
// Filter filters a slice using a given function.
// If the filter function returns true, the element stays, otherwise it gets removed.
func FilterSlice[T any](arr []T, filter func(T) bool) []T {
func Filter[T any](arr []T, filter func(T) bool) []T {
n := make([]T, 0)
for _, e := range arr {
if filter(e) {
@ -22,8 +22,8 @@ func FilterSlice[T any](arr []T, filter func(T) bool) []T {
return n
}
// RemoveDuplicateSlice removes all duplicates inside a slice.
func RemoveDuplicateSlice[T comparable](sliceList []T) []T {
// RemoveDuplicate removes all duplicates inside a slice.
func RemoveDuplicate[T comparable](sliceList []T) []T {
allKeys := make(map[T]bool)
list := []T{}
for _, item := range sliceList {
@ -35,15 +35,15 @@ func RemoveDuplicateSlice[T comparable](sliceList []T) []T {
return list
}
// ReverseSlice reverses a given slice.
func ReverseSlice[S ~[]E, E any](s S) {
// Reverse reverses a given slice.
func Reverse[E any](s []E) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
// CompareOrderedSlice compares two slices for both element equality and element order.
func CompareOrderedSlice[T comparable](a, b []T) bool {
// CompareOrdered compares two slices for both element equality and element order.
func CompareOrdered[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
}
@ -55,9 +55,9 @@ func CompareOrderedSlice[T comparable](a, b []T) bool {
return true
}
// CompareUnorderedSlice compares two slices for element equality.
// CompareUnorderedS compares two slices for element equality.
// The order of those elements does not matter.
func CompareUnorderedSlice[T comparable](a, b []T) bool {
func CompareUnordered[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
}
@ -71,3 +71,12 @@ func CompareUnorderedSlice[T comparable](a, b []T) bool {
}
return hits == len(a)
}
func Contains[T comparable](a []T, b T) bool {
for _, v := range a {
if v == b {
return true
}
}
return false
}