Пример #1
0
func ExampleMakeFilter() {
	type Person struct {
		Age  int
		Name string
	}
	var filterAdults func(persons []Person, predicate func(person Person, i int) bool) []Person
	fmt.Println(funcs.MakeFilter(&filterAdults))
	// TODO: handle error
	people := []Person{{18, "Joe"}, {26, "David"}, {15, "Anna"}}
	adults := filterAdults(people, func(person Person, index int) bool {
		return person.Age >= 18
	})
	fmt.Println(len(adults))
	fmt.Println(adults[0].Age, adults[0].Name)

	// Output:
	// <nil>
	// 2
	// 18 Joe
}
Пример #2
0
	getKeys func(map[bson.ObjectId]reflect.Value) []bson.ObjectId
	_       = funcs.Must(funcs.MakeGetKeys(&getKeys))

	flatten func([][]interface{}) []interface{}
	_       = funcs.Must(funcs.MakeFlatten(&flatten))

	mapResultsToInterfaces func([]map[string]interface{}, func(map[string]interface{}) []interface{}) [][]interface{}
	_                      = funcs.Must(funcs.MakeMap(&mapResultsToInterfaces))

	keyResultsBySourceID func(results []map[string]interface{}, mapper func(result map[string]interface{}) (id bson.ObjectId)) map[bson.ObjectId]map[string]interface{}
	_                    = funcs.Must(funcs.MakeKeyBy(&keyResultsBySourceID))

	mapResultsToRelatedObjectIds func(results []map[string]interface{}, mapper func(result map[string]interface{}) bson.ObjectId) []bson.ObjectId
	_                            = funcs.Must(funcs.MakeMap(&mapResultsToRelatedObjectIds))

	keyRelatedResultsByObjectID func(results []reflect.Value, mapper func(result reflect.Value) bson.ObjectId) map[bson.ObjectId]reflect.Value
	_                           = funcs.Must(funcs.MakeKeyBy(&keyRelatedResultsByObjectID))

	mapInterfacesToObjectIds func([]interface{}, func(interface{}) bson.ObjectId) []bson.ObjectId
	_                        = funcs.Must(funcs.MakeMap(&mapInterfacesToObjectIds))

	filter func([]bson.ObjectId, func(id bson.ObjectId) bool) []bson.ObjectId
	_      = funcs.Must(funcs.MakeFilter(&filter))

	indexOf func([]interface{}, interface{}) int
	_       = funcs.Must(funcs.MakeIndexOf(&indexOf))

	indexOfString func([]string, string) int
	_             = funcs.Must(funcs.MakeIndexOf(&indexOfString))
)