Skip to content

Commit 43ebec2

Browse files
committed
feat(rx): add zip from iterable operator (#220)
feat(rx): add zip from iterable operator (#220) ref(rx): minor refactorings (#220)
1 parent ad4607c commit 43ebec2

9 files changed

+219
-73
lines changed

rx/duration.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,47 +37,47 @@ type execution struct {
3737
isTick bool
3838
}
3939

40-
func timeCausality[T any](elems ...any) (context.Context, Observable[T], Duration) {
40+
func timeCausality[T any](values ...any) (context.Context, Observable[T], Duration) {
4141
ch := make(chan Item[T], 1)
42-
fs := make([]execution, len(elems)+1)
42+
fs := make([]execution, len(values)+1)
4343
ctx, cancel := context.WithCancel(context.Background())
4444

45-
for i, elem := range elems {
45+
for i, value := range values {
4646
i := i
47-
elem := elem
47+
value := value
4848

49-
if el, ok := elem.(Item[T]); ok && el.IsTick() {
49+
if el, ok := value.(Item[T]); ok && el.IsTick() {
5050
fs[i] = execution{
5151
f: func() {},
5252
isTick: true,
5353
}
5454
} else {
55-
switch elem := elem.(type) {
55+
switch value := value.(type) {
5656
case Item[T]:
5757
fs[i] = execution{
5858
f: func() {
59-
ch <- elem
59+
ch <- value
6060
},
6161
}
6262

6363
case error:
6464
fs[i] = execution{
6565
f: func() {
66-
ch <- Error[T](elem)
66+
ch <- Error[T](value)
6767
},
6868
}
6969

7070
case T:
7171
fs[i] = execution{
7272
f: func() {
73-
ch <- Of(elem)
73+
ch <- Of(value)
7474
},
7575
}
7676
}
7777
}
7878
}
7979

80-
fs[len(elems)] = execution{
80+
fs[len(values)] = execution{
8181
f: func() {
8282
cancel()
8383
},

rx/factory_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ var _ = Describe("Factory", func() {
785785
defer leaktest.Check(GinkgoT())()
786786

787787
/*
788-
needs to accommodate item.N, ie the numeric aux value
788+
TODO: needs to accommodate item.N, ie the numeric aux value
789789
and also should be modified to support all the other
790790
new ways of interpreting an item (Ch, Tick, Tv)
791791
*/

rx/observable-operator-distinct_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ var _ = Describe("Observable operator", func() {
2020
defer cancel()
2121

2222
obs := testObservable[int](ctx, 1, 2, 2, 1, 3).Distinct(
23-
func(_ context.Context, item int) (int, error) {
24-
return item, nil
23+
func(_ context.Context, value int) (int, error) {
24+
return value, nil
2525
},
2626
)
2727

@@ -44,8 +44,8 @@ var _ = Describe("Observable operator", func() {
4444
defer cancel()
4545

4646
obs := testObservable[int](ctx, 1, 2, 2, errFoo, 3).Distinct(
47-
func(_ context.Context, item int) (int, error) {
48-
return item, nil
47+
func(_ context.Context, value int) (int, error) {
48+
return value, nil
4949
},
5050
)
5151

@@ -69,12 +69,12 @@ var _ = Describe("Observable operator", func() {
6969
defer cancel()
7070

7171
obs := testObservable[int](ctx, 1, 2, 2, 2, 3, 4).Distinct(
72-
func(_ context.Context, v int) (int, error) {
73-
if v == 3 {
72+
func(_ context.Context, value int) (int, error) {
73+
if value == 3 {
7474
return 0, errFoo
7575
}
7676

77-
return v, nil
77+
return value, nil
7878
},
7979
)
8080

@@ -195,8 +195,8 @@ var _ = Describe("Observable operator", func() {
195195
defer cancel()
196196

197197
obs := testObservable[int](ctx, 1, 2, 2, 1, 3).DistinctUntilChanged(
198-
func(_ context.Context, item int) (int, error) {
199-
return item, nil
198+
func(_ context.Context, value int) (int, error) {
199+
return value, nil
200200
}, rx.NativeItemLimitComparator, rx.WithCPUPool[int]())
201201

202202
rx.Assert(ctx, obs, rx.HasItems[int]{
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package rx_test
2+
3+
import (
4+
"context"
5+
6+
"github.com/fortytw2/leaktest"
7+
. "github.com/onsi/ginkgo/v2" //nolint:revive // ginkgo ok
8+
"github.com/snivilised/lorax/rx"
9+
)
10+
11+
var _ = Describe("Observable operator", func() {
12+
Context("ZipFromIterable", func() {
13+
When("source and other observers same length", func() {
14+
It("🧪 should: emit zipped elements", func() {
15+
// rxgo: Test_Observable_ZipFromObservable
16+
defer leaktest.Check(GinkgoT())()
17+
18+
ctx, cancel := context.WithCancel(context.Background())
19+
defer cancel()
20+
21+
obs1 := testObservable[int](ctx, 1, 2, 3)
22+
obs2 := testObservable[int](ctx, 10, 20, 30)
23+
zipper := func(_ context.Context, a, b rx.Item[int]) (int, error) {
24+
return a.V + b.V, nil
25+
}
26+
zip := obs1.ZipFromIterable(obs2, zipper)
27+
rx.Assert(ctx, zip, rx.HasItems[int]{
28+
Expected: []int{11, 22, 33},
29+
})
30+
})
31+
})
32+
33+
When("source observer longer than other", func() {
34+
It("🧪 should: omit zip from trailing items", func() {
35+
// rxgo: Test_Observable_ZipFromObservable_DifferentLength1
36+
defer leaktest.Check(GinkgoT())()
37+
38+
ctx, cancel := context.WithCancel(context.Background())
39+
defer cancel()
40+
41+
obs1 := testObservable[int](ctx, 1, 2, 3)
42+
obs2 := testObservable[int](ctx, 10, 20)
43+
zipper := func(_ context.Context, a, b rx.Item[int]) (int, error) {
44+
return a.V + b.V, nil
45+
}
46+
zip := obs1.ZipFromIterable(obs2, zipper)
47+
rx.Assert(ctx, zip, rx.HasItems[int]{
48+
Expected: []int{11, 22},
49+
})
50+
})
51+
})
52+
53+
When("source observer shorter than other", func() {
54+
It("🧪 should: omit zip from trailing items", func() {
55+
// rxgo: Test_Observable_ZipFromObservable_DifferentLength2
56+
defer leaktest.Check(GinkgoT())()
57+
58+
ctx, cancel := context.WithCancel(context.Background())
59+
defer cancel()
60+
61+
obs1 := testObservable[int](ctx, 1, 2)
62+
obs2 := testObservable[int](ctx, 10, 20, 30)
63+
zipper := func(_ context.Context, a, b rx.Item[int]) (int, error) {
64+
return a.V + b.V, nil
65+
}
66+
zip := obs1.ZipFromIterable(obs2, zipper)
67+
rx.Assert(ctx, zip, rx.HasItems[int]{
68+
Expected: []int{11, 22},
69+
})
70+
})
71+
})
72+
})
73+
})

0 commit comments

Comments
 (0)