Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ func EachMap[K Comparable, V any](in map[K]V, call func(key K, value V)) {
}
}

func ConvertMap[K1, K2 comparable, V1, V2 any](in map[K1]V1, call func(key K1, value V1) (K2, V2)) (out map[K2]V2) {
out = make(map[K2]V2, len(in))
for k1, v1 := range in {
k2, v2 := call(k1, v1)
out[k2] = v2
}
return
}

func FilterMap[K comparable, V any](in map[K]V, filter func(key K, value V) bool) (out map[K]V) {
out = make(map[K]V, len(in))
for k, v := range in {
Expand Down
8 changes: 8 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ func Each[T any](in []T, call func(value T, index int)) {
}
}

func Convert[T, V any](in []T, call func(value T, index int) V) (out []V) {
out = make([]V, len(in))
for i, v := range in {
out[i] = call(v, i)
}
return
}

func Join[T any](in ...[]T) (out []T) {
size := 0
for _, m := range in {
Expand Down