github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/isc/associate.go (about) 1 package isc 2 3 //Associate Returns a Map containing key-value map provided by transform function applied to elements of the given collection. 4 //If any of two map would have the same key the last one gets added to the map. 5 //The returned map preserves the entry iteration order of the original collection. 6 func Associate[T any, K comparable, V any](list []T, transform func(T) Pair[K, V]) map[K]V { 7 r := make(map[K]V) 8 return AssociateTo(list, &r, transform) 9 } 10 11 //AssociateTo Populates and returns the destination map with key-value pairs provided by transform function applied to each element of the given collection. 12 //If any of two pairs would have the same key the last one gets added to the map. 13 func AssociateTo[T any, K comparable, V any](list []T, destination *map[K]V, transform func(T) Pair[K, V]) map[K]V { 14 for _, e := range list { 15 item := transform(e) 16 (*destination)[item.First] = item.Second 17 } 18 return *destination 19 } 20 21 //AssociateBy Returns a Map containing the elements from the given collection indexed by the key returned from keySelector function applied to each element. 22 //If any two elements would have the same key returned by keySelector the last one gets added to the map. 23 //The returned map preserves the entry iteration order of the original collection. 24 func AssociateBy[T any, K comparable](list []T, keySelector func(T) K) map[K]T { 25 r := make(map[K]T) 26 for _, e := range list { 27 r[keySelector(e)] = e 28 } 29 return r 30 } 31 32 //AssociateByAndValue Returns a Map containing the values provided by valueTransform and indexed by keySelector functions applied to elements of the given collection. 33 //If any two elements would have the same key returned by keySelector the last one gets added to the map. 34 //The returned map preserves the entry iteration order of the original collection. 35 func AssociateByAndValue[T any, V any, K comparable](list []T, keySelector func(T) K, valueTransform func(T) V) map[K]V { 36 r := make(map[K]V) 37 for _, e := range list { 38 r[keySelector(e)] = valueTransform(e) 39 } 40 return r 41 } 42 43 // AssociateByTo Populates and returns the destination mutable map with key-value pairs, where key is provided by the keySelector function applied to each element of the given collection and value is the element itself. 44 //If any two elements would have the same key returned by keySelector the last one gets added to the map 45 func AssociateByTo[T any, K comparable](list []T, destination *map[K]T, keySelector func(T) K) map[K]T { 46 for _, e := range list { 47 (*destination)[keySelector(e)] = e 48 } 49 return *destination 50 } 51 52 // AssociateByAndValueTo Populates and returns the destination mutable map with key-value pairs, where key is provided by the keySelector function applied to each element of the given collection and value is the element itself. 53 //If any two elements would have the same key returned by keySelector the last one gets added to the map 54 func AssociateByAndValueTo[T, V any, K comparable](list []T, destination *map[K]V, keySelector func(T) K, valueTransform func(T) V) map[K]V { 55 for _, e := range list { 56 (*destination)[keySelector(e)] = valueTransform(e) 57 } 58 return *destination 59 } 60 61 //AssociateWith Returns a Map where keys are elements from the given collection and values are produced by the valueSelector function applied to each element. 62 //If any two elements are equal, the last one gets added to the map. 63 //The returned map preserves the entry iteration order of the original collection. 64 func AssociateWith[T comparable, V any](list []T, valueSelector func(T) V) map[T]V { 65 destination := make(map[T]V) 66 for _, e := range list { 67 destination[e] = valueSelector(e) 68 } 69 return destination 70 } 71 72 //AssociateWithTo Populates and returns the destination mutable map with key-value pairs for each element of the given collection, where key is the element itself and value is provided by the valueSelector function applied to that key. 73 //If any two elements are equal, the last one overwrites the former value in the map. 74 func AssociateWithTo[T comparable, V any](list []T, destination *map[T]V, valueSelector func(T) V) map[T]V { 75 for _, e := range list { 76 (*destination)[e] = valueSelector(e) 77 } 78 return *destination 79 }