github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/isc/listobj_ext.go (about) 1 package isc 2 3 type ISCListToMap[T any, R any] struct { 4 ISCList[T] 5 } 6 7 func ListToMapFrom[T any, R any](list ISCList[T]) ISCListToMap[T, R] { 8 return ISCListToMap[T, R]{ 9 list, 10 } 11 } 12 13 func (l ISCListToMap[T, R]) FlatMap(f func(T) []R) ISCList[R] { 14 return ListFlatMap(l.ISCList, f) 15 } 16 17 func (l ISCListToMap[T, R]) FlatMapIndexed(f func(int, T) []R) ISCList[R] { 18 return ListFlatMapIndexed(l.ISCList, f) 19 } 20 21 func (l ISCListToMap[T, R]) FlatMapTo(dest *[]R, f func(T) []R) ISCList[R] { 22 return ListFlatMapTo(l.ISCList, dest, f) 23 } 24 25 func (l ISCListToMap[T, R]) FlatMapIndexedTo(dest *[]R, f func(int, T) []R) ISCList[R] { 26 return ListFlatMapIndexedTo(l.ISCList, dest, f) 27 } 28 29 func (l ISCListToMap[T, R]) Map(f func(T) R) ISCList[R] { 30 return ListMap(l.ISCList, f) 31 } 32 33 func (l ISCListToMap[T, R]) MapIndexed(f func(int, T) R) ISCList[R] { 34 return ListMapIndexed(l.ISCList, f) 35 } 36 37 func (l ISCListToMap[T, R]) MapTo(dest *[]R, f func(T) R) ISCList[R] { 38 return ListMapTo(l.ISCList, dest, f) 39 } 40 41 func (l ISCListToMap[T, R]) MapIndexedTo(dest *[]R, f func(int, T) R) ISCList[R] { 42 return ListMapIndexedTo(l.ISCList, dest, f) 43 } 44 45 func (l ISCListToMap[T, R]) Reduce(init func(T) R, f func(R, T) R) R { 46 return Reduce(l.ISCList, init, f) 47 } 48 49 func (l ISCListToMap[T, R]) ReduceIndexed(init func(int, T) R, f func(int, R, T) R) R { 50 return ReduceIndexed(l.ISCList, init, f) 51 } 52 53 type ISCListToSlice[T any, R comparable] struct { 54 ISCList[T] 55 } 56 57 func ListToSliceFrom[T any, R comparable](list ISCList[T]) ISCListToSlice[T, R] { 58 return ISCListToSlice[T, R]{ 59 list, 60 } 61 } 62 63 func (l ISCListToSlice[T, R]) SliceContains(predicate func(T) R, key R) bool { 64 return SliceContains(l.ISCList, predicate, key) 65 } 66 67 func (l ISCListToSlice[T, R]) SliceTo(valueTransform func(T) R) ISCMap[R, T] { 68 return SliceTo(l.ISCList, valueTransform) 69 } 70 71 type ISCListToTriple[T comparable, K comparable, V any] struct { 72 ISCList[T] 73 } 74 75 func ListToTripleFrom[T comparable, K comparable, V any](list ISCList[T]) ISCListToTriple[T, K, V] { 76 return ISCListToTriple[T, K, V]{ 77 list, 78 } 79 } 80 81 func (l ISCListToTriple[T, K, V]) GroupBy(f func(T) K) map[K][]T { 82 return GroupBy(l.ISCList, f) 83 } 84 85 func (l ISCListToTriple[T, K, V]) GroupByTransform(f func(T) K, trans func(T) V) map[K][]V { 86 return GroupByTransform(l.ISCList, f, trans) 87 } 88 89 func (l ISCListToTriple[T, K, V]) GroupByTo(dest *map[K][]T, f func(T) K) map[K][]T { 90 return GroupByTo(l.ISCList, dest, f) 91 } 92 93 func (l ISCListToTriple[T, K, V]) GroupByTransformTo(dest *map[K][]V, f func(T) K, trans func(T) V) map[K][]V { 94 return GroupByTransformTo(l.ISCList, dest, f, trans) 95 } 96 97 func (l ISCListToTriple[T, K, V]) Associate(transform func(T) Pair[K, V]) ISCMap[K, V] { 98 return Associate(l.ISCList, transform) 99 } 100 101 func (l ISCListToTriple[T, K, V]) AssociateTo(destination *map[K]V, transform func(T) Pair[K, V]) ISCMap[K, V] { 102 return AssociateTo(l.ISCList, destination, transform) 103 } 104 105 func (l ISCListToTriple[T, K, V]) AssociateBy(keySelector func(T) K) ISCMap[K, T] { 106 return AssociateBy(l.ISCList, keySelector) 107 } 108 109 func (l ISCListToTriple[T, K, V]) AssociateByAndValue(keySelector func(T) K, valueTransform func(T) V) ISCMap[K, V] { 110 return AssociateByAndValue(l.ISCList, keySelector, valueTransform) 111 } 112 113 func (l ISCListToTriple[T, K, V]) AssociateByTo(destination *map[K]T, keySelector func(T) K) ISCMap[K, T] { 114 return AssociateByTo(l.ISCList, destination, keySelector) 115 } 116 117 func (l ISCListToTriple[T, K, V]) AssociateByAndValueTo(destination *map[K]V, keySelector func(T) K, valueTransform func(T) V) ISCMap[K, V] { 118 return AssociateByAndValueTo(l.ISCList, destination, keySelector, valueTransform) 119 } 120 121 func (l ISCListToTriple[T, K, V]) AssociateWith(valueSelector func(T) V) ISCMap[T, V] { 122 return AssociateWith(l.ISCList, valueSelector) 123 } 124 125 func (l ISCListToTriple[T, K, V]) AssociateWithTo(destination *map[T]V, valueSelector func(T) V) ISCMap[T, V] { 126 return AssociateWithTo(l.ISCList, destination, valueSelector) 127 } 128 129 type ISCListToPair[K comparable, V comparable] struct { 130 ISCList[Pair[K, V]] 131 } 132 133 func ListToPairFrom[K comparable, V comparable](list ISCList[Pair[K, V]]) ISCListToPair[K, V] { 134 return ISCListToPair[K, V]{ 135 list, 136 } 137 } 138 139 func ListToPairWithPairs[K comparable, V comparable](list ...Pair[K, V]) ISCListToPair[K, V] { 140 return ISCListToPair[K, V]{ 141 list, 142 } 143 } 144 145 func (l ISCListToPair[K, V]) ToMap() ISCMap[K, V] { 146 m := make(map[K]V) 147 for _, item := range l.ISCList { 148 m[item.First] = item.Second 149 } 150 return NewMapWithMap(m) 151 }