-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
151 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package slice | ||
|
||
func Contains(sl []interface{}, v interface{}) bool { | ||
for _, vv := range sl { | ||
if vv == v { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func ContainsInt(sl []int, v int) bool { | ||
for _, vv := range sl { | ||
if vv == v { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func ContainsInt64(sl []int64, v int64) bool { | ||
for _, vv := range sl { | ||
if vv == v { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func ContainsString(sl []string, v string) bool { | ||
for _, vv := range sl { | ||
if vv == v { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,22 @@ | ||
package slice | ||
|
||
// SliceMerge merges interface slices to one slice. | ||
func Merge(slice1, slice2 []interface{}) (c []interface{}) { | ||
c = append(slice1, slice2...) | ||
return | ||
} | ||
|
||
func MergeInt(slice1, slice2 []int) (c []int) { | ||
c = append(slice1, slice2...) | ||
return | ||
} | ||
|
||
func MergeInt64(slice1, slice2 []int64) (c []int64) { | ||
c = append(slice1, slice2...) | ||
return | ||
} | ||
|
||
func MergeString(slice1, slice2 []string) (c []string) { | ||
c = append(slice1, slice2...) | ||
return | ||
} |
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,22 @@ | ||
package slice | ||
|
||
func SumInt64(s []int64) (sum int64) { | ||
for _, v := range s { | ||
sum += v | ||
} | ||
return | ||
} | ||
|
||
func SumInt(s []int) (sum int) { | ||
for _, v := range s { | ||
sum += v | ||
} | ||
return | ||
} | ||
|
||
func SumFloat64(s []float64) (sum float64) { | ||
for _, v := range s { | ||
sum += v | ||
} | ||
return | ||
} |
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,70 @@ | ||
package slice | ||
|
||
func UniqueInt64(s []int64) []int64 { | ||
size := len(s) | ||
if size == 0 { | ||
return []int64{} | ||
} | ||
|
||
m := make(map[int64]struct{}) | ||
for i := 0; i < size; i++ { | ||
m[s[i]] = struct{}{} | ||
} | ||
|
||
realLen := len(m) | ||
ret := make([]int64, realLen) | ||
|
||
idx := 0 | ||
for key := range m { | ||
ret[idx] = key | ||
idx++ | ||
} | ||
|
||
return ret | ||
} | ||
|
||
func UniqueInt(s []int) []int { | ||
size := len(s) | ||
if size == 0 { | ||
return []int{} | ||
} | ||
|
||
m := make(map[int]struct{}) | ||
for i := 0; i < size; i++ { | ||
m[s[i]] = struct{}{} | ||
} | ||
|
||
realLen := len(m) | ||
ret := make([]int, realLen) | ||
|
||
idx := 0 | ||
for key := range m { | ||
ret[idx] = key | ||
idx++ | ||
} | ||
|
||
return ret | ||
} | ||
|
||
func UniqueString(s []string) []string { | ||
size := len(s) | ||
if size == 0 { | ||
return []string{} | ||
} | ||
|
||
m := make(map[string]struct{}) | ||
for i := 0; i < size; i++ { | ||
m[s[i]] = struct{}{} | ||
} | ||
|
||
realLen := len(m) | ||
ret := make([]string, realLen) | ||
|
||
idx := 0 | ||
for key := range m { | ||
ret[idx] = key | ||
idx++ | ||
} | ||
|
||
return ret | ||
} |