-
Notifications
You must be signed in to change notification settings - Fork 32
/
distinct.go
44 lines (40 loc) · 1 KB
/
distinct.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
package underscore
import "reflect"
func (m enumerable) Distinct(selector interface{}) IEnumerable {
if selector == nil {
selector = func(value, _ interface{}) facade {
return facade{
reflect.ValueOf(value),
}
}
}
return enumerable{
Enumerator: func() IEnumerator {
iterator := m.GetEnumerator()
selectValue := reflect.ValueOf(selector)
set := make(map[interface{}]bool)
return &enumerator{
MoveNextFunc: func() (valueValue reflect.Value, keyValue reflect.Value, ok bool) {
for ok = iterator.MoveNext(); ok; ok = iterator.MoveNext() {
valueValue = iterator.GetValue()
keyValue = iterator.GetKey()
v := getReturnValue(selectValue, iterator).Interface()
if _, has := set[v]; !has {
set[v] = true
return
}
}
return
},
}
},
}
}
func (m enumerable) DistinctBy(fieldName string) IEnumerable {
getter := FieldValue(fieldName)
return m.Distinct(func(value, _ interface{}) facade {
return facade{
getter(value),
}
})
}