Skip to content

Commit 6d0684b

Browse files
authored
Merge pull request #116 from vellotis/#50
Fixes issue #50
2 parents bb6cb22 + cdeac5b commit 6d0684b

File tree

8 files changed

+46
-11
lines changed

8 files changed

+46
-11
lines changed

README.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,26 @@ see also, typesafe implementations: FilterInt_, FilterInt64_, FilterFloat32_, Fi
261261
.. _FilterInt64: https://godoc.org/github.com/thoas/go-funk#FilterInt64
262262
.. _FilterString: https://godoc.org/github.com/thoas/go-funk#FilterString
263263

264+
funk.Reduce
265+
...........
266+
267+
Reduces an iteratee based on an accumulator function or operation rune for numbers.
268+
269+
.. code-block:: go
270+
271+
// Using operation runes. '+' and '*' only supported.
272+
r := funk.Reduce([]int{1, 2, 3, 4}, '+', float64(0)) // 10
273+
r := funk.Reduce([]int{1, 2, 3, 4}, '*', 1) // 24
274+
275+
// Using accumulator function
276+
r := funk.Reduce([]int{1, 2, 3, 4}, func(acc float64, num int) float64 {
277+
return acc + float64(num)
278+
}, float64(0)) // 10
279+
280+
r := funk.Reduce([]int{1, 2, 3, 4}, func(acc string, num int) string {
281+
return acc + fmt.Sprint(num)
282+
}, "") // "1234"
283+
264284
funk.Find
265285
.........
266286

@@ -318,7 +338,7 @@ Manipulates an iteratee (map, slice) and transforms it to another type:
318338
}) // map[string]string{"1": "Florent", "2": "Gilles"}
319339
320340
funk.FlatMap
321-
........
341+
............
322342

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

builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type Builder interface {
3939
LastIndexOf(elem interface{}) int
4040
NotEmpty() bool
4141
Product() float64
42-
Reduce(reduceFunc, acc interface{}) float64
42+
Reduce(reduceFunc, acc interface{}) interface{}
4343
Sum() float64
4444
Type() reflect.Type
4545
Value() interface{}

chain_builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (b *chainBuilder) NotEmpty() bool {
125125
func (b *chainBuilder) Product() float64 {
126126
return Product(b.collection)
127127
}
128-
func (b *chainBuilder) Reduce(reduceFunc, acc interface{}) float64 {
128+
func (b *chainBuilder) Reduce(reduceFunc, acc interface{}) interface{} {
129129
return Reduce(b.collection, reduceFunc, acc)
130130
}
131131
func (b *chainBuilder) Sum() float64 {

chain_builder_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,11 @@ func TestChainReduce(t *testing.T) {
10121012
ReduceFunc: '*',
10131013
Acc: 1,
10141014
},
1015+
{
1016+
In: []string{"1", "2", "3", "4"},
1017+
ReduceFunc: func(acc string, elem string) string { return acc + elem },
1018+
Acc: "",
1019+
},
10151020
}
10161021

10171022
for idx, tc := range testCases {

lazy_builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (b *lazyBuilder) NotEmpty() bool {
100100
func (b *lazyBuilder) Product() float64 {
101101
return Product(b.exec())
102102
}
103-
func (b *lazyBuilder) Reduce(reduceFunc, acc interface{}) float64 {
103+
func (b *lazyBuilder) Reduce(reduceFunc, acc interface{}) interface{} {
104104
return Reduce(b.exec(), reduceFunc, acc)
105105
}
106106
func (b *lazyBuilder) Sum() float64 {

lazy_builder_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,11 @@ func TestLazyReduce(t *testing.T) {
10121012
ReduceFunc: '*',
10131013
Acc: 1,
10141014
},
1015+
{
1016+
In: []string{"1", "2", "3", "4"},
1017+
ReduceFunc: func(acc string, elem string) string { return acc + elem },
1018+
Acc: "",
1019+
},
10151020
}
10161021

10171022
for idx, tc := range testCases {

reduce.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

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

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

86-
resultInterface := accValue.Convert(returnType).Interface()
87-
return resultInterface.(float64)
86+
return accValue.Convert(returnType).Interface()
8887
}

reduce_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ func TestReduce(t *testing.T) {
1212
Arr interface{}
1313
Func interface{}
1414
Acc interface{}
15-
Result float64
15+
Result interface{}
1616
}{
1717
{
1818
[]int{1, 2, 3, 4},
19-
func(acc, elem int) int { return acc + elem },
19+
func(acc, elem float64) float64 { return acc + elem },
2020
0,
2121
float64(10),
2222
},
2323
{
2424
&[]int16{1, 2, 3, 4},
2525
'+',
2626
5,
27-
float64(15),
27+
int16(15),
2828
},
2929
{
3030
[]float64{1.1, 2.2, 3.3},
@@ -36,14 +36,20 @@ func TestReduce(t *testing.T) {
3636
&[]int{1, 2, 3, 5},
3737
func(acc int8, elem int16) int32 { return int32(acc) * int32(elem) },
3838
1,
39-
float64(30),
39+
int32(30),
4040
},
4141
{
4242
[]interface{}{1, 2, 3.3, 4},
4343
'*',
4444
1,
4545
float64(26.4),
4646
},
47+
{
48+
[]string{"1", "2", "3", "4"},
49+
func(acc string, elem string) string { return acc + elem },
50+
"",
51+
"1234",
52+
},
4753
}
4854

4955
for idx, test := range testCases {

0 commit comments

Comments
 (0)