Skip to content

Commit

Permalink
add SliceReduce and MapReduce methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dsxack committed Oct 15, 2018
1 parent 43f9b8e commit 967bfc9
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 1 deletion.
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/DsXack/go

require (
github.com/davecgh/go-spew v1.1.0
github.com/pmezard/go-difflib v1.0.0
github.com/stretchr/testify v1.2.2
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
24 changes: 24 additions & 0 deletions reflect/collect/map_reduce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package collect

import "reflect"

// MapReduce method reduces the map to a single value, passing the result of each iteration
// into the subsequent iteration:
func MapReduce(mapValue interface{}, initialValue interface{}, reduceFunc interface{}) interface{} {
refFunc := reflect.ValueOf(reduceFunc)
refMap := reflect.ValueOf(mapValue)

refInitialValue := reflect.ValueOf(initialValue)
refValue := reflect.New(refInitialValue.Type())
refValue.Elem().Set(refInitialValue)

for _, k := range refMap.MapKeys() {
v := refFunc.Call(
[]reflect.Value{refValue.Elem(), k, refMap.MapIndex(k)},
)[0]

refValue.Elem().Set(v)
}

return refValue.Elem().Interface()
}
16 changes: 16 additions & 0 deletions reflect/collect/map_reduce_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package collect

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestMapReduce(t *testing.T) {
res := MapReduce(
map[int]int{1: 1, 2: 2},
0,
func(accum int, key, value int) int { return accum + value },
).(int)

assert.Equal(t, 3, res)
}
2 changes: 1 addition & 1 deletion reflect/collect/slice_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ func TestSliceMap(t *testing.T) {
},
).([]string)

assert.ElementsMatch(t, res, []string{"1", "4", "9"})
assert.ElementsMatch(t, []string{"1", "4", "9"}, res)
}
23 changes: 23 additions & 0 deletions reflect/collect/slice_reduce.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package collect

import "reflect"

// SliceReduce method reduces the slice to a single value, passing the result of each iteration
// into the subsequent iteration:
func SliceReduce(value interface{}, initialValue interface{}, mapFunc interface{}) interface{} {
refFunc := reflect.ValueOf(mapFunc)
refSlice := reflect.ValueOf(value)

refInitialValue := reflect.ValueOf(initialValue)
refValue := reflect.New(refInitialValue.Type())
refValue.Elem().Set(refInitialValue)

for i := 0; i < refSlice.Len(); i += 1 {
v := refFunc.Call(
[]reflect.Value{refValue.Elem(), reflect.ValueOf(i), refSlice.Index(i)},
)[0]
refValue.Elem().Set(v)
}

return refValue.Elem().Interface()
}
16 changes: 16 additions & 0 deletions reflect/collect/slice_reduce_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package collect

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestSliceReduce(t *testing.T) {
res := SliceReduce(
[]int{1, 2, 3},
0,
func(accum, index, value int) int { return accum + value },
).(int)

assert.Equal(t, 6, res)
}

0 comments on commit 967bfc9

Please sign in to comment.