-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add SliceReduce and MapReduce methods
- Loading branch information
Showing
7 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |