Beispiel #1
0
func ExampleMakeMap_Second() {
	// Let's map the person fullnames to an array of names
	type Person struct{ FirstName, LastName string }
	var mapPersonsToStrings func([]Person, func(Person, int) string) []string
	if err := funcs.MakeMap(&mapPersonsToStrings); err != nil {
		log.Fatal(err)
	}
	people := []Person{{"John", "Doe"}, {"Jane", "Doe"}}
	fmt.Println(mapPersonsToStrings(people, func(p Person, index int) string {
		return fmt.Sprintf("#%d %s %s", index, p.FirstName, p.LastName)
	}))
	// Output:
	// [#0 John Doe #1 Jane Doe]
}
Beispiel #2
0
func ExampleMakeMap() {
	// Let's map book titles
	type Book struct {
		Title string
		ID    int
	}
	var mapBooksToStrings func([]Book, func(Book) string) []string
	if err := funcs.MakeMap(&mapBooksToStrings); err != nil {
		log.Fatal(err)
	}
	books := []Book{{"Les Misérables", 34043}, {"Germinal", 349439}}
	fmt.Println(mapBooksToStrings(books, func(book Book) string {
		return book.Title
	}))
	// Output:
	// [Les Misérables Germinal]
}
Beispiel #3
0
func BenchmarkMap_MakeMap(b *testing.B) {
	type Person struct {
		Name string
		Age  int
	}
	var (
		mapPersonsToNames func(persons []Person, mapper func(person Person) string) []string
		persons           = []Person{{"Joe", 18}, {"Kim", 29}, {"Jane", 38}, {"Jack", 24}, {"David", 79}}
	)
	if err := funcs.MakeMap(&mapPersonsToNames); err != nil {
		b.Fatal(err)
	}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_ = mapPersonsToNames(persons, func(person Person) string {
			return person.Name
		})
	}
}
Beispiel #4
0
package platform

import (
	"github.com/Mparaiso/go-tiger/funcs"
)

var (
	mapStringsToStrings func([]string, func(string) string) []string
	_                   = funcs.Must(funcs.MakeMap(&mapStringsToStrings))
)
Beispiel #5
0
	}
	return result + "\n]}\n"
}

var (
	keyValuesByObjectID func(collection []reflect.Value, selector func(reflect.Value) bson.ObjectId) map[bson.ObjectId]reflect.Value
	_                   = funcs.Must(funcs.MakeKeyBy(&keyValuesByObjectID))

	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))