-
Notifications
You must be signed in to change notification settings - Fork 2
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
calvin
committed
Jul 12, 2022
1 parent
f863213
commit c8073c8
Showing
3 changed files
with
138 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,10 @@ | ||
package sugar | ||
|
||
func Contains[T comparable](collection []T, element T) bool { | ||
for _, t := range collection { | ||
if t == element { | ||
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,122 @@ | ||
package sugar | ||
|
||
func Keys[K comparable, V any](in map[K]V) []K { | ||
result := make([]K, 0, len(in)) | ||
|
||
for k := range in { | ||
result = append(result, k) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func Values[K comparable, V any](in map[K]V) []V { | ||
result := make([]V, 0, len(in)) | ||
|
||
for _, v := range in { | ||
result = append(result, v) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func FiltrateBy[K comparable, V any](in map[K]V, fileter func(K, V) bool) map[K]V { | ||
result := map[K]V{} | ||
|
||
for k, v := range in { | ||
if fileter(k, v) { | ||
result[k] = v | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
func FiltrateByKeys[K comparable, V any](in map[K]V, keys []K) map[K]V { | ||
result := map[K]V{} | ||
|
||
for k, v := range in { | ||
if Contains(keys, k) { | ||
result[k] = v | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
func FiltrateByValues[K comparable, V comparable](in map[K]V, values []V) map[K]V { | ||
result := map[K]V{} | ||
|
||
for k, v := range in { | ||
if Contains(values, v) { | ||
result[k] = v | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
func MapToEntries[K comparable, V any](in map[K]V) []Entry[K, V] { | ||
result := make([]Entry[K, V], 0, len(in)) | ||
|
||
for k, v := range in { | ||
result = append(result, Entry[K, V]{k, v}) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func EntriesToMap[K comparable, V any](entries []Entry[K, V]) map[K]V { | ||
result := map[K]V{} | ||
|
||
for _, entry := range entries { | ||
result[entry.Key] = entry.Value | ||
} | ||
|
||
return result | ||
} | ||
|
||
func Invert[K comparable, V comparable](in map[K]V) map[V]K { | ||
result := map[V]K{} | ||
|
||
for k, v := range in { | ||
result[v] = k | ||
} | ||
|
||
return result | ||
} | ||
|
||
// Assign merges multiple maps from left to right. | ||
func Assign[K comparable, V any](maps ...map[K]V) map[K]V { | ||
result := map[K]V{} | ||
|
||
for _, m := range maps { | ||
for k, v := range m { | ||
result[k] = v | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
// MapUpdateKeys manipulates a map keys and transforms it to a map of another type. | ||
func MapUpdateKeys[K comparable, V any, R comparable](in map[K]V, iteratee func(K, V) R) map[R]V { | ||
result := map[R]V{} | ||
|
||
for k, v := range in { | ||
result[iteratee(k, v)] = v | ||
} | ||
|
||
return result | ||
} | ||
|
||
// MapUpdateValues manipulates a map values and transforms it to a map of another type. | ||
func MapUpdateValues[K comparable, V any, R any](in map[K]V, iteratee func(K, V) R) map[K]R { | ||
result := map[K]R{} | ||
|
||
for k, v := range in { | ||
result[k] = iteratee(k, v) | ||
} | ||
|
||
return result | ||
} |
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,6 @@ | ||
package sugar | ||
|
||
type Entry[K comparable, V any] struct { | ||
Key K | ||
Value V | ||
} |