-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: support generics,add new feature
- `Reduce` - `Chunk` - `Map` - `Filter` - GroupBy
- Loading branch information
1 parent
fd52422
commit 868682a
Showing
5 changed files
with
185 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package utils | ||
|
||
|
||
func Chunk[T any](slice []T, size int) [][]T { | ||
var chunks [][]T | ||
for i := 0; i < len(slice); i += size { | ||
end := i + size | ||
if end > len(slice) { | ||
end = len(slice) | ||
} | ||
chunks = append(chunks, slice[i:end]) | ||
} | ||
return chunks | ||
} | ||
|
||
|
||
func Reduce[T any](slice []T, reducer func(T, T) T, initialValue T) T { | ||
result := initialValue | ||
|
||
for _, val := range slice { | ||
result = reducer(result, val) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func Map[T any](slice []T, mapper func(T) T) []T { | ||
result := make([]T, len(slice)) | ||
|
||
for i, val := range slice { | ||
result[i] = mapper(val) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func Filter[T any](slice []T, predicate func(T) bool) []T { | ||
var result []T | ||
|
||
for _, val := range slice { | ||
if predicate(val) { | ||
result = append(result, val) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
func PadEnd[T any](slice []T, targetLength int, padValue T) []T { | ||
currentLength := len(slice) | ||
paddingLength := targetLength - currentLength | ||
if paddingLength <= 0 { | ||
return slice | ||
} | ||
newSlice := make([]T, 0,paddingLength) | ||
for i := 0; i < paddingLength; i++ { | ||
newSlice = append(newSlice, padValue) | ||
} | ||
newSlice = append(slice, newSlice...) | ||
|
||
return newSlice | ||
} | ||
|
||
func PadStart[T any](slice []T, targetLength int, padValue T) []T { | ||
currentLength := len(slice) | ||
paddingLength := targetLength - currentLength | ||
if paddingLength <= 0 { | ||
return slice | ||
} | ||
|
||
newSlice := make([]T, 0, paddingLength) | ||
for i := 0; i < paddingLength; i++ { | ||
newSlice = append(newSlice, padValue) | ||
} | ||
|
||
newSlice = append(newSlice, slice...) | ||
|
||
return newSlice | ||
} | ||
|
||
func GroupBy[T any, K comparable](slice []T, getKey func(T) K) map[K][]T { | ||
groups := make(map[K][]T) | ||
|
||
for _, item := range slice { | ||
key := getKey(item) | ||
groups[key] = append(groups[key], item) | ||
} | ||
|
||
return groups | ||
} |
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,23 @@ | ||
package utils | ||
type Toggler[T any] interface { | ||
Switch() T | ||
Value() T | ||
} | ||
|
||
type toggleValue[T any] struct { | ||
values []T | ||
currentIdx int | ||
} | ||
|
||
func (t *toggleValue[T]) Switch() T { | ||
t.currentIdx = (t.currentIdx + 1) % len(t.values) | ||
return t.Value() | ||
} | ||
|
||
func (t *toggleValue[T]) Value() T { | ||
return t.values[t.currentIdx] | ||
} | ||
|
||
func UseToggle[T any](value ...T) Toggler[T] { | ||
return &toggleValue[T]{values: value, currentIdx: 0} | ||
} |
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