Skip to content

Commit

Permalink
Add type param R to Reduce() and ReduceRgiht()
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaxwu committed Dec 23, 2021
1 parent f7c9333 commit 77641a8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion reduce.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package slices

// Reduce slice to a value
func Reduce[T any](slice []T, reduce func(total T, item T, index int, slice []T) T, init T) T {
func Reduce[T any, R any](slice []T, reduce func(total R, item T, index int, slice []T) R, init R) R {
for index := 0; index < len(slice); index++ {
init = reduce(init, slice[index], index, slice)
}
Expand Down
2 changes: 1 addition & 1 deletion reduce_right.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package slices

// ReduceRight slice to a value
func ReduceRight[T any](slice []T, reduce func(total T, item T, index int, slice []T) T, init T) T {
func ReduceRight[T any, R any](slice []T, reduce func(total R, item T, index int, slice []T) R, init R) R {
for index := len(slice) - 1; index >= 0; index-- {
init = reduce(init, slice[index], index, slice)
}
Expand Down
33 changes: 33 additions & 0 deletions reduce_right_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package slices

import (
"reflect"
"strconv"
"testing"
)

Expand Down Expand Up @@ -36,3 +37,35 @@ func TestReduceRight(t *testing.T) {
})
}
}

func TestReduceRight2(t *testing.T) {
type args struct {
slice []int
reduce func(total string, item int, index int, slice []int) string
init string
}
tests := []struct {
name string
args args
want string
}{
{
name: "number1",
args: args{
slice: []int{1, 2, 3, 4, 5},
reduce: func(total string, item int, index int, slice []int) string {
return total + strconv.Itoa(item)
},
init: "",
},
want: "54321",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ReduceRight(tt.args.slice, tt.args.reduce, tt.args.init); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ReduceRight() = %v, want %v", got, tt.want)
}
})
}
}
33 changes: 33 additions & 0 deletions reduce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package slices

import (
"reflect"
"strconv"
"testing"
)

Expand Down Expand Up @@ -36,3 +37,35 @@ func TestReduce(t *testing.T) {
})
}
}

func TestReduce2(t *testing.T) {
type args struct {
slice []int
reduce func(total string, item int, index int, slice []int) string
init string
}
tests := []struct {
name string
args args
want string
}{
{
name: "numberToString",
args: args{
slice: []int{1, 2, 3, 4, 5},
reduce: func(total string, item int, index int, slice []int) string {
return total + strconv.Itoa(item)
},
init: "",
},
want: "12345",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Reduce(tt.args.slice, tt.args.reduce, tt.args.init); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Reduce() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 77641a8

Please sign in to comment.