github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/isc/group.go (about)

     1  package isc
     2  
     3  //GroupBy Groups elements of the original collection by the key returned by the given keySelector function applied to each element and returns a map where each group key is associated with a list of corresponding elements.
     4  //The returned map preserves the entry iteration order of the keys produced from the original collection.
     5  func GroupBy[T any, K comparable](list []T, keySelector func(T) K) (destination map[K][]T) {
     6  	dest := make(map[K][]T)
     7  	return GroupByTo(list, &dest, keySelector)
     8  }
     9  
    10  //GroupByTransform Groups values returned by the trans function applied to each element of the original collection
    11  //by the key returned by the given keySelector function applied to the element and puts to a map
    12  //each group key associated with a list of corresponding values.
    13  func GroupByTransform[T any, K comparable, V any](list []T, keySelector func(T) K, trans func(T) V) map[K][]V {
    14  	dest := make(map[K][]V)
    15  	return GroupByTransformTo(list, &dest, keySelector, trans)
    16  	//var r = make(map[K][]V)
    17  	//for _, e := range list {
    18  	//	key := keySelector(e)
    19  	//	sl, _ := r[key]
    20  	//	sl = append(sl, trans(e))
    21  	//	r[key] = sl
    22  	//}
    23  	//return r
    24  }
    25  
    26  //GroupByTo Groups elements of the original collection by the key returned by the given keySelector function applied
    27  //to each element and puts to the dest map each group key associated with a list of corresponding elements.
    28  //Returns: The dest map‘s val
    29  func GroupByTo[T any, K comparable](list []T, dest *map[K][]T, keySelector func(T) K) (destination map[K][]T) {
    30  	r := make(map[K][]T)
    31  	if *dest == nil {
    32  		return r
    33  	}
    34  	for _, e := range list {
    35  		key := keySelector(e)
    36  		sl, _ := r[key]
    37  		sl = append(sl, e)
    38  		r[key] = sl
    39  		sll, _ := (*dest)[key]
    40  		sll = append(sll, e)
    41  		(*dest)[key] = sll
    42  	}
    43  	return r
    44  }
    45  
    46  //GroupByTransformTo  Groups values returned by the trans function applied to each element of the original collection
    47  //by the key returned by the given keySelector function applied to the element and puts to the dest map
    48  //each group key associated with a list of corresponding values.
    49  //Returns: The dest map's val.
    50  func GroupByTransformTo[T any, K comparable, V any](list []T, dest *map[K][]V, keySelector func(T) K, trans func(T) V) map[K][]V {
    51  	r := make(map[K][]V)
    52  	for _, e := range list {
    53  		key := keySelector(e)
    54  		value := trans(e)
    55  		sl, _ := r[key]
    56  		sl = append(sl, value)
    57  		r[key] = sl
    58  		sll, _ := (*dest)[key]
    59  		sll = append(sll, value)
    60  		(*dest)[key] = sll
    61  	}
    62  	return r
    63  }