Skip to content

Commit

Permalink
Merge pull request #116 from vellotis/#50
Browse files Browse the repository at this point in the history
Fixes issue #50
  • Loading branch information
thoas committed Mar 11, 2021
2 parents bb6cb22 + cdeac5b commit 6d0684b
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 11 deletions.
22 changes: 21 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,26 @@ see also, typesafe implementations: FilterInt_, FilterInt64_, FilterFloat32_, Fi
.. _FilterInt64: https://godoc.org/github.com/thoas/go-funk#FilterInt64
.. _FilterString: https://godoc.org/github.com/thoas/go-funk#FilterString

funk.Reduce
...........

Reduces an iteratee based on an accumulator function or operation rune for numbers.

.. code-block:: go
// Using operation runes. '+' and '*' only supported.
r := funk.Reduce([]int{1, 2, 3, 4}, '+', float64(0)) // 10
r := funk.Reduce([]int{1, 2, 3, 4}, '*', 1) // 24
// Using accumulator function
r := funk.Reduce([]int{1, 2, 3, 4}, func(acc float64, num int) float64 {
return acc + float64(num)
}, float64(0)) // 10
r := funk.Reduce([]int{1, 2, 3, 4}, func(acc string, num int) string {
return acc + fmt.Sprint(num)
}, "") // "1234"
funk.Find
.........

Expand Down Expand Up @@ -318,7 +338,7 @@ Manipulates an iteratee (map, slice) and transforms it to another type:
}) // map[string]string{"1": "Florent", "2": "Gilles"}
funk.FlatMap
........
............

Manipulates an iteratee (map, slice) and transforms it to to a flattened collection of another type:

Expand Down
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
5 changes: 5 additions & 0 deletions chain_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,11 @@ func TestChainReduce(t *testing.T) {
ReduceFunc: '*',
Acc: 1,
},
{
In: []string{"1", "2", "3", "4"},
ReduceFunc: func(acc string, elem string) string { return acc + elem },
Acc: "",
},
}

for idx, tc := range testCases {
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: 5 additions & 0 deletions lazy_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,11 @@ func TestLazyReduce(t *testing.T) {
ReduceFunc: '*',
Acc: 1,
},
{
In: []string{"1", "2", "3", "4"},
ReduceFunc: func(acc string, elem string) string { return acc + elem },
Acc: "",
},
}

for idx, tc := range testCases {
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()
}
14 changes: 10 additions & 4 deletions reduce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ func TestReduce(t *testing.T) {
Arr interface{}
Func interface{}
Acc interface{}
Result float64
Result interface{}
}{
{
[]int{1, 2, 3, 4},
func(acc, elem int) int { return acc + elem },
func(acc, elem float64) float64 { return acc + elem },
0,
float64(10),
},
{
&[]int16{1, 2, 3, 4},
'+',
5,
float64(15),
int16(15),
},
{
[]float64{1.1, 2.2, 3.3},
Expand All @@ -36,14 +36,20 @@ func TestReduce(t *testing.T) {
&[]int{1, 2, 3, 5},
func(acc int8, elem int16) int32 { return int32(acc) * int32(elem) },
1,
float64(30),
int32(30),
},
{
[]interface{}{1, 2, 3.3, 4},
'*',
1,
float64(26.4),
},
{
[]string{"1", "2", "3", "4"},
func(acc string, elem string) string { return acc + elem },
"",
"1234",
},
}

for idx, test := range testCases {
Expand Down

0 comments on commit 6d0684b

Please sign in to comment.