Beispiel #1
0
func ExampleMakeReduce_Second() {
	// Let's make a reduce function
	var reduce func([]string, func(result string, s string, i int) string, string) string
	if err := funcs.MakeReduce(&reduce); err != nil {
		log.Fatal(err)
	}
	strings := []string{"foo", "bar"}
	fmt.Println(reduce(strings, func(result, element string, index int) string {
		return result + fmt.Sprint(index) + element
	}, ""))
	// Output:
	// 0foo1bar
}
Beispiel #2
0
func ExampleMakeReduce() {
	// Let's use MakeReduce to create a map function.
	type Person struct {
		Name string
		Age  int
	}
	var MapPersonsToNames func(persons []Person, reducer func(names []string, person Person) []string, initial []string) []string
	err := funcs.MakeReduce(&MapPersonsToNames)
	if err != nil {
		log.Fatal(err)
	}
	result := MapPersonsToNames([]Person{{"Frank", 30}, {"John", 43}, {"Jane", 26}}, func(names []string, person Person) []string {
		return append(names, person.Name)
	}, []string{})
	fmt.Println(result)
	// Output:
	// [Frank John Jane]
}
Beispiel #3
0
func TestErrInvalidNumberOfInputValues(t *testing.T) {
	var reduce func(collection []string, reducer func(result string, element string) string)
	err := funcs.MakeReduce(&reduce)
	test.Fatal(t, err, funcs.ErrInvalidNumberOfInputValues)
}
Beispiel #4
0
func TestErrNotAFunction(t *testing.T) {
	var reduce interface{}
	err := funcs.MakeReduce(&reduce)
	test.Fatal(t, err, funcs.ErrNotAFunction)
}
Beispiel #5
0
func TestErrNotApointer(t *testing.T) {
	var reduce func([]int, func(int, int) int, int)
	err := funcs.MakeReduce(reduce)
	test.Fatal(t, err, funcs.ErrNotAPointer)
}