-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.go
69 lines (55 loc) · 1.67 KB
/
map.go
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
package functools
import (
"errors"
"reflect"
)
/*
'Map' applies 'func' to every item of iterable 'slice' and return a slice of the results.
Function allowes slices and arrays.
Map(func, slice) slice
MapSafe(func, slice) (slice, err)
*/
func Apply(function, slice interface{}) interface{} {
return _map(function, slice)
}
func ApplySafe(function, slice interface{}) (result interface{}, err error) {
defer except(&err)
result = _map(function, slice)
return
}
func Map(function, slice interface{}) interface{} {
return _map(function, slice)
}
func MapSafe(function, slice interface{}) (result interface{}, err error) {
defer except(&err)
result = _map(function, slice)
return
}
func _map(function, slice interface{}) interface{} { // Resolved conflict with built-in map statement
rv := reflect.ValueOf(slice)
if rv.Kind() != reflect.Slice && rv.Kind() != reflect.Array {
raise(errors.New("The passed collection is not a slice or an array"), "Map")
}
fn := reflect.ValueOf(function)
t := rv.Type().Elem()
if !verifyMapFuncType(fn, t) {
raise(errors.New("Function must be of type func("+t.String()+
") interface{} or func(interface{}) interface{}"), "Map")
}
var param [1]reflect.Value
out := reflect.MakeSlice(reflect.SliceOf(fn.Type().Out(0)), 0, rv.Len())
for i := 0; i < rv.Len(); i++ {
param[0] = rv.Index(i)
out = reflect.Append(out, fn.Call(param[:])[0])
}
return out.Interface()
}
func verifyMapFuncType(fn reflect.Value, elType reflect.Type) bool {
if fn.Kind() != reflect.Func {
return false
}
if fn.Type().NumIn() != 1 || fn.Type().NumOut() != 1 {
return false
}
return fn.Type().In(0).Kind() == reflect.Interface || fn.Type().In(0) == elType
}