github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/f/arrays.go (about) 1 package f 2 3 // Iterator is the function that accepts element of slice/array and its index 4 type Iterator func(interface{}, int) 5 6 // ResultIterator is the function that accepts element of slice/array and its index and returns any result 7 type ResultIterator func(interface{}, int) interface{} 8 9 // ConditionIterator is the function that accepts element of slice/array and its index and returns boolean 10 type ConditionIterator func(interface{}, int) bool 11 12 // Each iterates over the slice and apply Iterator to every item 13 func Each(array []interface{}, iterator Iterator) { 14 for index, data := range array { 15 iterator(data, index) 16 } 17 } 18 19 // Maps iterates over the slice and apply ResultIterator to every item. Returns new slice as a result. 20 func Maps(array []interface{}, iterator ResultIterator) []interface{} { 21 var result = make([]interface{}, len(array)) 22 for index, data := range array { 23 result[index] = iterator(data, index) 24 } 25 return result 26 } 27 28 // Find iterates over the slice and apply ConditionIterator to every item. Returns first item that meet ConditionIterator or nil otherwise. 29 func Find(array []interface{}, iterator ConditionIterator) interface{} { 30 for index, data := range array { 31 if iterator(data, index) { 32 return data 33 } 34 } 35 return nil 36 } 37 38 // Filter iterates over the slice and apply ConditionIterator to every item. Returns new slice. 39 func Filter(array []interface{}, iterator ConditionIterator) []interface{} { 40 var result = make([]interface{}, 0) 41 for index, data := range array { 42 if iterator(data, index) { 43 result = append(result, data) 44 } 45 } 46 return result 47 } 48 49 // Count iterates over the slice and apply ConditionIterator to every item. Returns count of items that meets ConditionIterator. 50 func Count(array []interface{}, iterator ConditionIterator) int { 51 count := 0 52 for index, data := range array { 53 if iterator(data, index) { 54 count = count + 1 55 } 56 } 57 return count 58 } 59 60 // SplitObjects Separate objects into several size. 61 func SplitObjects(array []interface{}, size int) [][]interface{} { 62 var chunkSet [][]interface{} 63 var chunk []interface{} 64 65 for len(array) > size { 66 chunk, array = array[:size], array[size:] 67 chunkSet = append(chunkSet, chunk) 68 } 69 if len(array) > 0 { 70 chunkSet = append(chunkSet, array[:]) 71 } 72 return chunkSet 73 } 74 75 // SplitObjectMaps Separate objects into several size. 76 func SplitObjectMaps(array []map[string]interface{}, size int) [][]map[string]interface{} { 77 var chunkSet [][]map[string]interface{} 78 var chunk []map[string]interface{} 79 80 for len(array) > size { 81 chunk, array = array[:size], array[size:] 82 chunkSet = append(chunkSet, chunk) 83 } 84 if len(array) > 0 { 85 chunkSet = append(chunkSet, array[:]) 86 } 87 return chunkSet 88 } 89 90 // StringsContains Check if string value is contained in slice. 91 func StringsContains(s []string, sub string) bool { 92 for _, v := range s { 93 if v == sub { 94 return true 95 } 96 } 97 return false 98 } 99 100 // StringsExclude returns new string slice exclude sub string. 101 func StringsExclude(s []string, sub string) []string { 102 p := make([]string, 0, len(s)) 103 for _, v := range s { 104 if v == sub { 105 continue 106 } 107 p = append(p, v) 108 } 109 return p 110 }