Moved stuff, added Contains to sliceutils
This commit is contained in:
parent
94e1712122
commit
ae2da5efa5
10 changed files with 21 additions and 43 deletions
8
.idea/.gitignore
vendored
8
.idea/.gitignore
vendored
|
@ -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
|
|
|
@ -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>
|
|
|
@ -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>
|
|
|
@ -1,6 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="VcsDirectoryMappings">
|
|
||||||
<mapping directory="" vcs="Git" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
|
@ -1,8 +1,8 @@
|
||||||
package sliceutils
|
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.
|
// 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))
|
n := make([]M, len(arr))
|
||||||
for i, e := range arr {
|
for i, e := range arr {
|
||||||
n[i] = apply(e)
|
n[i] = apply(e)
|
||||||
|
@ -10,9 +10,9 @@ func MapSlice[T any, M any](arr []T, apply func(T) M) []M {
|
||||||
return n
|
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.
|
// 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)
|
n := make([]T, 0)
|
||||||
for _, e := range arr {
|
for _, e := range arr {
|
||||||
if filter(e) {
|
if filter(e) {
|
||||||
|
@ -22,8 +22,8 @@ func FilterSlice[T any](arr []T, filter func(T) bool) []T {
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveDuplicateSlice removes all duplicates inside a slice.
|
// RemoveDuplicate removes all duplicates inside a slice.
|
||||||
func RemoveDuplicateSlice[T comparable](sliceList []T) []T {
|
func RemoveDuplicate[T comparable](sliceList []T) []T {
|
||||||
allKeys := make(map[T]bool)
|
allKeys := make(map[T]bool)
|
||||||
list := []T{}
|
list := []T{}
|
||||||
for _, item := range sliceList {
|
for _, item := range sliceList {
|
||||||
|
@ -35,15 +35,15 @@ func RemoveDuplicateSlice[T comparable](sliceList []T) []T {
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReverseSlice reverses a given slice.
|
// Reverse reverses a given slice.
|
||||||
func ReverseSlice[S ~[]E, E any](s S) {
|
func Reverse[E any](s []E) {
|
||||||
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
|
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
|
||||||
s[i], s[j] = s[j], s[i]
|
s[i], s[j] = s[j], s[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CompareOrderedSlice compares two slices for both element equality and element order.
|
// CompareOrdered compares two slices for both element equality and element order.
|
||||||
func CompareOrderedSlice[T comparable](a, b []T) bool {
|
func CompareOrdered[T comparable](a, b []T) bool {
|
||||||
if len(a) != len(b) {
|
if len(a) != len(b) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -55,9 +55,9 @@ func CompareOrderedSlice[T comparable](a, b []T) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// CompareUnorderedSlice compares two slices for element equality.
|
// CompareUnorderedS compares two slices for element equality.
|
||||||
// The order of those elements does not matter.
|
// 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) {
|
if len(a) != len(b) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -71,3 +71,12 @@ func CompareUnorderedSlice[T comparable](a, b []T) bool {
|
||||||
}
|
}
|
||||||
return hits == len(a)
|
return hits == len(a)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Contains[T comparable](a []T, b T) bool {
|
||||||
|
for _, v := range a {
|
||||||
|
if v == b {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
Loading…
Reference in a new issue