1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
package funk
import (
"fmt"
"reflect"
)
// ForEach iterates over elements of collection and invokes iteratee
// for each element.
func ForEach(arr interface{}, predicate interface{}) {
if !IsIteratee(arr) {
panic("First parameter must be an iteratee")
}
var (
funcValue = reflect.ValueOf(predicate)
arrValue = reflect.ValueOf(arr)
arrType = arrValue.Type()
funcType = funcValue.Type()
)
if arrType.Kind() == reflect.Slice || arrType.Kind() == reflect.Array {
if !IsFunction(predicate, 1, 0) {
panic("Second argument must be a function with one parameter")
}
arrElemType := arrValue.Type().Elem()
arrElemPointerType := reflect.New(arrElemType).Type()
usePointer := arrElemPointerType.ConvertibleTo(funcType.In(0))
// Checking whether element type is convertible to function's first argument's type.
if !arrElemType.ConvertibleTo(funcType.In(0)) && !usePointer {
panic("Map function's argument is not compatible with type of array.")
}
for i := 0; i < arrValue.Len(); i++ {
if usePointer {
funcValue.Call([]reflect.Value{arrValue.Index(i).Addr()})
} else {
funcValue.Call([]reflect.Value{arrValue.Index(i)})
}
}
}
if arrType.Kind() == reflect.Map {
if !IsFunction(predicate, 2, 0) {
panic("Second argument must be a function with two parameters")
}
// Type checking for Map<key, value> = (key, value)
keyType := arrType.Key()
valueType := arrType.Elem()
if !keyType.ConvertibleTo(funcType.In(0)) {
panic(fmt.Sprintf("function first argument is not compatible with %s", keyType.String()))
}
if !valueType.ConvertibleTo(funcType.In(1)) {
panic(fmt.Sprintf("function second argument is not compatible with %s", valueType.String()))
}
for _, key := range arrValue.MapKeys() {
funcValue.Call([]reflect.Value{key, arrValue.MapIndex(key)})
}
}
}
// ForEachRight iterates over elements of collection from the right and invokes iteratee
// for each element.
func ForEachRight(arr interface{}, predicate interface{}) {
if !IsIteratee(arr) {
panic("First parameter must be an iteratee")
}
var (
funcValue = reflect.ValueOf(predicate)
arrValue = reflect.ValueOf(arr)
arrType = arrValue.Type()
funcType = funcValue.Type()
)
if arrType.Kind() == reflect.Slice || arrType.Kind() == reflect.Array {
if !IsFunction(predicate, 1, 0) {
panic("Second argument must be a function with one parameter")
}
arrElemType := arrValue.Type().Elem()
arrElemPointerType := reflect.New(arrElemType).Type()
usePointer := arrElemPointerType.ConvertibleTo(funcType.In(0))
// Checking whether element type is convertible to function's first argument's type.
if !arrElemType.ConvertibleTo(funcType.In(0)) && !usePointer {
panic("Map function's argument is not compatible with type of array.")
}
for i := arrValue.Len() - 1; i >= 0; i-- {
if usePointer {
funcValue.Call([]reflect.Value{arrValue.Index(i).Addr()})
} else {
funcValue.Call([]reflect.Value{arrValue.Index(i)})
}
}
}
if arrType.Kind() == reflect.Map {
if !IsFunction(predicate, 2, 0) {
panic("Second argument must be a function with two parameters")
}
// Type checking for Map<key, value> = (key, value)
keyType := arrType.Key()
valueType := arrType.Elem()
if !keyType.ConvertibleTo(funcType.In(0)) {
panic(fmt.Sprintf("function first argument is not compatible with %s", keyType.String()))
}
if !valueType.ConvertibleTo(funcType.In(1)) {
panic(fmt.Sprintf("function second argument is not compatible with %s", valueType.String()))
}
keys := Reverse(arrValue.MapKeys()).([]reflect.Value)
for _, key := range keys {
funcValue.Call([]reflect.Value{key, arrValue.MapIndex(key)})
}
}
}
// Head gets the first element of array.
func Head(arr interface{}) interface{} {
value := redirectValue(reflect.ValueOf(arr))
valueType := value.Type()
kind := value.Kind()
if kind == reflect.Array || kind == reflect.Slice {
if value.Len() == 0 {
return nil
}
return value.Index(0).Interface()
}
panic(fmt.Sprintf("Type %s is not supported by Head", valueType.String()))
}
// Last gets the last element of array.
func Last(arr interface{}) interface{} {
value := redirectValue(reflect.ValueOf(arr))
valueType := value.Type()
kind := value.Kind()
if kind == reflect.Array || kind == reflect.Slice {
if value.Len() == 0 {
return nil
}
return value.Index(value.Len() - 1).Interface()
}
panic(fmt.Sprintf("Type %s is not supported by Last", valueType.String()))
}
// Initial gets all but the last element of array.
func Initial(arr interface{}) interface{} {
value := redirectValue(reflect.ValueOf(arr))
valueType := value.Type()
kind := value.Kind()
if kind == reflect.Array || kind == reflect.Slice {
length := value.Len()
if length <= 1 {
return arr
}
return value.Slice(0, length-1).Interface()
}
panic(fmt.Sprintf("Type %s is not supported by Initial", valueType.String()))
}
// Tail gets all but the first element of array.
func Tail(arr interface{}) interface{} {
value := redirectValue(reflect.ValueOf(arr))
valueType := value.Type()
kind := value.Kind()
if kind == reflect.Array || kind == reflect.Slice {
length := value.Len()
if length <= 1 {
return arr
}
return value.Slice(1, length).Interface()
}
panic(fmt.Sprintf("Type %s is not supported by Initial", valueType.String()))
}
|