Skip to content

Commit

Permalink
thoas#50 Change funk.Reduce return type to interface{}
Browse files Browse the repository at this point in the history
  • Loading branch information
vellotis committed Mar 10, 2021
1 parent d02a012 commit a365a2b
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Builder interface {
LastIndexOf(elem interface{}) int
NotEmpty() bool
Product() float64
Reduce(reduceFunc, acc interface{}) float64
Reduce(reduceFunc, acc interface{}) interface{}
Sum() float64
Type() reflect.Type
Value() interface{}
Expand Down
2 changes: 1 addition & 1 deletion chain_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (b *chainBuilder) NotEmpty() bool {
func (b *chainBuilder) Product() float64 {
return Product(b.collection)
}
func (b *chainBuilder) Reduce(reduceFunc, acc interface{}) float64 {
func (b *chainBuilder) Reduce(reduceFunc, acc interface{}) interface{} {
return Reduce(b.collection, reduceFunc, acc)
}
func (b *chainBuilder) Sum() float64 {
Expand Down
2 changes: 1 addition & 1 deletion lazy_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (b *lazyBuilder) NotEmpty() bool {
func (b *lazyBuilder) Product() float64 {
return Product(b.exec())
}
func (b *lazyBuilder) Reduce(reduceFunc, acc interface{}) float64 {
func (b *lazyBuilder) Reduce(reduceFunc, acc interface{}) interface{} {
return Reduce(b.exec(), reduceFunc, acc)
}
func (b *lazyBuilder) Sum() float64 {
Expand Down
5 changes: 2 additions & 3 deletions reduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// Reduce takes a collection and reduces it to a single value using a reduction
// function (or a valid symbol) and an accumulator value.
func Reduce(arr, reduceFunc, acc interface{}) float64 {
func Reduce(arr, reduceFunc, acc interface{}) interface{} {
arrValue := redirectValue(reflect.ValueOf(arr))

if !IsIteratee(arrValue.Interface()) {
Expand Down Expand Up @@ -83,6 +83,5 @@ func Reduce(arr, reduceFunc, acc interface{}) float64 {
accValue = result[0]
}

resultInterface := accValue.Convert(returnType).Interface()
return resultInterface.(float64)
return accValue.Convert(returnType).Interface()
}

0 comments on commit a365a2b

Please sign in to comment.