github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/utils/funcitr/funcitr.go (about) 1 // Copyright 2019 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package funcitr 16 17 // MapStrings iterates over a slice of strings calling the mapping function for each value 18 // in the slice. The mapped values are returned in a new slice, and their order corresponds 19 // with the input slice (The Nth item in the output slice is the result returned by the mapping 20 // function when given the Nth item from the input slice.) 21 func MapStrings(strings []string, mapFunc func(string) string) []string { 22 if strings == nil { 23 return nil 24 } 25 26 results := make([]string, len(strings)) 27 28 for i, str := range strings { 29 results[i] = mapFunc(str) 30 } 31 32 return results 33 } 34 35 // FilterStrings iterates over a slice of strings calling the filter function for each value 36 // in the slice. The output slice contains contains all input strings for which the filter 37 // function returned true. Order is maintained from input to output. 38 func FilterStrings(strings []string, filter func(string) bool) []string { 39 if strings == nil { 40 return nil 41 } 42 43 results := make([]string, 0, len(strings)) 44 45 for _, str := range strings { 46 if filter(str) { 47 results = append(results, str) 48 } 49 } 50 51 return results 52 } 53 54 // MapSlice iterates over a slice of calling the mapping function for each value in the 55 // slice. The mapped values are returned in a new slice, and their order corresponds with 56 // the input slice (The Nth item in the output slice is the result returned by the mapping 57 // function when given the Nth item from the input slice.) 58 func MapSlice(sl []interface{}, mapFunc func(interface{}) interface{}) []interface{} { 59 if sl == nil { 60 return nil 61 } 62 63 results := make([]interface{}, len(sl)) 64 for i, item := range sl { 65 results[i] = mapFunc(item) 66 } 67 68 return results 69 } 70 71 // MapFloat64s iterates over a slice of float64s calling the mapping function for each value 72 // in the slice. The mapped values are returned in a new slice, and their order corresponds 73 // with the input slice (The Nth item in the output slice is the result returned by the mapping 74 // function when given the Nth item from the input slice.) 75 func MapFloat64s(floats []float64, mapFunc func(float64) float64) []float64 { 76 if floats == nil { 77 return nil 78 } 79 80 results := make([]float64, len(floats)) 81 82 for i, fl := range floats { 83 results[i] = mapFunc(fl) 84 } 85 86 return results 87 } 88 89 // MapFloat32s iterates over a slice of floats calling the mapping function for each value 90 // in the slice. The mapped values are returned in a new slice, and their order corresponds 91 // with the input slice (The Nth item in the output slice is the result returned by the mapping 92 // function when given the Nth item from the input slice.) 93 func MapFloat32s(floats []float32, mapFunc func(float32) float32) []float32 { 94 if floats == nil { 95 return nil 96 } 97 98 results := make([]float32, len(floats)) 99 100 for i, fl := range floats { 101 results[i] = mapFunc(fl) 102 } 103 104 return results 105 } 106 107 // MapInts iterates over a slice of ints calling the mapping function for each value 108 // in the slice. The mapped values are returned in a new slice, and their order corresponds 109 // with the input slice (The Nth item in the output slice is the result returned by the mapping 110 // function when given the Nth item from the input slice.) 111 func MapInts(ints []int, mapFunc func(int) int) []int { 112 if ints == nil { 113 return nil 114 } 115 116 results := make([]int, len(ints)) 117 118 for i, fl := range ints { 119 results[i] = mapFunc(fl) 120 } 121 122 return results 123 } 124 125 // MapInt64s iterates over a slice of int64s calling the mapping function for each value 126 // in the slice. The mapped values are returned in a new slice, and their order corresponds 127 // with the input slice (The Nth item in the output slice is the result returned by the mapping 128 // function when given the Nth item from the input slice.) 129 func MapInt64s(ints []int64, mapFunc func(int64) int64) []int64 { 130 if ints == nil { 131 return nil 132 } 133 134 results := make([]int64, len(ints)) 135 136 for i, fl := range ints { 137 results[i] = mapFunc(fl) 138 } 139 140 return results 141 }