-
Notifications
You must be signed in to change notification settings - Fork 3
/
sub.go
46 lines (43 loc) · 1.12 KB
/
sub.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
package fp
import (
"reflect"
"sync"
)
func (q *stream) Sub(other Stream) Stream {
if isNilStream(other) {
return q
}
var once sync.Once
var set KVStream
getSet := func() KVStream {
once.Do(func() {
set = other.ToSet()
})
return set
}
typ := reflect.FuncOf([]reflect.Type{q.expectElemTyp}, []reflect.Type{boolType}, false)
fn := reflect.MakeFunc(typ, func(in []reflect.Value) []reflect.Value {
return []reflect.Value{reflect.ValueOf(getSet().Contains(in[0].Interface()))}
})
return q.Reject(fn.Interface())
}
func (q *stream) SubBy(other Stream, keyfn interface{}) Stream {
if isNilStream(other) {
return q
}
var once sync.Once
var set KVStream
keyfnval := reflect.ValueOf(keyfn)
getSet := func() KVStream {
once.Do(func() {
set = other.ToSetBy(keyfn)
})
return set
}
typ := reflect.FuncOf([]reflect.Type{q.expectElemTyp}, []reflect.Type{boolType}, false)
fn := reflect.MakeFunc(typ, func(in []reflect.Value) []reflect.Value {
key := keyfnval.Call([]reflect.Value{in[0]})[0].Interface()
return []reflect.Value{reflect.ValueOf(getSet().Contains(key))}
})
return q.Reject(fn.Interface())
}