forked from tobyhede/go-underscore
/
partition.go
50 lines (39 loc) · 1.02 KB
/
partition.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
package un
import (
"reflect"
)
func init() {
MakePartition(&Partition)
MakePartition(&PartitionInt)
}
/**
Partition func([]A, func(A) bool) ([]A []A)
**/
var Partition func(interface{}, func(interface{}) bool) ([]interface{}, []interface{})
var PartitionInt func([]int, func(int) bool) ([]int, []int)
var PartitionString func([]string, func(string) bool) ([]string, []string)
func MakePartition(fn interface{}) {
Maker(fn, _partition)
}
func _partition(values []reflect.Value) []reflect.Value {
slice := interfaceToValue(values[0])
fn := values[1]
var t, f reflect.Value
if values[0].Kind() == reflect.Interface {
t = reflect.ValueOf(make([]interface{}, 0))
f = reflect.ValueOf(make([]interface{}, 0))
} else {
t = reflect.MakeSlice(slice.Type(), 0, 0)
f = reflect.MakeSlice(slice.Type(), 0, 0)
}
for i := 0; i < slice.Len(); i++ {
e := slice.Index(i)
r := fn.Call([]reflect.Value{e})
if r[0].Bool() {
t = reflect.Append(t, e)
} else {
f = reflect.Append(f, e)
}
}
return []reflect.Value{t, f}
}