Skip to content

Commit

Permalink
map util function library
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin committed Jul 12, 2022
1 parent f863213 commit c8073c8
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
10 changes: 10 additions & 0 deletions intersect.go
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
}
122 changes: 122 additions & 0 deletions map.go
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
}
6 changes: 6 additions & 0 deletions tuple.go
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
}

0 comments on commit c8073c8

Please sign in to comment.