Skip to content

Commit

Permalink
feat(rx): remove un used type (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Apr 24, 2024
1 parent ce1d0a1 commit 04cb3e5
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 85 deletions.
35 changes: 0 additions & 35 deletions rx/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,3 @@ func (c *NumericCalc[T]) Zero() T {
func ItemCalc[T Numeric]() Calculator[T] {
return new(NumericCalc[T])
}

type NumericItemCalc[T Numeric] struct {
zero T
}

func (c *NumericItemCalc[T]) Add(a, b Item[T]) int {
return a.Num() + b.Num()
}

func (c *NumericItemCalc[T]) Div(a, b Item[T]) Item[T] {
if b.Num() == 0 {
return c.Zero()
}

// !!!TODO(fix): we might need to use Opaque or another type
// so we don't lose the precision with this division.
//
return Num[T](a.Num() / b.Num())
}

func (c *NumericItemCalc[T]) Inc(i Item[T]) Item[T] {
n, _ := i.aux.(int)
n++
i.aux = n

return i
}

func (c *NumericItemCalc[T]) IsZero(v Item[T]) bool {
return v.Num() == 0
}

func (c *NumericItemCalc[T]) Zero() Item[T] {
return Num[T](0)
}
48 changes: 24 additions & 24 deletions rx/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ import (
)

type (
// Item is a wrapper having either a value, error or an auxiliary value.
// Item is a wrapper having either a value, error or an opaque value
//
Item[T any] struct {
V T
E error
aux any
disc enums.ItemDiscriminator
V T
E error
opaque any
disc enums.ItemDiscriminator
}

// TimestampItem attach a timestamp to an item.
Expand Down Expand Up @@ -79,38 +79,38 @@ func Error[T any](err error) Item[T] {
// Bool creates a type safe boolean instance
func Bool[T any](b bool) Item[T] {
return Item[T]{
aux: b,
disc: enums.ItemDiscBoolean,
opaque: b,
disc: enums.ItemDiscBoolean,
}
}

// Returns the tick value of item
func (it Item[T]) Bool() bool {
return it.aux.(bool)
return it.opaque.(bool)
}

// True creates a type safe boolean instance set to true
func True[T any]() Item[T] {
return Item[T]{
aux: true,
disc: enums.ItemDiscBoolean,
opaque: true,
disc: enums.ItemDiscBoolean,
}
}

// False creates a type safe boolean instance set to false
func False[T any]() Item[T] {
return Item[T]{
aux: false,
disc: enums.ItemDiscBoolean,
opaque: false,
disc: enums.ItemDiscBoolean,
}
}

// WCh creates an item from a channel
func WCh[T any](ch any) Item[T] {
if c, ok := ch.(chan<- Item[T]); ok {
return Item[T]{
aux: c,
disc: enums.ItemDiscWChan,
opaque: c,
disc: enums.ItemDiscWChan,
}
}

Expand All @@ -119,33 +119,33 @@ func WCh[T any](ch any) Item[T] {

// Returns the channel value of item
func (it Item[T]) Ch() chan<- Item[T] {
return it.aux.(chan<- Item[T])
return it.opaque.(chan<- Item[T])
}

// Num creates a type safe tick value instance
func Num[T any](n NumVal) Item[T] {
return Item[T]{
aux: n,
disc: enums.ItemDiscNumber,
opaque: n,
disc: enums.ItemDiscNumber,
}
}

// Returns the tick value of item
func (it Item[T]) Num() NumVal {
return it.aux.(NumVal)
return it.opaque.(NumVal)
}

// Opaque creates an item from any type of value
func Opaque[T any](o any) Item[T] {
return Item[T]{
aux: o,
disc: enums.ItemDiscOpaque,
opaque: o,
disc: enums.ItemDiscOpaque,
}
}

// Opaque returns the opaque value of item without typecast
func (it Item[T]) Opaque() any {
return it.aux
return it.opaque
}

// TV creates a type safe tick instance (no value)
Expand All @@ -158,14 +158,14 @@ func Tick[T any]() Item[T] {
// TV creates a type safe tick value instance
func TV[T any](tv int) Item[T] {
return Item[T]{
aux: tv,
disc: enums.ItemDiscTickValue,
opaque: tv,
disc: enums.ItemDiscTickValue,
}
}

// Returns the tick value of item
func (it Item[T]) TV() int {
return it.aux.(int)
return it.opaque.(int)
}

// Disc returns the discriminator of the item
Expand Down
2 changes: 1 addition & 1 deletion rx/must-try.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func must[T, O any](item Item[T], fn tryFunc[T, O]) (result O) {
}

func try[T, O any](item Item[T], expected enums.ItemDiscriminator) (O, error) {
if value, ok := item.aux.(O); ok {
if value, ok := item.opaque.(O); ok {
return value, nil
}

Expand Down
1 change: 0 additions & 1 deletion rx/observable-operator-contains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ var _ = Context("Observable operator", func() {
// rxgo: Test_Observable_Contain_Parallel
defer leaktest.Check(GinkgoT())()

// TODO(impl): CPUPool
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down
5 changes: 0 additions & 5 deletions rx/observable-operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1363,11 +1363,6 @@ func (op *mapOperator[T]) end(_ context.Context, _ chan<- Item[T]) {
func (op *mapOperator[T]) gatherNext(ctx context.Context,
item Item[T], dst chan<- Item[T], _ operatorOptions[T],
) {
// switch item.V.(type) {
// case *mapOperator:
// return
// }
// TODO: check above switch not required
item.SendContext(ctx, dst)
}

Expand Down
4 changes: 0 additions & 4 deletions rx/optional-single.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ type mapOperatorOptionalSingle[T any] struct {
func (op *mapOperatorOptionalSingle[T]) next(ctx context.Context,
item Item[T], dst chan<- Item[T], operatorOptions operatorOptions[T],
) {
// TODO: no longer needed: if !item.IsNumeric() {
// panic(fmt.Errorf("not a number (%v)", item))
// }
//
res, err := op.apply(ctx, item.V)
if err != nil {
dst <- Error[T](err)
Expand Down
28 changes: 13 additions & 15 deletions rx/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,15 @@ type (
IsZero(T) bool
Zero() T
}

// CalculatorItem defines numeric operations for T
CalculatorItem[T any] interface {
Add(Item[T], Item[T]) Item[T]
Div(Item[T], Item[T]) Item[T]
Inc(Item[T]) Item[T]
IsZero(Item[T]) bool
Zero() Item[T]
}

// Comparator defines a func that returns an int:
// - 0 if two elements are equals
// - A negative value if the first argument is less than the second
// - A positive value if the first argument is greater than the second
Comparator[T any] func(Item[T], Item[T]) int
// DistributionFunc used by GroupBy
DistributionFunc[T any] func(Item[T]) int

// DistributionFunc used by GroupByDynamic
DynamicDistributionFunc[T any] func(Item[T]) string

// InitLimit defines a function to be used with Min and Max operators that defines
// a limit initialiser, that is to say, for Max we need to initialise the internal
// maximum reference point to be minimum value for type T and the reverse for the
Expand Down Expand Up @@ -97,7 +85,6 @@ type (
// With generics, Map is a very awkward operator that needs special attention.
// In the short term, what we can say is that the base functionality only
// allows mapping to different values within the same type.
// FuncIntM[T any] func(context.Context, int) (int, error)
// FuncN defines a function that computes a value from N input values.
FuncN[T any] func(...T) T
// ErrorFunc defines a function that computes a value from an error.
Expand Down Expand Up @@ -136,26 +123,37 @@ type (
// RangeIterator allows the client defines how the Range operator emits derived
// items.
RangeIterator[T any] interface {
// Init performs pre iteration check and returns an error on failure.
Init() error
// Start should return the initial index value
Start() (*T, error)
// Step is used by Increment and defines the size of increment for each iteration
Step() T
// Increment increments the index value
Increment(index *T) T
// Plus(index, by T)
// While defines a condition that must be true for the loop to
// continue iterating.
While(current T) bool
}

// ProxyField assists the client when dealing with struct type of T. Used
// by RangeIteratorPF. Any struct type that is used as the type T must nominate
// a field (the proxy), that will be used as the iterator field. T represents
// the parent type and O represents the proxied field type.
ProxyField[T any, O Numeric] interface {
// Field defines the nominated proxy field
Field() O
// Inc is the incrementor function invoked by the iterator. Implementations
// should add the value of by to index.
Inc(index *T, by T) *T
Index(int) *T
// Index should return an instance of T that represents the numeric value
// of i. Typically, this requires returning an instance of T whose nominated
// proxied field is set to i.
Index(i int) *T
}

RangeIteratorPF[T ProxyField[T, O], O Numeric] interface {
// Init performs pre iteration check and returns an error on failure.
Init() error
// Start should return the initial index value
Start() (*T, error)
Expand Down

0 comments on commit 04cb3e5

Please sign in to comment.