-
Notifications
You must be signed in to change notification settings - Fork 29
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
1 parent
515cbc6
commit 7086281
Showing
1 changed file
with
25 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,25 @@ | ||
package sher | ||
|
||
// MappingFromSlice 将切片中的元素进行转换 | ||
func MappingFromSlice[S ~[]V, NS ~[]N, V, N any](slice S, handler func(value V) N) NS { | ||
if slice == nil { | ||
return nil | ||
} | ||
result := make(NS, len(slice)) | ||
for i, v := range slice { | ||
result[i] = handler(v) | ||
} | ||
return result | ||
} | ||
|
||
// MappingFromMap 将 map 中的元素进行转换 | ||
func MappingFromMap[M ~map[K]V, NM ~map[K]N, K comparable, V, N any](m M, handler func(value V) N) NM { | ||
if m == nil { | ||
return nil | ||
} | ||
result := make(NM, len(m)) | ||
for k, v := range m { | ||
result[k] = handler(v) | ||
} | ||
return result | ||
} |