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

     1  package isc
     2  
     3  /// functions for list
     4  
     5  func ListMap[T any, R any](list []T, f func(T) R) []R {
     6  	var n []R
     7  	for _, e := range list {
     8  		n = append(n, f(e))
     9  	}
    10  	return n
    11  }
    12  
    13  func ListMapNotNull[T any, R any](list []*T, f func(T) R) []R {
    14  	var n []R
    15  	for _, e := range list {
    16  		if e != nil {
    17  			n = append(n, f(*e))
    18  		}
    19  	}
    20  	return n
    21  }
    22  
    23  func ListMapIndexed[T any, R any](list []T, f func(int, T) R) []R {
    24  	var n []R
    25  	for i, e := range list {
    26  		n = append(n, f(i, e))
    27  	}
    28  	return n
    29  }
    30  
    31  func ListMapIndexedNotNull[T any, R any](list []*T, f func(int, T) R) []R {
    32  	var n []R
    33  	for i, e := range list {
    34  		if e != nil {
    35  			n = append(n, f(i, *e))
    36  		}
    37  	}
    38  	return n
    39  }
    40  
    41  func ListMapTo[T any, R any](list []T, dest *[]R, f func(T) R) []R {
    42  	var n []R
    43  	for _, e := range list {
    44  		item := f(e)
    45  		*dest = append(*dest, item)
    46  		n = append(n, item)
    47  	}
    48  	return n
    49  }
    50  
    51  func ListMapIndexedTo[T any, R any](list []T, dest *[]R, f func(int, T) R) []R {
    52  	var n []R
    53  	for i, e := range list {
    54  		item := f(i, e)
    55  		*dest = append(*dest, item)
    56  		n = append(n, item)
    57  	}
    58  	return n
    59  }
    60  
    61  func ListMapNotNullTo[T any, R any](list []*T, dest *[]R, f func(T) R) []R {
    62  	var n []R
    63  	for _, e := range list {
    64  		if e != nil {
    65  			item := f(*e)
    66  			*dest = append(*dest, item)
    67  			n = append(n, item)
    68  		}
    69  	}
    70  	return n
    71  }
    72  
    73  func ListMapIndexedNotNullTo[T any, R any](list []*T, dest *[]R, f func(int, T) R) []R {
    74  	var n []R
    75  	for i, e := range list {
    76  		if e != nil {
    77  			item := f(i, *e)
    78  			*dest = append(*dest, item)
    79  			n = append(n, item)
    80  		}
    81  	}
    82  	return n
    83  }
    84  
    85  /// functions for map
    86  
    87  func MapMap[K comparable, V any, R any](m map[K]V, f func(K, V) R) []R {
    88  	var n []R
    89  	for k, v := range m {
    90  		n = append(n, f(k, v))
    91  	}
    92  	return n
    93  }
    94  
    95  func MapMapNotNull[K comparable, V any, R any](m map[K]*V, f func(K, V) R) []R {
    96  	var n []R
    97  	for k, v := range m {
    98  		if v != nil {
    99  			n = append(n, f(k, *v))
   100  		}
   101  	}
   102  	return n
   103  }
   104  
   105  func MapMapTo[K comparable, V any, R any](m map[K]V, dest *[]R, f func(K, V) R) []R {
   106  	var n []R
   107  	for k, v := range m {
   108  		item := f(k, v)
   109  		*dest = append(*dest, item)
   110  		n = append(n, item)
   111  	}
   112  	return n
   113  }
   114  
   115  func MapMapNotNullTo[K comparable, V any, R any](m map[K]*V, dest *[]R, f func(K, V) R) []R {
   116  	var n []R
   117  	for k, v := range m {
   118  		if v != nil {
   119  			item := f(k, *v)
   120  			*dest = append(*dest, item)
   121  			n = append(n, item)
   122  		}
   123  	}
   124  	return n
   125  }