Conditions | 5 |
Total Lines | 21 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | package internal |
||
16 | func Reduce(a interface{}, f func(accumulator interface{}, val interface{}, i int) interface{}, accumulator interface{}) (interface{}, error) { |
||
17 | val := reflect.ValueOf(a) |
||
18 | switch val.Kind() { |
||
19 | case reflect.Slice, reflect.Array: |
||
20 | length := val.Len() |
||
21 | index := -1 |
||
22 | newSlice := copySlice(a) |
||
23 | if IsAssigned(accumulator) && length > 0 { |
||
24 | index++ |
||
25 | accumulator = newSlice.Index(index).Interface() |
||
26 | } |
||
27 | index++ |
||
28 | for index < length { |
||
29 | |||
30 | rowVal := val.Index(index) |
||
31 | accumulator = f(accumulator, rowVal.Interface(), index) |
||
32 | index++ |
||
33 | } |
||
34 | return accumulator, nil |
||
35 | } |
||
36 | return nil, errs.NewUnsupportedTypeError(val.Kind().String()) |
||
37 | } |
||
38 |