Skip to content

Commit 3c07d8b

Browse files
committed
Accumulate accept any type
1 parent f0842a3 commit 3c07d8b

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

accumulate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package iterium
22

33
// Accumulate returns an iterator that sends the accumulated
44
// result from the binary function to the channel.
5-
func Accumulate[N Number](iterable Iter[N], operator func(N, N) N) Iter[N] {
6-
iter := Instance[N](iterable.Count(), iterable.IsInfinite())
5+
func Accumulate[T any](iterable Iter[T], operator func(T, T) T) Iter[T] {
6+
iter := Instance[T](iterable.Count(), iterable.IsInfinite())
77

88
go func() {
99
defer iterRecover()
1010
defer iter.Close()
1111

12-
var last N
12+
var last T
1313
var start bool
1414
for true {
1515
next, err := iterable.Next()

accumulate_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package iterium
22

33
import (
4+
"fmt"
45
"github.com/stretchr/testify/assert"
56
"testing"
67
)
@@ -24,3 +25,15 @@ func TestAccumulate(t *testing.T) {
2425
assert.Exactly(t, []int{1, 2, 6, 24, 120}, slice)
2526
}
2627
}
28+
29+
func mergeAcc(first, second string) string {
30+
return fmt.Sprintf("%s-%s", first, second)
31+
}
32+
33+
func TestAccumulateString(t *testing.T) {
34+
data := New("A", "B", "C", "D")
35+
accumulateString := Accumulate(data, mergeAcc)
36+
if slice, err := accumulateString.Slice(); assert.Nil(t, err) {
37+
assert.Exactly(t, []string{"A", "A-B", "A-B-C", "A-B-C-D"}, slice)
38+
}
39+
}

0 commit comments

Comments
 (0)