github.com/l3x/learn-fp-go@v0.0.0-20171228022418-7639825d0b71/4-purely-functional/ch09-functor-monoid/02_generics_cars/src/car/car_slice.go (about)

     1  // Generated by: gen
     2  // TypeWriter: slice
     3  // Directive: +gen on Car
     4  
     5  package car
     6  
     7  // CarSlice is a slice of type Car. Use it where you would use []Car.
     8  type CarSlice []Car
     9  
    10  // Where returns a new CarSlice whose elements return true for func. See: http://clipperhouse.github.io/gen/#Where
    11  func (rcv CarSlice) Where(fn func(Car) bool) (result CarSlice) {
    12  	for _, v := range rcv {
    13  		if fn(v) {
    14  			result = append(result, v)
    15  		}
    16  	}
    17  	return result
    18  }
    19  
    20  // SumDollars sums Car over elements in CarSlice. See: http://clipperhouse.github.io/gen/#Sum
    21  func (rcv CarSlice) SumDollars(fn func(Car) Dollars) (result Dollars) {
    22  	for _, v := range rcv {
    23  		result += fn(v)
    24  	}
    25  	return
    26  }
    27  
    28  // GroupByString groups elements into a map keyed by string. See: http://clipperhouse.github.io/gen/#GroupBy
    29  func (rcv CarSlice) GroupByString(fn func(Car) string) map[string]CarSlice {
    30  	result := make(map[string]CarSlice)
    31  	for _, v := range rcv {
    32  		key := fn(v)
    33  		result[key] = append(result[key], v)
    34  	}
    35  	return result
    36  }
    37  
    38  // SelectDollars projects a slice of Dollars from CarSlice, typically called a map in other frameworks. See: http://clipperhouse.github.io/gen/#Select
    39  func (rcv CarSlice) SelectDollars(fn func(Car) Dollars) (result []Dollars) {
    40  	for _, v := range rcv {
    41  		result = append(result, fn(v))
    42  	}
    43  	return
    44  }