示例#1
0
func main() {

	words := []string{"doing", "go", "functional", "programming"}
	numbers := []int{1, 2, 3}

	fmt.Println("MAP")

	fmt.Println("Square", numbers, f.Map(func(x int) int { return x * x }, numbers))
	fmt.Println("Length", words, f.Map(func(x string) int { return len(x) }, words))
	fmt.Println("Hash", words, f.Map(hash, words))

	fmt.Println("REDUCE")

	fmt.Println("Sum", numbers, f.Reduce(func(a int, x int) int { return a + x }, numbers, 0))
	fmt.Println("Total length", words, f.Reduce(func(a int, x string) int { return a + len(x) }, words, 0))

	fmt.Println("FILTER")
	fmt.Println("Long words (>5)", words, f.Filter(func(x string) bool { return (len(x) > 5) }, words).([]string))

}
示例#2
0
func pipeline(fs []BandFunc, bs []Band) []Band {

	return f.DoMap(func(b Band) Band {

		return f.Reduce(func(b Band, f BandFunc) Band {

			return f(b)

		}, fs, b).(Band)

	}, bs).([]Band)
}
示例#3
0
func main() {

	people := []Person{Person{Name: "Mary", Height: 160}, Person{Name: "Isla", Height: 80}, Person{Name: "Sam"}}
	fmt.Println("People", people)

	knowingHeight := f.Filter(func(person Person) bool {
		return (person.Height > 0)
	}, people).([]Person)
	fmt.Println("Knowing height people", knowingHeight)

	average := f.Reduce(func(a float32, person Person) float32 {
		return a + (float32(person.Height) / float32(len(knowingHeight)))
	}, people, float32(0)).(float32)
	fmt.Println("Average height among people we know their height: ", average)
}
示例#4
0
func draw(r Race) {

	drawing := f.Map(func(car Car) string {

		return f.Reduce(func(a string, i int) string {

			return a + "-"

		}, make([]int, car.Position), "").(string)

	}, r.Cars).([]string)

	fmt.Println("............................")
	f.Map(func(a string) interface{} {

		fmt.Println(a)
		return a
	}, drawing)
}