1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 23:22:28 +08:00

feat: add ListToMap for list

This commit is contained in:
dudaodong
2023-02-06 11:06:46 +08:00
parent 1fe4cdc429
commit c35bda6a65
2 changed files with 24 additions and 0 deletions

View File

@@ -405,3 +405,14 @@ func (l *List[T]) batchRemove(list *List[T], complement bool) bool {
func (l *List[T]) Iterator() iterator.Iterator[T] {
return iterator.FromSlice(l.data)
}
// ToMap convert a list to a map based on iteratee function.
func ListToMap[T any, K comparable, V any](list *List[T], iteratee func(T) (K, V)) map[K]V {
result := make(map[K]V, list.Size())
for _, item := range list.data {
k, v := iteratee(item)
result[k] = v
}
return result
}