-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream_chunk_by.go
More file actions
61 lines (49 loc) · 1.07 KB
/
stream_chunk_by.go
File metadata and controls
61 lines (49 loc) · 1.07 KB
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
package streamer
type chunkByStream struct {
input Iterator
chunkFn func(x interface{}) interface{}
leftoverChunkItem interface{}
lastFlag interface{}
}
func newChunkByStream(input Iterator, chunkFn func(x interface{}) interface{}) (res *chunkByStream) {
res = &chunkByStream{
input: input,
chunkFn: chunkFn,
}
return
}
func (cs *chunkByStream) Next() (interface{}, bool) {
var (
flag interface{}
flagCalculated bool
chunk []interface{}
)
if cs.lastFlag != nil {
flag = cs.lastFlag
flagCalculated = true
cs.lastFlag = nil
}
if cs.leftoverChunkItem != nil {
chunk = append(chunk, cs.leftoverChunkItem)
cs.leftoverChunkItem = nil
}
for item, ok := cs.input.Next(); ok; item, ok = cs.input.Next() {
current := item
cond := cs.chunkFn(current)
if !flagCalculated {
flag = cond
flagCalculated = true
}
if flag != cond {
cs.leftoverChunkItem = current
cs.lastFlag = cond
break
}
flag = cond
chunk = append(chunk, current)
}
if len(chunk) == 0 {
return nil, false
}
return chunk, true
}