Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rx): add sum operator (#210) #211

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions rx/observable-operator-sum_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package rx_test

import (
"context"

"github.com/fortytw2/leaktest"
. "github.com/onsi/ginkgo/v2" //nolint:revive // ginkgo ok

"github.com/snivilised/lorax/rx"
)

var _ = Describe("Observable operator", func() {
Context("Sum", func() {
When("principle", func() {
It("🧪 should: return sum", func() {
// rxgo: Test_Observable_SumFloat32_OnlyFloat32
defer leaktest.Check(GinkgoT())()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

rx.Assert(ctx, testObservable[float32](ctx,
// explicit cast to float32 is required to match
// the generic type to ensure observable sends the
// item. Failure to perform this task results in
// an error ("channel value: '1' not sent (wrong type?)").
//
float32(1.0), float32(2.0), float32(3.0),
).Sum(rx.Calc[float32]()),
rx.HasItem[float32]{
Expected: 6.0,
})
})
})

When("empty", func() {
It("🧪 should: return empty result", func() {
// rxgo: Test_Observable_SumFloat32_Empty
defer leaktest.Check(GinkgoT())()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

rx.Assert(ctx,
rx.Empty[float32]().Sum(rx.Calc[float32]()),
rx.IsEmpty[float32]{},
)
})
})

// NB(Test_Observable_SumFloat32_DifferentTypes): Sum does
// not support different types, only values of type T are
// supported.

Context("Errors", func() {
When("error", func() {
It("🧪 should: result in error", func() {
// rxgo: Test_Observable_SumFloat32_Error
defer leaktest.Check(GinkgoT())()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

rx.Assert(ctx, testObservable[float32](ctx,
// we omit the explicit cast to float32 as normally would
// be the case, to enforce a resulting error.
//
1.1, 2.2, 3.3,
).Sum(rx.Calc[float32]()),
rx.HasAnError[float32]{},
)
})
})
})
})
})
7 changes: 7 additions & 0 deletions rx/observable-operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2181,6 +2181,13 @@ func (o *ObservableImpl[T]) StartWith(iterable Iterable[T], opts ...Option[T]) O
}
}

// Sum calculates the average emitted by an Observable and emits the result
func (o *ObservableImpl[T]) Sum(calc Calculator[T], opts ...Option[T]) OptionalSingle[T] {
return o.Reduce(func(_ context.Context, acc, elem Item[T]) (T, error) {
return calc.Add(acc.V, elem.V), nil
}, opts...)
}

// !!!

// ToSlice collects all items from an Observable and emit them in a slice and
Expand Down
1 change: 1 addition & 0 deletions rx/observable.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Observable[T any] interface {
SkipLast(nth uint, opts ...Option[T]) Observable[T]
SkipWhile(apply Predicate[T], opts ...Option[T]) Observable[T]
StartWith(iterable Iterable[T], opts ...Option[T]) Observable[T]
Sum(calc Calculator[T], opts ...Option[T]) OptionalSingle[T]
ToSlice(initialCapacity int, opts ...Option[T]) ([]Item[T], error)
}

Expand Down
Loading