-
Notifications
You must be signed in to change notification settings - Fork 0
/
concat.go
57 lines (51 loc) · 1.05 KB
/
concat.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
package gcf
type concatIterable[T any] struct {
itb1 Iterable[T]
itb2 Iterable[T]
}
type concatIterator[T any] struct {
it1 Iterator[T]
it2 Iterator[T]
iteratorItem[T]
}
// Concat makes Iterable elements concatenated of itb1 and itb2.
//
// itb1 := gcf.FromSlice([]int{1, 2, 3})
// itb2 := gcf.FromSlice([]int{4, 5, 6})
// itbc := gcf.Concat(itb1, itb2)
func Concat[T any](itb1 Iterable[T], itb2 Iterable[T]) Iterable[T] {
if isEmpty(itb1) && isEmpty(itb2) {
return orEmpty(itb1)
}
if isEmpty(itb2) {
return itb1
}
if isEmpty(itb1) {
return itb2
}
return &concatIterable[T]{itb1, itb2}
}
func (itb *concatIterable[T]) Iterator() Iterator[T] {
return &concatIterator[T]{
it1: itb.itb1.Iterator(),
it2: itb.itb2.Iterator(),
}
}
func (it *concatIterator[T]) MoveNext() bool {
if it.done {
return false
}
if it.it1.MoveNext() {
it.current = it.it1.Current()
return true
}
if it.it2.MoveNext() {
it.current = it.it2.Current()
return true
}
it.MarkDone()
return false
}
func (it *concatIterator[T]) Current() T {
return it.current
}